Kurt Russell
Kurt Russell

Reputation: 235

How to test 404 error with watir-webdriver in cucumber?

How can I test the 404 errors (page not found) in cucumber? I have for example a page with 100 pictures and I want to test if all of the pictures are visible. I want to check if one or more of the pictures have 404 error or not.

Upvotes: 3

Views: 1365

Answers (2)

orde
orde

Reputation: 5273

If you harvest the image urls (using watir, nokigiri, or mechanize), you can use open-uri to issue requests and then check the response code.

require 'watir-webdriver'
require "open-uri" 

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

urls = b.images.collect(&:src)               # harvest image srcs
urls << "http://www.foo.com/foo.gif"         # example of a 500 error

urls.each do |url|
  begin
    open(url) do |f|
      puts "#{f.base_uri} - #{f.status}"
    end
  rescue => e
    puts "#{url} - #{e}"
  end
end

Upvotes: 0

Alister Scott
Alister Scott

Reputation: 3685

You can do something like this:

require 'watir-webdriver'
b = Watir::Browser.new
b.goto "apod.nasa.gov/"
b.execute_script("return (typeof arguments[0].naturalWidth!=\"undefined\" && arguments[0].naturalWidth>0)", b.image(name: 'imagename1'))

See this blog post for more info

Upvotes: 2

Related Questions