0

I have next code which gets http code when url has been opened directly:

require 'net/http'
require 'uri'

  def visit_page(url, response_code: false)
    @browser.goto(url)
    if response_code
      page_status_code(url)
    end
  end

  def page_status_code(url)
    uri = URI.parse(url)
    connection = Net::HTTP.new(uri.host, uri.port)

    # If SSL.
    # connection.use_ssl = true
    # connection.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Get.new(uri.request_uri)
    response = connection.request(request)
    response.code
  end

In scenario it looks next:

response_code = page.visit_page('https://www.google.com', response_code: true)
puts response_code

And how I can apply this to get http code if I click on link?

page.link_element(text: 'lorem ipsum').click
Den Silver
  • 199
  • 16

2 Answers2

0

If you are click on anchor tag and it has href attribute the you try with the following code.

    Hrefvalue = page.link_element(text: 'lorem ipsum').get_attribute ('href')
    response_code = page.visit_page( Hrefvalue, response_code: true) 
puts response_code
Murthi
  • 5,299
  • 1
  • 10
  • 15
  • in this case I will just get href of ulr and will need to use `visit_page` method. And I mean get http code when link was clicked. Your variant was already appliyed before :). – Den Silver Jun 09 '17 at 17:42
0

I've found another solution:

mylink = @browser.link(:text, 'Lorem Ipsum')
url = URI.parse mylink.href
status_code = Net::HTTP.start(url.host, url.port) { |http| http.head(url.request_uri).code }
puts url
status_code.should == '200'

In this case I even do not need to click on link and its code will be got.

Den Silver
  • 199
  • 16