Capybara: Method title does not work correctly for iframe scope

Created on 11 Mar 2017  路  14Comments  路  Source: teamcapybara/capybara

Meta

Capybara Version: 2.12.1
Driver Information (and browser if relevant): Selenium Webdriver (chrome, phantomjs, probably others)

Expected Behavior

Capybara changes current scope and document on within_frame method usage.
Similar to text method https://github.com/teamcapybara/capybara/blob/master/lib/capybara/node/document.rb#L23
the title method should depend on a current document.

Actual Behavior

It always returns browser title instead of current document title

Steps to reproduce

within_frame(1) do
  puts text #returns frame text as expected
  puts title #returns unexpected browser title instead of frame title
end

Proposal

lib/capybara/node/document.rb

def title
  find(:xpath, '/html/head/title', visible: false).text(:all)
end

All 14 comments

Using the code https://gist.github.com/twalpole/1147a74a892607f19b3a22d34dd937d3 , this works correctly for both Poltergeist and capybara-webkit, but fails for Selenium. This is a bug in selenium-webdriver and should be reported there. It could possibly be worked around in Capybara::Selenium::Driver but I would not accept any changes in Capybara::Node::Document .

@twalpole Thanks a lot for your quick feedback. I have corrected affected drivers. You are right, originally I tested on phantomjs driver (selenium-webdriver gem). You also are right regarding edge case when a title is missing in a frame. I proposed a raw code.

If the bug is fully connected with selenium-webdriver gem, I suggest to post the corresponding issue there and close this one. At least other users will have an ability to find this conversation about the problem.

After looking at this further, it may actually be Poltergeist and capybara-webkit that are producing the incorrect result here. selenium-webdriver is following the webdriver spec which states that title is the title of the top level browsing context, not of the current browsing context.

@route @mhoran @jferris Do any of you have any input on which should be the correct interpretation of #title

Looks like inconsistency here. I found #current_url also works differently for different drivers. I am author Howitzer gem. It uses capybara. To make sure a correct frame is opened I need to validate the frame url and title. Unfortunatelly, right now I have to go with monkey patching Capybara::Selenium::Driver https://github.com/strongqa/howitzer/blob/master/lib/howitzer/web/capybara_methods_proxy.rb#L5

@romikoops Yes there is currently some inconsistency between drivers behavior in this but since we generally tend to lean towards the webdriver definition of things we'll probably end up making the current selenium behavior the "correct" one - which is that title and current_url refer to the top level browsing context and not the frames as specified here - https://w3c.github.io/webdriver/webdriver-spec.html#get-current-url - which correspond to what users can actually see in the browser. Monkey patching different behavior into Capybara is a terrible idea, you'd be much better off adding methods like frame_url and frame_title or something

Interesting. It certainly makes sense that the title would correspond to what is shown in the title bar of my browser. However, we went out of our way to support extracting the title from the current frame context. The same goes for #current_url, and I'm sure other methods (since the webdriver spec came along after we wrote capybara-webkit.)

@mhoran Yeah -- I'm wavering between whether Capybara should add frame_url and frame_title driver methods (and then maybe allow a parameter for assert_current_path, assert_title to choose that the user wants frame level) or make seleniums title and current_url return the frames info when in the frame context. The first would more align with the webdriver spec (and I prefer to go towards that), but I'm having a hard time coming up with a case where it makes sense to want to check the pages URL from inside a frame context.

Yeah, I agree. I think the top frame makes sense.

On Mon, Mar 13, 2017 at 03:29:21PM -0700, Thomas Walpole wrote:

@mhoran Yeah -- I'm wavering between whether Capybara should add frame_url and frame_title driver methods (and then maybe allow a parameter for assert_current_path, assert_title to choose that the user wants frame level) or make seleniums title and current_url return the frames info when in the frame context. The first would more align with the webdriver spec, but I'm having a hard time coming up with a case where it makes sense to want to check the wholes pages URL from inside a frame context.

--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
https://github.com/teamcapybara/capybara/issues/1845#issuecomment-286263845

--
Matt Horan [email protected] http://matthoran.com/

I think we should follow webdriver spec and there's always a way for users to find a "proper" title or url for a frame without monkey patching capybara just using proper xpath inside within_frame right?

@route - yes there is.

Ok, so I think the consensus is that current_url and title when called on the session should return the top level context url and title (that which would be shown in the browser) which is how selenium currently works. For Capybara 2.14, not the 2.13 release going out today/tomorrow, I'll add tests for that (will require changes in poltergeist and capybara-webkit) and look at possibly adding some frame specific title and url methods. Sound good?

@twalpole yep perfect

@twalpole Amazing! Let me know if I can help you with something.

This will actually be a breaking change - It's going to move to 3.0

Fixed via 9eb7e64879fa734c9b66dbd3646cdcca47717054

Was this page helpful?
0 / 5 - 0 ratings