Fail to use statement IF in Selenium - ruby

Currently I'm trying use a statement IF (If a button appears in the page, then run the IF), see the method Login in the system:
If the button doesn't appear in the page, I would like to run the next method Remove and add new expense
require "selenium-webdriver"
require "rspec"
require "rspec/expectations"
describe "#Add simple expense and after add a receipt", :suitee => true do
before(:all) do
#driver = Selenium::WebDriver.for :chrome
#base_url = "http://sitetest.com"
#driver.manage.window.maximize
end
it "Login in the system" do
#driver.get(#base_url)
#driver.find_element(:id, "user_email").send_keys "rafael#gmail.com"
#driver.find_element(:id, "user_password").send_keys "123456"
#driver.find_element(:name, "commit").click
if(#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed?)
#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").click
#driver.find_element(:css, ".introjs-button.introjs-skipbutton").click
#driver.find_element(:css, ".i.i-pencil").click
end
end
it "Remove and add new expense" do
begin
while(#driver.find_element(:css, ".i.i-pencil.icon").displayed?)
button = #driver.find_element(:id, "expense-bulk-select")
#driver.action.click(button).perform
delete = #driver.find_element(:id, "delete-multi-btn")
#driver.action.click(delete).perform
waitDisplayModal = Selenium::WebDriver::Wait.new(:timeout => 10)
waitDisplayModal.until {#driver.find_element(:class => "bootstrap-dialog-footer-buttons")}
#driver.find_element(:xpath, "//div[3]/div/div/button[2]").click
sleep 3
end
rescue Selenium::WebDriver::Error::NoSuchElementError
#driver.find_element(:id, "current_expense_merchant").send_keys "Taxi to work"
#driver.find_element(:id, "current_expense_amount").send_keys "50"
#driver.find_element(:id, "button-add-expense").click
waitDisplayIconTrash = Selenium::WebDriver::Wait.new(:timeout => 20)
waitDisplayIconTrash.until {#driver.find_element(:css => ".i.i-pencil.icon")}
end
end
after(:all) do
#driver.quit
end
end
My problem: When I run this script, appears this in my console:
Failure/Error: if(#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed?)
Selenium::WebDriver::Error::NoSuchElementError:
no such element
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-24-generic x86_64)
That is, the IF is not working as I would like. How can I fix it?
CHeers

https://code.google.com/p/selenium/wiki/RubyBindings
Use the Wait class to explicitly wait for some condition:
wait = Selenium::WebDriver::Wait.new(:timeout => 3)
wait.until { driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed? }
....

Related

How can I select multiple options from a select list in ruby using selenium webdriver

I want to select multiple options.
Then I want to verify it whether it is selected or not?
Here is my code:
require 'selenium-webdriver'
class Ques7
def test_multiple_select
driver = Selenium::WebDriver.for :firefox
driver.get(url)
driver.find_element(:xpath, './/*[#id="select_1"]').click
puts driver.find_element(:xpath, './/*[#id="select_1"]').selected?
driver.find_element(:xpath, './/*[#id="select_2"]').click
driver.find_element(:xpath, './/*[#id="select_4"]').click
puts driver.find_element(:xpath =>'.//*[#id="select_1"]',
:xpath => './/*[#id="select_2"]',:xpath => './/*[#id="select_4"]').selected?
end
end
obj = Ques7.new
obj.test_multiple_select

Selenium Does Click on Firefox but PhantomJS doesn't (Ruby)

i'm really new to Selenium WebDriver. I write a simple code for out of curiousity. I made an spam bot to open a page, shuffle in online users list and click everyone of them in order. Send some message, close the new window and repeat.
I made this work on Selenium Firefox driver. It seems work good. But i want to do it silent, not opening firefox everytime. So i found out i can do that by PhantomJS.
Here is my working code for firefox:
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :firefox
#reklam = 'Some testing message.'
end
def run
setup
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
x.click
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
But when i switch Webdriver from Firefox to PhantomJS, x.click method doesn't work. I made some tests and program doesnt go further from click method. After a while program ends with (Net::ReadTimeout) error.
Here is my last try to work it proper;
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :phantomjs
#reklam = 'http://peyloride.com siteme beklerim.'
end
def teardown
#driver.quit
end
def run
setup
#driver.manage.window.resize_to(1920, 1080)
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
puts "Shuffleandı"
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
puts "click'e geldik"
#driver.save_screenshot "phantomjs.png"
x.click
#driver.save_screenshot "phantomjs2.png"
puts "click yaptı sonunda aq"
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
As you can see i tried to take a screenshot to understand the problem but screenshot seems really fine. Here it is:
EDIT: Versions:
ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
gem list
...
selenium-webdriver (2.48.1)
...
phantomjs -v
1.9.0

Watir open multiple browser's or tab's

How can I open more than one browser using my code-watir, for example via a while loop from 0 to 10?
Here is my code:
require 'watir-webdriver'
require 'headless'
class Page
#headless = Headless.new
#headless.start
#browser = Watir::Browser.start 'bit.ly/***'
def self.get_connection
puts "Browser started"
puts #browser.title
#browser.driver.manage.timeouts.implicit_wait = 3 #3 seconds
#browser.select_list(:name => 'ctl00$tresc$111').select_value('6')
puts "Selected country"
#browser.select_list(:name => 'ctl00$tresc$222').wait_until_present
#browser.select_list(:name => 'ctl00$tresc$333').select_value('95')
puts "Selected city"
end
def self.close_connection
#browser.close
#headless.destroy
end
end
Page.get_connection
Page.close_connection
But how to do something like this?
while i < 10
Page.get_connection
end
This should open ten browsers:
10.times {Watir::Browser.new}
If you want to use the browsers later, you can put them in a hash:
browsers = {}
(0..9).each {|i| browsers[i] = Watir::Browser.new}
browsers[0].goto "google.com"
browsers[1].goto "yahoo.com"

Importing/Reading parameters in Ruby Test Unit

Please please help me before I lose it!
I'm using Ruby Test Unit and Selenium Webdriver. All of my tests start with a login function, therefore I want the tests to pull the username and password values from a central script/csv/text file, whatever is easiest. This would make it easier to control login details from one place rather than hard coding them into every script. I am not a Ruby programmer and therefore I am not finding this particularly easy. I'm thinking I should be able to declare the username and password parameters in the 'def setup' section, so that it reads the values from an external source.
Is this possible?
require "selenium-webdriver"
require "test/unit"
class CMSNewslist < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://skyintranet/"
#driver.manage.timeouts.implicit_wait = 90
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_c_m_s_newslist
#driver.get(#base_url + "/Enterprise-Applications-Test/")
#driver.find_element(:link, "Login").click
#driver.find_element(:id, "ctl00_MainRegion_LoginView_LoginControl_UserName").clear
#driver.find_element(:id, "ctl00_MainRegion_LoginView_LoginControl_UserName").send_keys "Ruband"
#driver.find_element(:id, "ctl00_MainRegion_LoginView_LoginControl_Password").clear
#driver.find_element(:id, "ctl00_MainRegion_LoginView_LoginControl_Password").send_keys "Donn1982"
#driver.find_element(:id, "ctl00_MainRegion_LoginView_LoginControl_LoginBtn").click
assert_equal "Logout", #driver.find_element(:class, "loginButton").text, "Login Unsuccessful"
puts "Login Successful"
Many Thanks.
You can put them in a separate file and read it or have a mixin that gets included in each script.
module LoginConfig
##username = 'foo'
##password = 'bar'
end
Then within your class CMSNewslist:
...
require 'login_config'
class CMSNewslist < Test::Unit::TestCase
include LoginConfig
...
Then you can use ##username and ##password. Here's a decent tutorial on Mixins...
http://www.tutorialspoint.com/ruby/ruby_modules.htm

Rescue for ScriptError in Selenium Webdriver?

I have a written a Code in Selenium Web driver to test the health of the Contact Us page of my web site.
Code is working fine. My further aim is to:
Use Rescue (to get error message when this scripts fail. Specifically when some element is not found during execution in the function > "def test_contact_us_screen_shot").
Code for reference.
require 'rubygems'
require "selenium-webdriver"
require "test/unit"
class ContactUsScreenShot < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://www.mycompany.com"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_contact_us_screen_shot
#driver.get(#base_url + "/contact_us")
#This method will take screenshot and save it in the same folder from where script is executed.
#driver.save_screenshot("./screen1.png")
puts "screen shot taken"
assert_equal "mycompany.com - Discuss your requirements to Outsource Software Product Development, Offshore Software Testing,", #driver.title
#driver.find_element(:id, "compan").clear
#driver.find_element(:id, "company").send_keys "My Company"
#driver.find_element(:id, "first_name").clear
#driver.find_element(:id, "first_name").send_keys "Mrityunjay"
#driver.find_element(:id, "last_name").clear
#driver.find_element(:id, "last_name").send_keys "Chauhan"
#driver.find_element(:id, "email").clear
#driver.find_element(:id, "email").send_keys "mrityunjay#gmail.com"
#driver.find_element(:id, "phone1").clear
#driver.find_element(:id, "phone1").send_keys "9999999999"
#driver.find_element(:id, "addr1").clear
#driver.find_element(:id, "addr1").send_keys "306 Office Address"
#driver.find_element(:id, "addr2").clear
#driver.find_element(:id, "addr2").send_keys "Office Address"
#driver.find_element(:id, "city").clear
#driver.find_element(:id, "city").send_keys "Dehradun"
#driver.find_element(:id, "state").clear
#driver.find_element(:id, "state").send_keys "UK"
Selenium::WebDriver::Support::Select.new(#driver.find_element(:id, "country")).select_by(:text, "India")
#driver.find_element(:id, "zip").clear
#driver.find_element(:id, "zip").send_keys "248001"
#driver.find_element(:id, "website").clear
#driver.find_element(:id, "website").send_keys "http://www.mycompany.com"
#driver.find_element(:id, "comments").clear
#driver.find_element(:id, "comments").send_keys "This is the Testing Script for Contact Us page. Please Ignore.\nThanks,\nMrityunjay Chauhan"
#driver.find_element(:name, "submit").click
assert_equal "mycompany.com - Discuss your requirements to Outsource Software Product Development, Offshore Software Testing,", #driver.title
begin
rescue Exception => e
puts e.message
#driver.save_screenshot("./error1.png")
end
puts "I am Here"
end
def element_present?(how, what)
#driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
#verification_errors << ex
end
end
Here is an answer -- just use "find_elements" instead which gives an empty array instead of throwing an exception.
Do you mean something like this
begin
#driver.find_element(:name, "submit")
rescue
#driver.save_screenshot("Submit_button_not_present.png")
assert_match "true","false","Your error message"
end
Following Rescue code is working fine in my code...
rescue Exception => e
puts e.message
puts "Element Not Found"
#driver.save_screenshot("./Test-Results/Contact_Us_Error.png")
puts "I am Here"
end #rescue
In rescue we can trace the errors by using Log. For this we have to generated log file. Code is given below:
First define log at the start of your code as:
# Here I have created a log file in my "Test-Result" folder
$log = Logger.new('./Test-Results/logfile.log')
Next, we will move the error message to our log file using code given below:
rescue Exception => e
$log.error "This is the Error occurred when Element/s not found: #{e}"
#driver.save_screenshot("./Test-Results/Contact_Us_Error.png")
puts "I am Here"
end #rescue
Log will be something like this:
E, [2012-06-04T16:03:44.546875 #796] ERROR -- : Error in Elements when not found!: Unable to locate element: {"method":"id","selector":"compan"}

Resources