I need some help using multiple browsers in a loop with Watir/Rspec.
My Goal is to:
Go to Google.ca
Quickly search something
Close browser.
Loop steps 1-3 using a different browser.
I can get this to work with using Watir, but do not know how to get this to work with Rspec.
Watir (working code):
require 'watir-webdriver'
require 'rspec'
browsers = [:ff, :chrome]
browsers.map do |x|
$browser = Watir::Browser.new x
$browser.goto('http://www.google.ca')
$browser.text_field(:id, 'gbqfq').set 'Juventus'
$browser.send_keys :enter
$browser.close
end #End loop
Rspec (not working):
require 'watir-webdriver'
require 'rspec'
browsers = [:ff, :chrome]
browsers.map do |x|
$browser = Watir::Browser.new x
$browser.goto('http://www.google.ca')
describe 'loop' do
it 'does something' do
$browser.text_field(:id, 'gbqfq').set 'Juventus'
$browser.send_keys :enter
$browser.close
end
end #End describe
end #End loop
This is what happens with the code above:
Loads Firefox
Goes to Google
Loads Chrome
Goes to Google
Searches on Chrome
It seems when I include Rspec describe the loop does not work as I had intended it to.
Finally figured it out :)
Here is the code for anyone that wants to do multiple browser testing without making different specs for each browser.
require 'watir-webdriver'
require 'rspec'
browsers = [:ff, :chrome]
browsers.map do |x|
describe 'Browser' do
before(:all) do
#browser = Watir::Browser.new x
end
it 'goes to Google.ca' do
#browser.goto('http://www.google.ca')
end
it 'searches' do
#browser.text_field(:id, 'gbqfq').when_present(3).set 'Juventus'
#browser.send_keys :enter
sleep 0.5 #roughly takes 0.5s for the images to load.
end
it 'closes browser' do
#browser.close
end
end #end describe
end #end loop
I think for this to work properly you need to initialize the browser after describe whereas before I was initializing the browser before describe
Related
With the function below, I can receive a print of the last execution of the test, however I want to learn how to receive a print at every step executed by the automation.
How can this be done?
env.rb
# encoding: utf-8
require 'watir'
require 'rspec'
hooks.rb
# coding: utf-8
require 'json'
require 'magic_encoding'
require 'win32console'
require 'watir'
require 'rspec'
browser = Watir::Browser.new
browser.driver.manage.window.maximize
Before do
#browser
#browser = browser
end
After do |_scenario|
browser.screenshot.save 'screenshot.png'
embed 'screenshot.png', 'image/png'
end
login.rb
given("que estou na tela de login") do
#browser.goto "url"
#I want a screenshot of this step
end
There is an AfterStep hook if you want to do an action after each step - eg:
AfterStep do
browser.screenshot.save 'screenshot.png'
end
I'm trying to make testcase using Selenium WebDriver and Ruby. I started learning Ruby a few times ago.
I created the testcase:
require "test/unit"
require "selenium-webdriver"
require "yaml"
thing = YAML.load_file('config.yaml')
puts thing.inspect
class Test < Test::Unit::TestCase
def setup
browser = thing('browser')
#driver = Selenium::WebDriver.for browser
#driver.get 'http://google.com'
#driver.manage.delete_all_cookies
end
def teardown
#driver.close
end
def test_page_search
end
end
I decided to use YAML for config file where I will can change and for WebDriver.
config.yaml:
# Set browser (firefox, ie, chrome, opera)
browser: ":firefox"
# Search query
search_query: "ios testing"
But when I'm running the testcase I'm getting the error:
"test_yaml.rb:11:in `setup'"
You have:
browser = thing('browser')
Did you mean:
browser = thing['browser']
If you're trying to access the browser key, that should take care of it.
Thank you!
I resolved the issue:
require "test/unit"
require "selenium-webdriver"
require "yaml"
class Test < Test::Unit::TestCase
def setup
thing = YAML.load_file('config.yaml')
puts thing.inspect
browser = thing['browser'].to_sym
#driver = Selenium::WebDriver.for browser
#driver.get 'http://google.com'
#driver.manage.delete_all_cookies
end
def teardown
# #driver.close
end
def test_page_search
end
end
I have 66 watir scripts that I have been creating over the past week to automate testing on the clients website.
However I have recently found out about a test framework called MiniTest which I am trying to implement now.
The reason I have set the URL as a variable is because there are 5 different sites that these tests need to run on so when they want me to run my pack on a different website I just need to update that 1 variable and not in each individual test.
require 'minitest/autorun'
require "watir-webdriver"
class MPTEST < MiniTest::Unit::TestCase
def setup()
url = "http://thewebsite.com/"
$browser = Watir::Browser.new :chrome
$browser.goto url
end
def test_myTestCase
$browser.link(:text, "Submit your CV").click
sleep(2)
$browser.button(:value,"Submit").click
assert($browser.label.text.includes?("This field is required"))
def teardown
$browser.close
end
end
When running that I receive the following output:
NameError: undefined local variable or method 'browser' for #<MPTEST:0x4cc72f8>c:/directory stuff...
Any ideas?
EDIT I have browser working however now there is an issue with my assert:
New code:
require 'minitest/autorun'
require "watir-webdriver"
class MPTEST < MiniTest::Unit::TestCase
def setup()
url ="http://thewebsite.com"
$browser = Watir::Browser.new :chrome
$browser.goto url
end
def test_myTestCase
$browser.link(:text, "Submit your CV").click
sleep(2)
$browser.button(:value,"Submit").click
assert($browser.label.text.includes?("This field is required"))
end
def teardown
$browser.close
end
end
And the error is:
NoMEthodError: undefined method 'includes?' for "":String
it seems to me you can you use #browser instead of $browser (but the problem might be not in this code)
The exception
NoMEthodError: undefined method 'includes?' for "":String
Is due to strings, in this case the value returned by $browser.label.text do not have an includes? method.
The method you actually want is include? (no plural):
assert($browser.label.text.include?("This field is required"))
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.
Can any one have more descriptive example of Rspec? I used the the example from site https://gist.github.com/1053934 works very fine. But i want to learn more like go to site X (eg. Google) -> enter data in text field (eg. Ruby)
Check for the HTML is Ruby available in the list?
Can any one have script that i can run on Rspec and that will generate report on that pass fail status of case.
I appreciate all the responses in advance.
Here's a complete example, courtesy of Watir docs.
require "rubygems"
require "rspec"
describe "google.com" do
let(:browser) { #browser ||= Watir::Browser.new :firefox }
before { browser.goto "http://google.com" }
after { browser.close }
it "should search for watir" do
browser.text_field(:name => "q").set "watir"
browser.button.click
browser.div(:id => "resultStats").wait_until_present
browser.title.should == "watir - Google Search"
end
end