YAML + Ruby usage - ruby

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

Related

How do I reference the Capybara driver for creating my pageobjects/specs.

I am attempting to use pageobjects along with with my Capybara specs but can't seem to properly reference the driver. Basically I want to be able to use the PageObjects to define the fields on the page (this is login_page.rb), but when I try to create the object in the spec, it is throwing errors with saying that the object is nil.
spec_helper.rb:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'page-object'
# loading page object files
page_paths = File.join(Dir.pwd, 'spec', 'pages', '**', '*.rb')
puts 'foo'
Dir.glob(page_paths).each { |file| puts file}
Dir.glob(page_paths).each { |file| require file }
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.app_host = *********** #redacted
Capybara.default_max_wait_time = 5
RSpec.configure do |config|
config.before(:all) do
#browser = Capybara::Selenium::Driver
end
config.before(:each) do
config.include Capybara::DSL
end
end
login_page.rb
class LoginPage
include Capybara::DSL
include PageObject
text_field(:username, id: 'username')
text_field(:password, id: 'password')
button(:login, id: 'loginButton')
def initialize(driver)
#driver = driver
end
end
login_spec.rb
require 'spec_helper'
describe 'On Login page' do
context 'using proper password' do
before(:each) do
visit('/')
end
it 'logs in as foo' do
login_page = LoginPage.new(#browser)
login_page.username = 'foo'
login_page.password = 'bar'
login_page.login
end
end
end
Assuming you're talking about the page-object gem - https://github.com/cheezy/page-object - it doesn't support Capybara, it supports watir-webdriver/watir and selenium-webdriver. Additionally Capybara::Selenium::Driver is a class not an object instance. As shown in the page-object readme you need to pass an object instance into your page objects constructor
#browser = Selenium::WebDriver.for :firefox
If you want a page object framework that supports Capybara you may want to look at something like site-prism instead.

Run some functionality before all test suites in minitest

This is my code:
class LoginTest < MiniTest::Test
def before_all
Selenium::WebDriver::Chrome.driver_path = DRIVER_PATH
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
#driver = Selenium::WebDriver.for :chrome, options: options
end
def test_one
end
def test_two
end
end
I want to call the driver method before all test suites.
Is there a way to run some code once before all suites?
Probably a bit late, and you probably already have find your answer,
but here is the solution
class LoginTest < MiniTest::Test
setup do
Selenium::WebDriver::Chrome.driver_path = DRIVER_PATH
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
#driver = Selenium::WebDriver.for :chrome, options: options
end
def test_one
end
def test_two
end
end
the code present in the function setup will be run before each test

Error while executing a method in Ruby using Selenium WebDriver

I am getting the following error when I execute the below code.
Please let me know the way to execute the method 'startup' in Eclipse.
Error Message:
': undefined local variable or method ***startup***' for PageObjects:Class (NameError) from C:/Technical/RubyTraining/PageObjects.rb:1:in'
Code:
class PageObjects
require 'selenium-webdriver'
require 'page-object'
def startup
#browser = Selenium::WebDriver.for :firefox
#browser.manage.window.maximize
end
startup
end
From the code you have provided, you don't need the class PageObjects (If you have more code, please post them all at once). So you might simply try this:
require 'selenium-webdriver'
# require 'page-object' # You are not using page-object either.
def startup
#browser = Selenium::WebDriver.for :firefox
#browser.manage.window.maximize
end
startup
However, if you want to use this class, you need to call this instance method setup outside your class after initializing the class. Like this:
class PageObjects
require 'selenium-webdriver'
require 'page-object'
def startup
#browser = Selenium::WebDriver.for :firefox
#browser.manage.window.maximize
end
end
PageObjects.new.startup

Ruby Page object Gem - Unable to pick a platform for the provided browser (RuntimeError)

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.

Create a ruby object from a supertype object

I am using the Selenium Webdriver libraries in Ruby. A typical piece of code looks like this:
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
# driver is an instance of Selenium::WebDriver::Driver
url = 'http://www.google.com/'
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
driver.get(url)
wait.until { driver.title.start_with? "Google" }
I would like to create a subclass of Selenium::WebDriver::Driver called Selenium::WebDriver::Driver::MyClass that will contain some new methods and instance variables.
As the above code illustrates, the way that instances of Selenium::WebDriver::Driver are created is with Selenium::WebDriver.for.
Without wholesale copying of code, how can I create a version of Selenium::WebDriver.for that does the same thing as Selenium::WebDriver.for but creates instances of Selenium::WebDriver::Driver::MyClass?
Why not just override the Selenium::WebDriver.for ? let me show you that my an example
# selenium code
module Selenium
class WebDriver
def self.for
puts "creating oldclass"
end
end
end
# your code
class Selenium::WebDriver
def self.for
puts "creating myclass"
end
end
Selenium::WebDriver.for
output:
creating myclass
Safe alternative is to derive class from Selenium::WebDriver and use that in your code, or to the extreme you can just open Driver class and add your behavior to it.
Check the source code. Selenium::WebDriver.for simply delegate the method call to Selenium::WebDriver::Driver.for.
If you don't have listener attached, you can simple create your own bridge MyClass::Bridge.new and then pass that to Selenium::WebDriver::Driver.new.
If you insist override the for method, here is some code snippet that might help.
module Selenium
module WebDriver
class Driver
class << self
alias_method :old_for, :for
def for(browser, opts = {})
if browser == :myclass
# create your MyClass::Bridge instance and pass that to new()
else
old_for(browser, opts)
end
end
end
end
end
end
If you just want to define some extra methods on your driver, you do not need to override WebDriver.for.
The following worked well for me:
First, in file customdriver.rb
require 'selenium-webdriver'
class CustomDriver < Selenium::WebDriver::Driver
#a custom method..
def click_on (_id)
element = find_element :id => _id
element.click
end
#add other custom methods here
#....
end
Then, in file main.rb
require-relative 'customdriver'
driver = CustomDriver.for :chrome
driver.click_on("buttonID")
Regards,

Resources