selenium webdriver python - make selenium test case fail if there is error in custom written python code


Question

I have a selenium webdriver test case exported to python using selenium IDE. Then wanted to add some additional functionality so inserted custom code in between the selenium test case.

Now the problem is - if i make a mistake or the custom written python code returns error - still the selenium test case continues to execute

Whereas i want the selenium test case execution to be stopped if there is such error. Have replicated the scenario below with a simple google search followed by a log movement code using shutil module

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
import os, shutil

class SeleniumException(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.co.in/"
        self.verificationErrors = []

        self.attachment = 1
        self.file_source_location = "F:\\python_test\\logfiles\\"
        self.file_move_location = "F:\\logfiles_back\\"

    def test_selenium_exception(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("Check this out")
        if self.attachment:
            try:
                for contents in os.listdir(self.file_source_location):
                    src_file = os.path.join(self.file_source_location, contents)
                    dst_file = os.path.join(self.file_move_location, contents)
                    shutil.move(src_file, dst_file)
                print'files_moved_success'
                driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
            except Exception as e:
                print e

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

In this code - If the movement from logfiles to logfiles_back fails due to reasons like wrong path ...Etc the catch block is executed and i want the selenium test case to quit reporting an error

What is happening right now is it reports the error and completes the execution of the test case

How to make this possible ?

1
3
12/11/2012 6:08:58 AM

Accepted Answer

If you want to raise the actual exception that was triggered, you can call raise by itself, which will raise the last active exception:

try:
    for contents in os.listdir(self.file_source_location):
        src_file = os.path.join(self.file_source_location, contents)
        dst_file = os.path.join(self.file_move_location, contents)
        shutil.move(src_file, dst_file)
    print'files_moved_success'
    driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
    print e
    raise # Raise the exception that brought you here 

If you don't want a traceback and just want to exit, you can also call sys.exit(1) (or whatever error code you want to use) after print e:

import sys
# ...
try:
    for contents in os.listdir(self.file_source_location):
        src_file = os.path.join(self.file_source_location, contents)
        dst_file = os.path.join(self.file_move_location, contents)
        shutil.move(src_file, dst_file)
    print'files_moved_success'
    driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
    print e
    sys.exit(1)
6
12/11/2012 6:23:00 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon