teampoltergeist / poltergeist

A PhantomJS driver for Capybara
MIT License
2.5k stars 415 forks source link

Trouble evaluating javascript within iFrame to locate an element within it #848

Closed reidcooper closed 7 years ago

reidcooper commented 7 years ago

Capybara Poltergeist

I am trying to process a jQuery script within an iFrame to locate an element, or count it. However, the script results 0. When I pause the rspec test, it results in 1, as well as in development.

capybara 2.7.1 poltergeist (1.10.0) phantomjs: stable 2.1.1

it "should be able to initialize album" do
    visit "/t/socks"

    execute_script('scrollTo(0, $("#pixlee_container").position().top-100)')

    @iframe = all('iframe[title="Pixlee media widget"]').last

    within_frame(@iframe) do
      find(:xpath, '//li[@class="photo_block"]', :match => :first).click
      expect(evaluate_script('$("div.mfp-container.mfp-image-holder").length')).to eq(1)
    end
  end
twalpole commented 7 years ago

Where are you pausing the test that changes the result? If it's right after the find line - then it's probably because the action click triggers hasn't completed before you're evaluating the script. You either need to check for some visible change that occurs on the page before running your evaluate_script or better yet, just use the Capybara rspec matchers provided, instead of using evaluate_script, since they will wait/retry until the condition becomes true

expect(page).to have_css('div.mfp-container.mfp-image-holder', count: 1)