1

I have this functional test that gets a url from an href.

But how do I test test if it is valid (ie: 200/success and not 404)

def test_card_links(self):
    """Click card, make sure url is valid"""
    card_link = self.browser.find_element_by_css_selector('#app-scoop-panel a').get_attribute('href');

1 Answer 1

1

Once you retrieve the href attribute as card_link you can check the validity of the link using either of the following approaches:

  • Using requests.head():

    import requests
    
    def test_card_links(self):
        """Click card, make sure url is valid"""
        card_link = self.browser.find_element_by_css_selector('#app-scoop-panel a').get_attribute('href')
        request_response = requests.head(card_link)
        status_code = request_response.status_code
        if status_code == 200:
            print("URL is valid/up")
        else:
            print("URL is invalid/down")
    
  • Using urlopen():

    import requests
    import urllib
    
    def test_card_links(self):
        """Click card, make sure url is valid"""
        card_link = self.browser.find_element_by_css_selector('#app-scoop-panel a').get_attribute('href')
        status_code = urllib.request.urlopen(card_link).getcode()
        if status_code == 200:
            print("URL is valid/up")
        else:
            print("URL is invalid/down")
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.