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()