1

I am testing a website that redirects the user to another website for buying stuff. Once I click on the Buy Now button a new browser tab opens and another website is launched displaying the product.

The objective is to test that the new website (which opens in a new tab) opens successfully (200 OK) without any error. This will assert that the integration is working fine (The new website is an affiliate to ours)

The solution I am trying is below:- (The code below is after reaching the buy now button)

self.driver.switch_to.window(self.driver.window_handles[1])
import requests
x = requests.get(self.driver.current_url)
print(x.status_code)

This returns HTTP 302, the definition of 302 is given below:-

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.

With 302 status I cannot say my test is passed. How can I proceed with this test? What else can be done to make sure I have a valid test, asserting 302 doesn't seem to the right way.

1

Seems your switching tab is not working properly. Please check that new tab handler is okay.

# collects handle ID of current window
        first_tab_handle = driver.current_window_handle
        print("first_tab_handle : "+str(first_tab_handle))

# collects handle ID after new tab is opened
        print("current window handle : "+ str(driver.current_window_handle))
        if driver.current_window_handle == first_tab_handle:
            print("driver focus is not switched to new tab opened using actionChains.")
        else:
            print("window handle has changed. Driver switched focus to new tab.")
        driver.switch_to.window(driver.window_handles[1])
        time.sleep(3)

Please use wait() method properly as new tab loading may require extra time.

For more help, you can check how to switch on active tab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.