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!
Related
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
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!!
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
My code has a lot of this: driver.blahblahblah. Consider the following code sample, taken from http://www.browserstack.com/automate/ruby.
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for(:remote,
:url => "http://USERNAME:ACCESS_KEY#hub.browserstack.com/wd/hub")
driver.navigate.to "http://www.google.com/ncr"
element = driver.find_element(:name, 'q')
element.submit
puts driver.title
driver.quit
How can I make driver implicit? For example there's a method called driver.save_screenshot(). I want to say save_screenshot("a.png") because only the driver variable/object has this method.
You can use delegate from ActiveSupport like the example below:
require 'active_support/core_ext/module/delegation'
class MyClass
delegate :find_element, :save_screenshot, to: :driver
def foo
find_element
save_screenshot
end
def driver
#driver ||= Driver.new
end
end
class Driver
def find_element
puts "find_element"
end
def save_screenshot
puts "save_screenshot"
end
end
MyClass.new.foo
Or decorate the driver using SimpleDelegator (but I don't recommend that).
If you have lots of methods whose receiver is driver, then a way to make the receivers implicit is:
driver.instance_eval do
method_1...
method_2...
...
end
but notice that this slows down a little. If you are just finding a way to be lazy, then the best way is to make the local variable as short as a single letter, and do not bother making it implicit.
d = .... # instead of `drive`
d.method_1...
d.method_2...
...
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,