Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Capybara find() and fill_in() methods

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.69k
    Comment on it

    Selenium is an open-source browser automation tool. So, while automating any functionality first we have to locate the element. Capybara provides two methods to locate elements:

     

    find()

    fill_in()

     

    Let suppose we have an input textbox that takes email as as argument. So, we can use both the methods to enter text in the input field:

     

    HTML:

    <input id="email" class="forgotPswd" type="text" value="" name="email"/>

    Method 1:

    def enter_forgot_pwd_email(email)
     @session.find('#email').set(email)
     self
    end

    Method 2:

    def enter_forgot_pwd_email(email)
     @session.fill_in '#email', with: email
     self
    end

    The first method works fine but the second method is unable to find element and giving error that: “Unable to locate “#email”


    The reason for this uneven behavior is that locator for find() and fill_in() method is different. find() method assumes that the first parameter is the default selector(xpath or css) if the first parameter is not a symbol. The first parameter in fill_in method is id, name or label text. So, when using find method then do not pass xpath or css as an argument.

     

    def enter_forgot_pwd_email(email)
     @session.fill_in 'email', with: email
     self
    end

     

    In the above method, now you can see that me passed only the id of element without any symbol then capybara has easily located the element.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: