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

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

+3


source to share


2 answers


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 information

+2


source


If you are collecting image urls (using watir, nokigiri or mechanize ), you can use open-uri to request 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

      

0


source







All Articles