Search code examples
rubywatirwatir-webdrivernomethoderror

watir method conflicts with my ruby class


Hi guys i'm doing some automations scripts with Watir and trying to create a ruby class to make it better but i'm having this error:

examen.rb:6:in 'enterEmail': undefined method 'text_field' for # (NoMethodError)

This is part of my conflictive code:

require 'watir-webdriver'
class LoginPage 
  def enterEmail (email)
    text_field(:user, :id => 'user_email').set email
  end
end

The problem that i see is: i did not define the 'text_field()' method in my class just because is a Watir method... Anyone knows how can i use the watir method in the classes that i create?


Solution

  • text_field is an instance method of The Watir::Browser class. If you want to use it, you have to call it on an instance of that class.

    class LoginPage
      def initialize()
        @b = Watir::Browser.new
      end 
    
      def enterEmail (email)
        @b.text_field(:user, :id => 'user_email').set email
      end
    end