Unable to verify expectations in a ruby class - ruby

I have a Class definition as below:
def initialize(driver)
#driver = driver
end
def verify_page(username)
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
btnSettings = wait.until { #driver.find_element(:xpath => "//a[#href='/UserManagement/MyAccount']") }
expect(btnSettings.text).to eq('Portal Administrator' + ' -- ' + username)
end
end
When I run the code that calls the 'verify' method of this class, I get the error:
undefined method `expect' for #<PortalHomePage:0x00000003b1d188>
How do i get past this problem?

In order to use rspec expectations.
Please do the following in your env.rb file
require 'rspec'
include RSpec::Matchers
This will solve your current problem
Comment for more information.
Hope it helps!!

Related

Trying to learn to use PageObjects with Ruby - getting error "uninitialized constant Site (NameError)"

I have some experience of Selenium in Python and Cucumber/Watir/RSpec in Ruby, and can write scripts that execute successfully, but they aren't using classes, so I am trying to learn more about classes and splitting the scripts up in to pageobejcts.
I found this example to learn from: http://watir.com/guides/page-objects/ so copied the script and made some minor edits as you'll see below.
I'm using SublimeText 3.x with Ruby 2.4.x on Win10, so you know what tools I'm using.
I put the whole script in to a single .rb file (the only differences are that I replaced the URL and the elements to enter the username and password) and tried to execute it and get the following error:
C:/selenium/ruby/lotw/lotwlogin.rb:3:in `<main>': uninitialized constant Site (NameError).
I added the top line (required 'watir') line and it made no difference to the error encountered.
So I have in lotwlogin.rb essentilly the structure and syntax of the original script with custom elements. However, the core structure is reporting an error and I don't know what to do about it.
Here is my script:
require 'watir'
site = Site.new(Watir::Browser.new :chrome) # was :firefox but that no longer works since FF63
login_page = site.login_page.open
user_page = login_page.login_as "testuser", "testpassword" # dummy user and password for now
user_page.should be_logged_in
class BrowserContainer
def initialize(browser)
#browser = browser
end
end
class Site < BrowserContainer
def login_page
#login_page = LoginPage.new(#browser)
end
def user_page
#user_page = UserPage.new(#browser)
end
def close
#browser.close
end
end
class LoginPage < BrowserContainer
URL = "https://lotw.arrl.org/lotw/login"
def open
#browser.goto URL
##browser.window.maximize
self # no idea what this is for
end
def login_as(user, pass)
user_field.set user
password_field.set pass
login_button.click
next_page = UserPage.new(#browser)
Watir::Wait.until { next_page.loaded? }
next_page
end
private
def user_field
#browser.text_field(:name => "login")
end
def password_field
#browser.text_field(:name => "password")
end
def login_button
#browser.button(:value => "Log On")
end
end # LoginPage
class UserPage < BrowserContainer
def logged_in?
logged_in_element.exists?
end
def loaded?
#browser.h3 == "Welcome to Your Logbook of the World User Account Home Page"
end
private
def logged_in_element
#browser.div(:text => "Log off")
end
end # UserPage
Any assistance how to not get the Site error would be appreciated.
Thanks
Mike
You define class Site only a few lines below. But at that point, it's not yet known.
Move this logic to after all class definitions:
site = Site.new(Watir::Browser.new :chrome) # was :firefox but that no longer works since FF63
login_page = site.login_page.open
user_page = login_page.login_as "testuser", "testpassword" # dummy user and password for now
user_page.should be_logged_in

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

not able to find installed Gem in ruby program

hi getting error which is not understand able i am new to ruby so please help .
i checked all thing which is possible for me.
require 'rubygems'
require 'selenium-webdriver'
require 'test/unit'
class SeleniumTest < Test::Unit::TestCase
driver = Selenium::WebDriver.for :firefox
driver.get "http://localhost:9000/assets/build/index.html#/login"
element = driver.find_element :name => "email"
element.send_keys "kaushik#abc.com"
element = driver.find_element :name => "password"
element.send_keys "password"
element.submit
page.find(:xpath, "//a[#href='#/courses/new']").click
#click_link ("//a[#href='#/courses/new']")
puts "Page title is #{driver.title}"
#page.should have_selector(:link_or_button, ' Create New Course...')
wait = Selenium::WebDriver::Wait.new(:timeout => 2000)
driver.quit
end
getting This error:-
TestClass.rb:7:in `<class:SeleniumTest>': undefined local variable or method `logger' for SeleniumTest:Class (NameError)
from TestClass.rb:6:in `<main>'
It seems that you didn't included the complete source code.
Besides that, all you code is bare in class SeleniumTest. You should put your code into the appropriate method or methods.
This type of errors are generated when objects or methods are not created or not scoped well.
In your case, the error message is telling you that the object logger in line 7 of you script does not exist.
As I can see from your source code, line 7 falls into the class definition. I guess you have something like
logger.log 'logging text'
in that line but you delete it from your post, and in lines 4 and 5 you have something like:
require 'logger'
logger = Logger.new('file.log')
If that is the case, you could put logger = Logger.new('file.log') inside the class definition, or define an instance object of type Logger inside the SeleniumClass class, or a global method or something other for logging messages.
Examples:
class SeleniumTest < Test::Unit::TestCase
logger = Logger.new('file.log')
logger.log "logging text"
...
end
or
class SeleniumTest < Test::Unit::TestCase
def initialize
#logger = Logger.new('file.log')
end
def log(message)
#logger.log mesage
end
...
def some_method_with_your_code
...
log "logging text"
...
end
end
st = SeleniumTest.new
st.some_method_with_your_code
... or something similar...
I hope this can help you solve your problem.
If not, you should put the complete source code and tell us what are you trying to do!

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

ruby: unable to use logger with rspec

I am trying to add logging to a simple rspec test. I am using Watir to drive Chrome within the spec and this works fine. I am unable to get logs using the "Logger" library.
This is my spec:
require 'rubygems'
require 'watir-webdriver'
require 'rspec'
require 'logger'
describe 'ui tests' do
let(:browser) { browser ||= Watir::Browser.new :chrome }
let(:log) {
log = Logger.new(STDOUT)
log = Logger.new('watir.tests.log', 'daily')
log.level = Logger::DEBUG
}
before {
browser.goto 'http://translate.google.com'
}
after { browser.close }
context 'simple tests' do
it 'simple test' do
log.info("Running simple test")
browser.text_field(:id => "source").set("ost")
# actual test/asserts here
end
end
end
The problem is that I am unable to call any logging method such as log.info inside the example. I get this error:
Failures:
1) ui tests simple tests simple test
Failure/Error: log.info("Running simple test")
NoMethodError:
undefined method `info' for 0:Fixnum
Why is this failing? Is it a scope issue? If I comment out the log.info the spec runs fine. I can use any methods on the "browser" object (for example, the call to browser.text_field) inside the example without a problem.
Where I am going wrong?
The value of log is the last thing evaluated inside the let(:log) { } block, which is Logger::DEBUG. This constant just corresponds to the Fixnum 0, and there's no method called info for 0. Try this instead:
let(:log) {
Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
}
By the way, what are you trying to do with setting log = Logger.new(STDOUT) and then immediately reassigning log = Logger.new('watir.tests.log', 'daily')?

Resources