Is there any validation check for already registered email id in ruby programming - ruby

# load in the webdriver gem to interect with selenium
require 'selenium-webdriver'
#setup chrome plugin
driver = Selenium::WebDriver::Chrome.driver_path='C:\Users\vidhi\Desktop\Ruby Folder\chromedriver.exe'
#this line will start the browser
driver = Selenium::WebDriver.for :chrome
wait = Selenium::WebDriver::Wait.new(:timeout => 20)
def document_initialised(driver)
driver.execute_script('return initialised')
end
#Navigate to URl
driver.get "http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation"
#Maximize the window
driver.manage.window.maximize
sleep 6
driver.action.key_down(:enter).perform
sleep 5
driver.find_element(:id,"email_create").send_keys "demouser099#gmail.com"
sleep 5
driver.action.key_down(:enter).perform
driver.find_element(:id,"SubmitCreate").click
sleep 2
driver.action.key_down(:enter).perform
#### Check that the radio button exists
puts "Test Passed: Radio button found" if wait.until {
driver.find_element(:id,"uniform-id_gender2").displayed?
}
#Change the state of the Radio Buttons
cb1 = wait.until {
element1 = driver.find_element(:id,"uniform-id_gender1")
element1 if element1.displayed?
}
cb1.click if cb1.selected? == false
cb3 = wait.until {
element2 = driver.find_element(:id,"uniform-id_gender2")
element2 if element2.displayed?
}
cb3.click if cb3.selected? == false
sleep 4
driver.find_element(:id,"customer_firstname").send_keys "demo"
sleep 3
driver.find_element(:id,"customer_lastname").send_keys "user"
sleep 5
password=driver.find_element(:id,"passwd").send_keys "demo#123"
sleep 4
if driver.find_element(:id,"uniform-days").displayed?
puts "Days dropdown is displayed.."
select = driver.find_element(:id,"uniform-days")
alloptions = select.find_elements(:tag_name,"option")
puts alloptions.size
alloptions.each do |option|
puts "Value is.."+option.attribute("value")
if option.attribute("value")=="20"
option.click
puts "Value has been selected.."
sleep 5
break
end
end
end
month_dropdown = driver.find_element(:id,"months")
months=Selenium::WebDriver::Support::Select.new(month_dropdown)
months.select_by(:text,"April ")
puts driver.find_element(:id,"months").text.include?("April")
years_dropdown = driver.find_element(:id,"years")
years=Selenium::WebDriver::Support::Select.new(years_dropdown)
years.select_by(:index,28)
sleep 3
driver.find_element(:id,"address1").send_keys "45 calony A"
sleep 4
if driver.find_element(:id,"uniform-id_state").displayed?
puts "State dropdown is displayed.."
select = driver.find_element(:id,"uniform-id_state")
alloptions = select.find_elements(:tag_name,"option")
puts alloptions.size
alloptions.each do |option|
puts "Text is.."+option.attribute("text")
if option.attribute("text")=="California"
option.click
puts "text has been selected.."
sleep 5
break
end
end
end
#Enter city
driver.find_element(:id,"city").send_keys "Los Angeles"
sleep 4
driver.find_element(:id,"postcode").send_keys "23654"
Country_dropdown =driver.find_element(:id,"id_country")
country=Selenium::WebDriver::Support::Select.new(Country_dropdown)
country.select_by(:index,1)
sleep 5
#Input Mobile Number
driver.find_element(:id,"phone_mobile").send_keys "985256238"
sleep 5
#Click on Submit button
driver.find_element(:id,"submitAccount").click
sleep 5
I have write script for registration form I have run my automation script then on first time it is successfully pass but when run twice with same email id then it is showing validation message as email already exists try with new one..so for this validation what is the assertion in ruby?
Here is the link for registration form-http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation

General side notes:
I see that you have a method defined that you don't use.
def document_initialised(driver)
driver.execute_script('return initialised')
end
Also there seems to be some un-nneeded variable assignment and not-needed driver.action.key_down(:enter).perform
It is possible to create a class to keep your code more structured ;)
To your question:
I would create an if to check if the error is displayed on the page or not after you did you submit with driver.find_element(:id,"SubmitCreate").click. You can see an example in about it in #register_email in my example.
If I create some class from it and do some other removal of code I think is obsolete, it looks like this (but it has not been tested by me).
# load in the webdriver gem to interect with selenium
require 'selenium-webdriver'
class RegisterUser
def run
return unless register_email
do_the_rest
end
private
def driver
return #driver if #driver.present?
Selenium::WebDriver::Chrome.driver_path='C:\Users\vidhi\Desktop\Ruby Folder\chromedriver.exe'
#driver = Selenium::WebDriver.for(:chrome)
#driver.get "http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation"
sleep 6
#driver.manage.window.maximize
#driver
end
def wait
Selenium::WebDriver::Wait.new(:timeout => 20)
end
def register_email
driver.find_element(:id,"email_create").send_keys "demouser099#gmail.com"
driver.find_element(:id,"SubmitCreate").click
sleep 5
return true unless driver.find_element(:id,"create_account_error").displayed?
# An account using this email address has already been registered.
# Please enter a valid password or request a new one.
puts driver.find_element(:id,"create_account_error").text
false
end
def do_the_rest
# Here you can put the rest of the code, or clean up even more and split into multiple methods
end
end
RegisterUser.new.run

Related

Is there another way to only get last loaded data using while loop in Ruby

I'm doing a web scraping with a dynamic website that has a "Load more" button. Though I solve the load more problems by using a while loop. It has another challenge when I try to scrape the data it just keeps multiplying. So the first batch of data is 24 data when I scrape the second batch it also scrapes the first batch so it scrape 48 data with only 24 new data being added and soon.
heres my code.
require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
url ="https://www.example.com/categories/car-parts"
driver.navigate.to "#{url}"
wait = Selenium::WebDriver::Wait.new(:timeout => 20)
while driver.page_source.include? "Load more"
load_more = wait.until {
load_more_element = driver.find_element(css: ".styles__loadMore___yYAF4")
}
sleep 3
load_more.click()
puts "load_more"
sleep 3
seller_url = wait.until {
element = driver.find_elements(:css, ".desktop__itemOneFourth___2t71A .styles__link___9msaS:nth-child(1)")
}
seller_url.each do |line|
seller_uri = line.attribute("href")
seller_hand = seller_uri[/https:\/\/www.example.com(.*\/([.\w+]+))/i]
seller_handle = seller_hand.gsub("https://www.example.com/", "")
seller = Seller.new
seller.seller_url = seller_uri
seller.seller_handle = seller_handle
seller.save
puts seller_handle
end
puts seller_url.size
sleep 3
What I want is that i continues to load but i want to scrape the last loaded batch minus all the previous batch.
You know how many records are loaded each time you hit the load more button so you can easily access only new records in the seller_url array:
items_per_page = 24
while driver.page_source.include? "Load more"
# ...
seller_url = wait.until {
element = driver.find_elements(:css, ".desktop__itemOneFourth___2t71A .styles__link___9msaS:nth-child(1)")
}
seller_url.last(items_per_page).each do |line|
# do stuff
end
pages_loaded += 1
end

How can I fix this error? Net::ReadTimeout (Net::ReadTimeout)

Actually I realize that it works well for some categories, like
step %{I go to "https://newyork.craigslist.org/search/spa?s=#{emails}"}
but not for others, like
# step %{I go to "https://newyork.craigslist.org/search/fbh?s=#{emails}"}
My function was working well for a few days, then suddenly it started giving out this error: Net::ReadTimeout (Net::ReadTimeout) right when i = 120.
Is there anything I can do to fix this?
Given(/^I go to "([^"]*)"?/) do |url|
visit(url)
end
Given("I save all emails") do
emails = 0
i = 119
until emails >= 500
until i == 120
fetch_emails(i, emails)
i += 1
end
click_next_button
emails += 120
puts emails
i = 1
puts i
end
end
def fetch_emails(i, emails)
find(:xpath, "(//a[#class='result-title hdrlnk'])[#{i}]").click
if Capybara.has_xpath?("//button[#class='reply-button js-only']")
find(:xpath, "//button[#class='reply-button js-only']").click
sleep(1)
if Capybara.has_xpath?("//p[#class='reply-email-address']")
# puts find(:xpath, "//p[#class='reply-email-address']//a").text
open('RESULTS.csv', 'a') do |f|
f << find(:xpath, "//p[#class='reply-email-address']//a").text + "\n"
end
end
end
# step %{I go to "https://newyork.craigslist.org/search/fbh?s=#{emails}"}
step %{I go to "https://newyork.craigslist.org/search/rfh?s=#{emails}"}
# step %{I go to "https://newyork.craigslist.org/search/lab?s=#{emails}"}
# step %{I go to "https://newyork.craigslist.org/search/spa?s=#{emails}"}
# step %{I go to "https://newyork.craigslist.org/search/trd?s=#{emails}"}
end
def click_next_button
first(".next").click
sleep(2)
end
If your chrome is upgrade to latest versions then use below capabilities
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: %w[
headless disable-gpu no-sandbox
--window-size=1980,1080 --enable-features=NetworkService,NetworkServiceInProcess
]
}
)
Capybara::Selenium::Driver.new app, browser: :chrome, desired_capabilities: capabilities

Selenium in ruby/chrome, Selenium wait will not function

No matter what I try to do, the browser tries to run the test too fast before it has a chance to find the element that I am looking for. If I put in a simple "sleep 2", it has a chance for the drop down menu to drop and load and successfully find the element. But I am wanting to learn to use the Selenium Wait command. I have tried numerous combinations of the below and looked all over the web for documentation or perhaps examples. I have found plenty of firefox and people say that some of the things below worked perfectly in firefox, but for me and my project team mates, we can not get any of the waits, implicit or explicit, to pause long enough for it to detect the element. The element does not exist until the drop down menu is fully dropped, then it can detect it. It doesn't take 2 seconds but it seems that none of my wait commands will actually make it wait. Like I said, I have tried numerous different things and almost all of them are below. If anyone can help guide me, i would appreciate it. Here is some of the code I have tried:
def setup
#driver = Selenium::WebDriver.for :chrome
#driver.get "https://website.herokuapp.com/"
#wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
# #driver = Selenium::WebDriver.for:chrome
# #driver.manage.timeouts.implicit_wait = 30
# #wait = Selenium::WebDriver::Wait.new(:timeout => 15)
# #wait = Selenium::WebDriver::Wait.new(:timeout => 10)
# #driver.manage.timeouts.implicit_wait = 10
# #wait = Selenium::WebDriver::Wait.new(timeout: 10)
#driver.manage.window.maximize()
# #driver.navigate.to("https://website.herokuapp.com/")
end
def test_user_name_is_present
login()
#driver.find_element(:class, "navbar-toggle").click()
# user = #driver.find_element(:class, "dropdown-toggle").text
# #wait.until{#driver.find_element(:class, "dropdown-toggle")}
#driver.find_element(:class, "dropdown-toggle")
#wait.until { #driver.find_element(:class => "dropdown-toggle") }
user = #driver.find_element(:class, "dropdown-toggle").text
assert_equal(true, user.include?('HEATHER'), "no user")
end
I'm more familiar with JavaScript or Java bindings.
But what about:
def test_user_name_is_present
login()
#driver.find_element(:class, "navbar-toggle").click()
#wait.until { #driver.find_element(:class => "dropdown-toggle").displayed? }
user = #driver.find_element(:class, "dropdown-toggle").text
assert_equal(true, user.include?('HEATHER'), "no user")
end
Our team uses this, just add to your test_helper.rb if using rails
def wait_for_ajax(sleep_sec = 4)
assert page.has_no_content?(:css, 'body.loading')
sleep sleep_sec if sleep_sec
end

Firefox Webdriver take so long to open on multithread

I Trying to open multiple instance of Firefox browser from my ruby code.
I use selenium 2.53.4 and firefox 47.0.2
The problem is after the thread has been created, the driver not initiate imidiately. it's took so long time until it's opened. And the second driver will opened after the first one is almost finished, this make multithread useless.
Here is my code
require "selenium-webdriver"
th = Array.new
i = 0
limit = 3
while i < 10
if(Thread.list.count <= 3)
th[i] = Thread.new(i){ |index|
start = Time.new
puts "#{index} - Start Initiate at #{start}"
driver = Selenium::WebDriver.for :firefox
finish = Time.new
puts "#{index} - Finish Initiate at #{finish}"
driver.get "http://google.com"
sleep(10)
driver.quit
puts "#{index} - Finished"
}
i = i + 1
puts "Thread - #{i} Created"
end # end if
end # end while
th.each{|t|
if(!t.nil?)
t.join
end
}
Did I code it properly? or it's firefox limitation? or selenium?
Note :
When I remove the thread it's take lesser time (about 6 s until navigate to intended URL)
it's work great using chrome driver

How to take screenshot in selenium webdriver with ruby with date and time included in screenshot name?

I am trying to get a screenshot at every step with the current date and time, but I am getting the error
Error: test_login(Login_page): Argument Error: wrong number of arguments (1 for 0)
The code is
def setup
#driver = Selenium::WebDriver.for :chrome
#driver.manage.window.maximize
#driver.navigate.to "https://www.findmedecor.com"
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
screenshot()
end
def test_login
#driver.find_element(:class,'open-overlay').click
screenshot(DateTime.now)
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
login_email = wait.until {
element = #driver.find_element(:name, "login_email")
element if element.displayed?
}
login_email.send_keys("suwarna.wade#rohagroup.com")
puts "Test Passed: login pop up found" if login_email.displayed?
screenshot(DateTime.now)
#driver.find_element(:id,'pass').send_keys('123456')
#driver.find_element(:id,'btn_login').click
puts "Logged in successfully"
puts "Time of test = ", DateTime.now
screenshot(DateTime.now)
end
$i = DateTime.now
def screenshot
#driver.save_screenshot("screenshot #{'$i'}.png")
$i= +1
end
end
The problem is that Time.now returns a format like '2016-09-28 04:45:40 +0000' which is not a valid filename on Windows. You can just reformat the date/time to something valid like
Time.now.strftime('%Y-%m-%d_%H.%M.%S')
which outputs 2016-09-27_23.33.59 and then put that in your filename.
http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime

Resources