Using Watir to Check for Bad Links

Using Watir to check for bad links

My answer is similar idea with the Tin Man's.


require 'net/http'
require 'uri'

mylinks = Browser.ul(:id, 'my_ul_id').links

mylinks.each do |link|
u = URI.parse link.href
status_code = Net::HTTP.start(u.host,u.port){|http| http.head(u.request_uri).code }
# testing with rspec
status_code.should == '200'
end

if you use Test::Unit for testing framework, you can test like the following, i think


assert_equal '200',status_code

another sample (including Chuck van der Linden's idea): check status code and log out URLs if the status is not good.


require 'net/http'
require 'uri'

mylinks = Browser.ul(:id, 'my_ul_id').links

mylinks.each do |link|
u = URI.parse link.href
status_code = Net::HTTP.start(u.host,u.port){|http| http.head(u.request_uri).code }
unless status_code == '200'
File.open('error_log.txt','a+'){|file| file.puts "#{link.href} is #{status_code}" }
end
end

How to loop through links and click each one using Watir

I think it won't be possible to click each link inside the loop since clicking a link will trigger a page load and I believe this invalidates the other Watir Link Elements.

So, what I suggest is that you take the IDs of the links and then loop over each other doing the clicks.


For instance, I would add an specific class to those sorting links and a meaningful id too.
Something along the lines of:

<a id='sortby-name' class='sorting' href='...'>Name</a>

Then you can pick them with this:

$ie.table(:id, "myGrid").body(:index, 1).each do | row |
# pick sorting links' ids
sorting_link_ids = row.links.select{|x| x.class_name == "sorting"}.map{|x| x.id}
end

And then, you do your clicking like this:

sorting_link_ids.each do |link_id|
$ie.table(:id, "myGrid").body(:index, 1).link(:id, link_id).click
end

Hope that helps!

How to access the following link element using Watir

You could locate the image and then get the parent element:

browser.image(:alt => "").parent.click

Or you could find a link that contains the image:

browser.links.find{ |a| a.image(:alt => "").exists? }.click

If you really want to use the text, then you can use a :text => ''. The text locator only checks the visible text of the link - ie it ignores the image.

browser.link(:text => '').click

Using Watir-webdriver how to check the URL of a page

browser.url will return url of the page, so to check if it is as expected, try something like this:

browser.url == "www.example.co.uk/news?page=1"

It will return true or false.

How to select an inline element using watir in ruby

The problem is that the link's text is not "HEATMAPS". It is actually "Heatmaps". The text locator is case-sensitive, which means you need:

lin = browser.link :text=> 'Heatmaps'

You can see this if you inspect the HTML:

<a ui-sref="campaign.heatmap-clickmap" href="#/analyze/analysis/108/heatmaps?token=eyJhY2NvdW50X2lkIjoxNTA3MzQsImV4cGVyaW1lbnRfaWQiOjEwOCwiY3JlYXRlZF9vbiI6MTQ0NDgxMjQ4MSwidHlwZSI6ImNhbXBhaWduIiwidmVyc2lvbiI6MSwiaGFzaCI6IjJmZjk3OTVjZTgwNmFmZjJiOTI5NDczMTc5YTBlODQxIn0%3D">
<!-- boIf: isAnalyticsCampaign -->
<span bo-if="isAnalyticsCampaign" class="ng-scope">Heatmaps</span>
<!-- boIf: !isAnalyticsCampaign -->
</a>

It only looks like "HEATMAPS" due to the styling. One of the styles includes a text-transform: uppercase; which visually capitalizes the text. Watir does not interpret the styles, so only knows that the text node is "Heatmaps".

Once you have identified the link, clicking still has a problem:

lin.click
#=> Selenium::WebDriver::Error::UnknownError:
#=> unknown error: Element <a ui-sref="analyze.heatmaps" ng-class="{selected: (locationContains('analyze', 'heatmap') && !locationContains('analyze', '/analysis') && state.current.name !== 'campaign.heatmap-clickmap') || (isAnalyzeHeatmapEnabled && (isAnalyzeDeprecatedHeatmapView || locationContains('analyze', '/analysis', 'heatmaps')) )}" data-qa="nav-main-analyze-heatmap" href="#/analyze/heatmap">...</a>
#=> is not clickable at point (90, 121).
#=> Other element would receive the click: <div ng-show="!isCROSetupView" class="">...</div>

The link being located is actually the one in the left menu, which is disabled, rather than the top menu. You need to scope the link locator to just the top menu. Using the parent ul element appears to be sufficient:

lin = browser.ul(class: 'page-nav').link(text: 'Heatmaps')
lin.click

To see the click complete, you might want to tell Chrome not to close at the end of the script. This is done by opening the browser using the following option:

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => true})
browser = Watir::Browser.new :chrome, desired_capabilities: caps

How to find a HTML link using Watir (3.0) and the name attribute

Since the error message says name is an unknown way of finding a <a> element, I guess name attribute is not longer supported for links. That looks like a bug to me, since as far as I can see name attribute is still supported for links: http://www.w3.org/TR/html401/struct/links.html

You can report the bug here: https://github.com/watir/watir-classic/issues

Search Text in Table and Click Link on different column Using Watir

After looking at the answer from Chuck van der Linden above, I modified it a bit and got a working code as follow:

table = driver.table(:class => ["table table-striped table-bordered table-hover"])
textRow = table.td(:text => "TestingName1").parent
if textRow.a(:id => "lnkDeleteVendor").exists?
textRow.a(:id => "lnkDeleteVendor").click
end

Thank you.



Related Topics



Leave a reply



Submit