0

I am creating a few classes for some automated tests. I've created a LoginTest class that logs into a website. Once the LoginTest class is finished testing I would like to pass the logged in instance to the next class. How should I go about doing that?

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from dotenv import load_dotenv
import os
import unittest
import time

class LoginTest(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # load env
        load_dotenv()

        # Initialize Chrome driver
        self.driver = webdriver.Chrome()

        # Set implicit wait to 10 seconds
        self.driver.implicitly_wait(10)

    def test_login(self):
        # Get email and password from environment variables
        email = os.getenv("EMAIL")
        password = os.getenv("PASSWORD")

        driver = self.driver

        # Go to login screen
        driver.get("https://website.com/login")

        # Locate the email and password input fields and enter the credentials
        email_field = driver.find_element(By.ID, "input_email")
        email_field.send_keys(email)

        password_field = driver.find_element(By.ID, "input_password")
        password_field.send_keys(password)

        # Click the login button
        driver.find_element(By.NAME, "form.submitted").click()

        watchlist_title = EC.title_contains('Login | Dashboard')
        # test this if it works
        # probably should just check if the url is correct
        self.assertIsNotNone(watchlist_title, "Login failed. Home page title is not found.")

    @classmethod
    def tearDownClass(self):
        # Close the browser
        self.driver.quit()

class DataTableTest(LoginTest):
    def test_table_entries(self):
        driver = self.driver

        # Continue using the logged-in session for further test steps
        driver.get("https://datatable.com")

class ChartTest(LoginTest):
    def test_chart(self):
        driver = self.driver

        # Continue using the logged-in session for further test steps
        driver.get("https://chart.com")


if __name__ == "__main__":
    unittest.main()
Ryan113
  • 676
  • 1
  • 10
  • 27
  • Which one is the first used, which one is the second used class? And in which class are you logging in? – obayhan Jul 17 '23 at 06:29
  • [Selenium says to ensure that the tests are isolated from one another](https://www.selenium.dev/documentation/test_practices/encouraged/avoid_sharing_state/). To retain login instance, [you can try adding cookies](https://stackoverflow.com/questions/15058462/how-to-save-and-load-cookies-using-python-selenium-webdriver) – Reyot Jul 17 '23 at 07:11
  • @obayhan The LoginTest class is used first. Then DataTableTest. Then ChartTest. Reading what Daniel Sabrel posted though it sound like this is a bad idea. Apparently I should be creating a new Webdriver instance for every test. This allows for test isolation and makes parallelization simpler. – Ryan113 Jul 17 '23 at 15:49
  • @DanielSabrel That is interesting. So every time I run a new test class I should be creating a unique Webdriver instance for it? Sounds like it would add some time to the test running to log in every time, but makes sense that the tests can run in isolation. – Ryan113 Jul 17 '23 at 15:52
  • [If you don't have any module fixtures, you can use the --level=class option to run test classes in parallel](https://pypi.org/project/unittest-parallel/#:~:text=Parallelism) – Reyot Jul 17 '23 at 16:22

0 Answers0