Search code examples
ruby-on-railstestingrspec-rails

How to find a variable with rspec tests using page.find


I have a test case, most of my other pages have at least one field that is just straight text and can be found using:

page.find("tr", text: "What I filled in").find("a.tick").click

This page all of the options are drop down selects, so how do I find a variable? The rest of the syntax looks like this:

it "edits person job and redirects to index" do
    expect(p = FactoryGirl.create(:person)).to be_valid()
    expect(j = FactoryGirl.create(:job)).to be_valid()
    visit new_job_path
    select p.name, from: "person_job_person_id"
    select p.name, from: "person_job_job_id"
    click_button "create person job"
    page.find("tr", p).find("a.tick").click
end

it is unable to find the p to click?


Solution

  • If you want to click on the p element, you can easily do it with css selector or xpath:

    find(:css, 'the css selector of your p element').click
    

    or

    find(:xpath, 'the xpath of your p element').click
    

    For example

    find(:css, 'body > div.layout > div > button').click
    

    If you are using Chrome, you can easily find them by inspecting your element, right click on his code and copying the css selector or the xpath.