0

So when my test pass it will show the "yeah" word in the end, however when it fails , it will only show and exception NoSuchElementException and the word "Failed" will not show up anywhere. its like else statement is not there. What should i do to make the word Failed show up when the test fails.

class CreateAccount(unittest.TestCase):
    def setUp(self):
        global driver
        driver = webdriver.Firefox()
        driver.implicitly_wait(10)
        driver.get("http://www.copy.com")
        driver.maximize_window()


    def test_main(self):
        createNewAccount = driver.find_element_by_xpath(
            "/html/body/main/div/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/div/form/div[5]/div[1]/a")
        createNewAccount.click()

        firstName = driver.find_element_by_xpath("//*[@id='dom_id_3']")
        lastName = driver.find_element_by_xpath("//*[@id='dom_id_4']")
        emailField = driver.find_element_by_xpath("//*[@id='dom_id_5']")
        passwordField = driver.find_element_by_xpath("//*[@id='dom_id_6']")

        submitButton = driver.find_element_by_xpath(
            "/html/body/main/div/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div[2]/div[1]/form/button")

        firstName.send_keys("mike")
        lastName.send_keys("mano")
        emailField.send_keys("koko1jjjjjk2h5k86@qa.test")
        passwordField.send_keys("test12")

        submitButton.click()

        xpath = driver.find_element_by_xpath(
            "/html/body/main/div/article[2]/div[4]/header/div[2]/div/div[4]/a")

        **if xpath.is_displayed():
            print("yeah")
        else:
            print("Failed")**


    def tearDown(self):
        driver.quit()


if __name__ == "__name__":
    unittest.main()
0

Your if statement feels like it's not even there because when your test encounters and exception and dies, that if statement effectively is not even there. The line of code that is dying is this:

xpath = driver.find_element_by_xpath(
    "/html/body/main/div/article[2]/div[4]/header/div[2]/div/div[4]/a")

Your if statement never gets a chance to run. This makes sense because you have to find a thing before you can determine if it is displayed or not.

Since your code is stopping on an exception, but you want it to continue, you'll need to handle that exception.

You can do something like this:

try:
    xpath = driver.find_element_by_xpath("/html/body/main/div/article[2]/div[4]/header/div[2]/div/div[4]/a")
except NoSuchElementException, e:
    print("Failed")
    raise NoSuchElementException(e) #or any other exception you find more relevant
2
  • Yeah but this way it will show the Failed message however the test will pass and will not show an exception. – user3671239 Apr 7 '15 at 22:44
  • @user3671239 Just throw an exception after the print function if that's how you want it to work. I'll edit the answer to demonstrate. – Lucas Schwarz Apr 8 '15 at 17:42

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.