hjelpmig hjelpmig - 3 months ago 9
Ruby Question

How can I ignore the host name when testing that an HTML attribute contains an expected URL?

I am writting a test to check if meta tag exists and if the content matches. Look at the example below:

def have_meta(name, expected)
page.find "meta[property='#{name}'][content='#{expected}']", :visible => false
end


I am calling the function as below:

have_meta("og:title", "this is the title")
have_meta("og:url", "http://www.lorem.com/ipsum")


The problem is that my url contents changes based on which environment I am working in. E.g. It can be:

http://www.lorem.com/ipsum


or

http://www.sdfsd.com/ipsum


I want to have a regex match for the "content" part of Meta. Ignoring the main url but checking the "ipsum" part. Do you know how can I do that?

Answer

I would use CSS's ends-with attribute selector:

def have_meta_with_path(name, path)
  page.find "meta[property='#{name}'][content$='#{path}']", :visible => false
end

where path is just e.g. '/ipsum'. No regex needed. It will be safest to have two methods: your original have_meta, which tests the entire value, and the one above, which tests the end. You wouldn't want to just test the end of the og:title.