Search code examples
seleniumautomated-testsrubygemscapybara

I'm trying to find an invisible field to fill in using Capybara, but nothing I am doing seems to work


I am currently trying to fill in the Google sign in form using Capybara, but I am having the hardest time getting it to find the hidden field to fill in.

Here is the HTML for the field I am trying to fill in.

enter image description here

Here is the test code

require "rails_helper.rb"

RSpec.describe "Sign in page" do
    it 'displays the page intention' do
        visit('/mars')
        expect(page).to have_content 'Sign in with Google'
    end
    it 'fills in user information' do
       find(:xpath, '//*[@id="identifierId"]', visible: false)
    end
end

And this is the error I am getting.enter image description here


Solution

  • First, you shouldn't be attempting to fill in a hidden field, you should be interacting with the visible elements like a user would have to. In this case, it may mean needing to click on another element first to trigger the email input to change to being active. Something like

    first('form div[jscontroller]').click
    fill_in('identifierId', with: email)
    

    should work for the google login page.

    Note: also you should be preferring CSS over XPath for finding elements whenever possible, since it will be faster, easier to read, and can't unintentionally break element scoping (// vs .// in XPath - see https://github.com/teamcapybara/capybara#beware-the-xpath--trap)

    Update: Additionally all the it blocks are completely independent, with a browser reset in between, so you need to visit the desired page for each one (if all the it blocks in the describe require visiting the same page you could put the visit in a before block.

    Note: this all assumes you are actually using the Selenium driver for this test, as the question tags imply.