Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.5 kB
1
Indexable
Never
import time
import unittest
from selenium import webdriver

class BaseTest(unittest.TestCase):
    def setUp(self):
        """Set up the WebDriver instance and configure test environment."""
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()

    def tearDown(self):
        """Tear down the WebDriver instance and capture screenshots on failure or success."""
        # Access the test outcome
        result = self._outcome.result
        
        # Generate timestamp for screenshot filenames
        timestamp = time.strftime("%Y%m%d-%H%M%S")

        # Capture a screenshot if there are any failures
        if any(error[1] for error in result.failures):
            screenshot_filename = f"screenshot_{timestamp}_failure.png"
            self.driver.save_screenshot(screenshot_filename)
            print(f"Failed screenshot {screenshot_filename} saved.")
        elif any(error[1] for error in result.errors):
            screenshot_filename = f"screenshot_{timestamp}_error.png"
            self.driver.save_screenshot(screenshot_filename)
            print(f"Error screenshot {screenshot_filename} saved.")
        else:
            screenshot_filename = f"screenshot_{timestamp}_success.png"
            self.driver.save_screenshot(screenshot_filename)
            print(f"Success screenshot {screenshot_filename} saved.")

        # Quit the WebDriver
        self.driver.quit()
Leave a Comment