1

I am experimenting with using rspec and watir to do some tdd and have come across a problem I can't seem to get past. I want to have watir click a link (target="_blank") and then get the url of the newly loaded page. Watir clicks the link but when I attempt to get the url I receive the old url not the current. Watir docs seem to indicate that the Browser url method will return the current url. I found a blog post that seems to solve this issue by having Watir execute some javascript to get the current url but this isn't working for me. Is there anyway to get the current url from a link click with Watir?

<!-- the html -->
<a href="http://www.linkedin.com" target="_blank">LinkedIn</a>

#The rspec code
it "should load LinkedIn" do
  browser.link(:href => "http://www.linkedin.com").click
  browser.url.should == "http://www.linkedin.com"
end

1 Answer 1

2

The target will load the link in a new browser window, therefore you need to switch to that window to assert the url:

it "should load LinkedIn" do
  browser.link(:href => "http://www.linkedin.com").click
  browser.window(:title => /.*LinkedIn.*/).use do
    browser.url.should == "http://www.linkedin.com"
  end
end

See: http://watirwebdriver.com/browser-popups/ for more examples

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I didn't realize I needed to switch to the new window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.