not able to find installed Gem in ruby program

141 Views Asked by At

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 "[email protected]"
    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>'
1

There are 1 best solutions below

0
On

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!