Nomethoderror on watir webdriver ruby - ruby

i am new to Watir. When I run this file in terminal, ruby TestJoinChange.rb, I get NoMethodError
require 'rubygems'
require 'test/unit'
require 'watir-webdriver'
browser = Watir::Browser.new
class TestJoinChange < Test::TestCase
def test_join(logintype,usr,pwd)
# open browser to page
#browser.goto 'http://change.com'
end
end
How do I pass parameter values? Why my function is returning no method error?

"You have defined subroutine now you can pass values when you call that subroutine by making an object of the class."
your code:
require 'rubygems'
require 'test/unit'
require 'watir-webdriver'
browser = Watir::Browser.new
class TestJoinChange < Test::TestCase
def test_join(logintype,usr,pwd)
# open browser to page
#browser.goto 'http://change.com'
end
end
now just create an object for your class:
obj = TestJoinChange.new
# it will create object for your class and now call the subroutine and pass any value in that#
obj.test_join("logintype_value", "user", "password")
Now you can use the values in your class by storing all passed values in to variables.

Related

if i remove the class name from the code and call the method direcly it will run, but using class shows me an error

If i remove the class name from the code and call the method direcly it will run, but using class shows me an error
require 'rubygems'
require 'selenium-webdriver'
require 'test/unit'
# define in setup
def setup
#driver = Selenium::WebDriver.for:firefox
#driver.navigate.to "#{#base_url}"
navigate_to_signin.click
end
class LoginValiation < Test::Unit::TestCase
def test_login_blank_validation
setup
signin_button.click
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
expected = #driver.find_element(:xpath, ".//*[#id='login_form']/div[1]").text
assert_equal("Login and/or password are wrong.", expected)
teardown
end
# Define in page objects
def signin_button
#driver.find_element(:name, "submit")
end
end
output
Error:
test_login_blank_validation(LoginValiation):
NoMethodError: undefined method `find_element' for nil:NilClass
C:/Users/Kuntal.Sugandhi/Desktop/rubyproject/ruby practice/test/login_page.rb:23:in ` signin_button'
test_login_validation.rb:19:in `test_login_blank_validation'
On removing the class and run the test directly
output
test_login_validation.rb:19:in `test_login_blank_validation': undefined method ` assert_equal' for main:Object (NoMethodError

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

YAML + Ruby usage

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

Watir / MiniTest - Undefined local variable or method 'browser'

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"))

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