I'm running automation tests with watir-webdriver.
I am not able to pass command line arguments ARGV through test unit, I get an ArgumentError.
require 'rubygems'
require 'watir-webdriver'
require 'test/unit'
ARGV.each do |arg|
if arg.downcase.include? 'chrome'
$browser = 'chrome'
elsif arg.downcase.include? 'firefox'
$browser = 'firefox'
elsif arg.downcase.include? 'ff'
$browser = 'firefox'
elsif arg.downcase.include? 'ie'
$browser = 'ie'
end
end
class TEST_SITE < Test::Unit::TestCase
def setup
if $browser == 'chrome'
$b = Watir::Browser.new :chrome
elsif $browser == 'firefox'
$b = Watir::Browser.new :ff
elsif $browser == 'ie'
$b = Watir::Browser.new :ie
end
end
end
Is there another option or somehow override the test unit class?
Test/Unit seems to have logic around how it is handling the values in ARGV, though not exactly sure what values it is checking for. However, if you make your arguments more parameter like, they get ignored by Test/Unit and your tests should run.
Try running the following from command line (you should not need to change your code):
ruby filename.rb -browser=ff
Related
I'm working on integrating allure into my tests but have come across a problem with the first test suite failing and returning a undefined method `find_element' for nil:NilClass error with every test in the suite. The tests run as expected when I'm not using allure and the same thing happens regardless of what suite is run.
I've had a look into it and it seems the before and after hooks in my spec helper aren't being used in the failed tests. Any ideas what could be causing this?
require 'rspec'
require 'selenium-webdriver'
require 'allure-rspec'
require 'nokogiri'
require 'uuid'
require 'pathname'
RSpec.configure do |c|
c.include AllureRSpec::Adaptor
c.expect_with :rspec do |c|
### Enable both should and expect syntax ###
c.syntax = [:should, :expect]
end
c.before(:all) do
#Start the Phantom-js driver before running any headless tests
case ENV['browser']
when 'headless'
## Run in command line before a headless test: phantomjs --webdriver=8001
system('start cmd /k phantomjs --webdriver=8001')
sleep 3
end
end
c.before(:each) do
#Find the browser to the used and set the driver to the appropriate one
case ENV['browser']
when 'chrome'
#driver = Selenium::WebDriver.for :chrome
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :chrome)
when 'ie'
#driver = Selenium::WebDriver.for :internet_explorer
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :ie)
when 'firefox'
#driver = Selenium::WebDriver.for :firefox
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :firefox)
when 'headless'
#driver = Selenium::WebDriver.for :remote, url: 'http://localhost:8001'
end
#Maximize the browser
#driver.manage.window.maximize
#driver.get ENV['base_url']
end
c.after(:each) do |c|
#Takes a screen shot if an exception is thrown and attaches it to the allure XML when running Rake tests
if c.exception != nil
$failure = true
c.attach_file("screenshot", File.new(#driver.save_screenshot(File.join(Dir.pwd, "test_reports/allure/#{UUID.new.generate}.png"))))
end
#driver.quit
end
end
AllureRSpec.configure do |c|
#Outputs the Allure XML
c.output_dir = "test_reports/allure"
end
beginner is here with a simple question.
Using Ruby with Selenium web-driver. Just wanted to make my life easier to make a reusable class that will return me a different webdrivers. Tried the below, but it did not work when I tried to call.
def Sel_drive
def ff_drive
return Selenium::WebDriver.for :firefox
end
def ie_drive
return Selenium::WebDriver.for :ie
end
def chrome_drive
return Selenium::WebDriver.for :chrome
end
def name_of_browser(browser_name)
if browser_name == 'Firefox'
driver = ff_drive
end
elsif browser_name == 'IE'
driver = ie_drive
else
browser_name == 'Chrome'
driver = chrome_drive
end
You can use case to return the correct webdriver.
def Sel_drive(drive_type)
case drive_type
when "firefox"
return Selenium::WebDriver.for :firefox
when "ie"
return Selenium::WebDriver.for :ie
when "chrome"
return Selenium::WebDriver.for :chrome
else
puts "Not a valid driver"
end
end
driver = Sel_drive("firefox")
I get this error on running my feature file.
Unable to pick a platform for the provided browser (RuntimeError)
Help required, please.
Here is the code;
class GooglePage
include PageObject
def self.visitor
visit("http://www.google.com")
end
end
env.rb
require 'selenium-webdriver'
require 'page-object'
require 'rubygems'
require 'page-object/page_factory'
World (PageObject::PageFactory)
#browser = Selenium::WebDriver.for :firefox
Step-Definitions
require_relative 'GooglePage'
Given(/^I am on the Google home page$/) do
visit(GooglePage)
# visit('http://www.google.com')
on(GooglePage).visitor
end
This won't work:
visit(GooglePage)
because you haven't called page_url in GooglePage (ln 4 below)
class GooglePage
include PageObject
page_url "http://www.google.com"
def self.visitor
visit("http://www.google.com")
end
end
Move the line #browser = Selenium::WebDriver.for :firefox to the Before method in hooks.rb
Before do
#browser = Selenium::WebDriver.for :firefox
end
After do
#browser.close
end
what if you make your rake file pass on tags and BROWSER and URL,e.g.
rake my_task BROWSER=chrome URL=http://google.com.au
and hooks will check if the BROWSER is chrome, then will use the specific Webdriver for chrome. But how to pass the URL in the PageObject?
Currently from hooks I have the ff:
when "chrome" then
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps.version = "40.0.2214.115 m"
caps.native_events = false
caps.javascript_enabled= true
# This is for increasing the default timeout to 180
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180
browser = Selenium::WebDriver.for :chrome
browser.manage.window.maximize
end
Before do
browser.cookies.clear
#browser = browser
end
After do
unless ENV["BROWSER"].eql? "ie"
browser.close
sleep 2
end
end
Then on my HomePage pageobject I have the ff:
class HomePage
include PageObject
page_url("#{ENV['URL']}")
#opens the url defined in ENV['URL']
def goto_homepage
visit(HomePage)
end
end
Using Watir, the URL got opened, so trying this out in Selenium and it won't work.
I am new to Ruby and currently working on a JAVA project that has its behavioural test cases written in ruby(CUCUMBER BDD).When I tried to take a build of my project through TeamCity it shows 6 test case failures with
"Watir::Exception::NoMatchingWindowFoundException: Unable to locate window,"
or
"Spec::Expectations::ExpectationNotMetError:"
errors.No changes have been made to these test cases .Any suggestions as to where I should look ?
THis is my env.rb file ..do I need to change anything here .
require 'watir'
require 'open-uri'
require 'spec'
require 'win32ole'
require 'rake'
require 'cucumber/rake/task'
require 'net/http'
require 'uri'
require 'mysql'
require 'active_record'
require 'features/db/mysqldb'
require 'features/db/order'
require 'features/db/listener_state'
def clear_all_mock_requests
open 'http://xxxxxx:8080/httpmockserver/soapResponse!clearAllRequests.action'
end
Mysqldb.connect("xxx", "xxx", "xx", "xxx", "xxxxx")
clear_all_mock_requests()
Watir::Browser.default = ENV['browser'] == 'firefox' ? 'firefox' : 'ie'
#Watir::Browser.default = 'firefox'
BROWSER = Watir::Browser.new
WIN32OLE.class_eval do ||
def visible?;
return false if self.style.invoke('display').downcase == 'none';
return true;
end
def type
{:text_field=>'text', :radio=>'radio', :select_list => 'select-one', :checkbox => 'checkbox'}.each do |method, name|
return name if BROWSER.method(method).call(:name, self.name).exist?
end
return nil
end
def exist?
return true
end
end
NilClass.class_eval do ||
def exist?
return false
end
end
at_exit do
BROWSER.close
end
Watir::Exception::NoMatchingWindowFoundException: Unable to locate window usually means that you're trying to latch onto a browser that does not exist. Check your browser.open functions.
I am using webdriver-user-agent mentioned here – http://watirwebdriver.com/mobile-devices/
This is the code I am using when trying out this gem
Browser: FF/Chrome
Ruby: 1.9.3 / Selenium :2.30.0 / Watir : 4.0.2
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = HTTP_TIMEOUT
profile = Selenium::WebDriver::Firefox::Profile.new
device = ENV["DEVICE"]
orientation = ENV["ORIENTATION"]
driver = UserAgent.driver(:browser => :firefox, :agent =>device, :orientation=>orientation)
devices = UserAgent.resolution_for(device,orientation)
UserAgent.resize_inner_window(driver,devices[0],devices[1])
Watir::Browser.new driver
Now when the last statement is executed, i get the following error
(STEP) Launching FIREFOX (using web driver user agent)……
browser:
#
undefined method `to_sym’ for # (NoMethodError)
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:42:in `load_driver_for’
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:8:in `new’
Based on some investigation, problem is happening at highlighted line below as its trying to .to_sym on the selenium webdriver object.
def load_driver_for(browser)
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
But if we add a line like given below, this gem is working as expected.
def load_driver_for(browser)
if “#{ENV["BROWSER"]}”.eql?(“chrome_useragent”)||”#{ENV["BROWSER"]}”.eql?(“firefox_useragent”)
Watir.driver = :webdriver
else
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
end
since this is watir code outside of our framework, this is not the right way to do this, any suggestion on how to avoid this situation ?
The problem is reproducible when you do:
require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
browser = Watir::Browser.new driver
browser.goto 'tiffany.com'
browser.url.should == 'http://m.tiffany.com/International.aspx'
You can fix the issue by requiring watir-webdriver directly instead of through the watir metagem. Change the first line to:
require 'watir-webdriver'