user3597472
user3597472

Reputation: 7

Python Selenium Web Driver - fail the test case deliberately if, "if condition is false"

I'm new to automation. Please help.

I want to fail the test case if the below condition is false. How can I achieve that with python code in selenium webdriver. In below case, it passes the test even if the condition is false (BIAdisplayed is 359631 i expect it to fail). See the execution result at the bottom.

    BIA = driver.find_element_by_xpath("/html/body/div[3]/div/accordion/div/div/div[2]/div/div[1]/form/fieldset/div[2]/div[2]/ul/li[3]/ul/li[1]/input")
    BIAdisplayed = BIA.get_attribute("value")

    if BIAdisplayed == 0:
        try:
            print BIAdisplayed
        except Exception as e:
            print e
            raise # Raise the exception that brought you here 
    driver.close()
    driver.switch_to_window(main_window)

=========================================================================

C:\Python26\Scripts>AMWebRegressionTest.py

Enter Alert AccessCode: TEST01

Enter Username: test

Enter Password:

....

359631

.

Ran 6 tests in 394.925s

OK

Upvotes: 0

Views: 2185

Answers (2)

MA53QXR
MA53QXR

Reputation: 82

The main point is that your statement

    if BIAdisplayed == "0":

prevents Python to throw the exception. BIAdisplayed == 359633 (?) so it does not satisfy this condition, so Python skips every code inside the if-block, including the part where the exception is thrown.

I am not sure what you want to do, but you could alter the if-block in

    if BIAdisplayed == "359633":
        raise Exception('BIAdisplayed == "359633"')

Upvotes: 0

Saifur
Saifur

Reputation: 16201

You are comparing string with int

BIA = driver.find_element_by_xpath("/html/body/div[3]/div/accordion/div/div/div[2]/div/div[1]/form/fieldset/div[2]/div[2]/ul/li[3]/ul/li[1]/input")

BIAdisplayed = BIA.get_attribute("value")

if BIAdisplayed == "0":
    try:
        print BIAdisplayed
    except Exception as e:
        print e
        raise # Raise the exception that brought you here 
driver.close()
driver.switch_to_window(main_window)

Upvotes: 1

Related Questions