Browser.back is not working

408 Views Asked by At

Using watir, I've written scripts to check multiple links are being directed to the right page as below.

Links= ["Link", "Link1"]

Links.each do |LinkValue|        
  @browser.link(:text => LinkValue).wait_until_present.click
  fail unless @browser.text.include?(LinkValue)
  @browser.back   
end

What I am trying is:

maintaining Linktext in an array
iterating with each linktext
verify
navigate to the previous page to start verifying with next linktext.

But the script is not working. It is not executing after first value and also not navigating back.

2

There are 2 best solutions below

2
Engr. Hasanuzzaman Sumon On

The following scrip working for me

require 'watir'
browser = Watir::Browser.new(:firefox) # :chrome also work
browser.goto 'https://www.google.com/'
browser.link(text: 'Gmail').wait_until_present.click
sleep(10)
browser.back
sleep(10)
0
orde On

You are calling Kernel::Fail, which will raise an exception if the condition isn't satisfied.

In this case, it looks like you are expecting that the destination page will contain the same link text that was clicked on the originating page. If that's not true, then the script will raise an exception and terminate.

Here's a contrived "working" example (which only "works" because the link text exists on both originating and destination pages):

require 'watir'

b = Watir::Browser.new :chrome
b.goto "http://www.iana.org/domains/reserved"

links = ["Overview", "Root Zone Management"]

links.each do |link|
  b.link(:text => link).click
  fail unless b.text.include? link
  b.back
end

b.close

Some observations:

  • I wouldn't use fail here. You should investigate a testing framework like Minitest or rspec, which have assertion methods for validating application behavior.
  • In ruby, variables (and methods and symbols) should be in snake_case.