Using pytest fixtures

30 Views Asked by At

I want to handle dependencies and conditional test execution in my test suite which have over 100+ tests. For certain tests, it must pass so that it can move on to the next test in the suite. I am looking at a better way of checking the dependencies in each test and proceed only if the dependency is passed. Currently my tests are like this.

@pytest.fixture(scope='class')
def setup(request):
  try:
    """
    Retrieve secrets from secrets manager
    """
    secret_name = "my-top-secret"
    secret_value = get_secret(secret_name)
    # secret_value = get_secret(secret_name)
    print(secret_value)
    secret_dict = json.loads(secret_value)
    UserName1 = secret_dict.get('UAT_USER_NAME_1')
    UserName7 = secret_dict.get('UAT_USER_NAME_7')
    UserName6 = secret_dict.get('UAT_USER_NAME_6')
    Password = secret_dict.get('UAT_PASSWORD')
    Client = secret_dict.get('UAT_CLIENT1')
    Uat_Url = secret_dict.get('BASE_URL_UAT')
    TestData_common.user_name1 = UserName1
    TestData_common.user_name6 = UserName6
    TestData_common.user_name7 = UserName7
    TestData_common.password = Password
    TestData_common.client = Client
    TestData_common.Url = Uat_Url
except WebDriverException as e:
    print('Collateral UAT seems to be down...> ', e)
    TestData_common.URL_FOUND = False

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument("--window-size=1920,1080")
options.add_argument("--start-maximized")
options.add_argument("--incognito")
options.add_argument('--headless')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
request.cls.driver = driver
driver.delete_all_cookies()
driver.get(TestData_common.Url)
yield
driver.quit()

@pytest.mark.usefixtures("setup")
class Test_Counterparty:
    def test_001_login(self):
    """Login to UAT and click AGREE button in Site Minder"""
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    print('Successfully clicked AGREE button in SiteMinder')
    self.loginPage = LoginPage(self.driver)
    self.loginPage.do_click_agree_button()
    self.driver.maximize_window()
    time.sleep(2)

def test_002_enter_user_name(self):
    """Enter User name for Collateral"""
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.AGREE_BTN_FOUND:
        pytest.fail(reason="Site Minder AGREE button is not found...")
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    self.loginPage = LoginPage(self.driver)
    self.loginPage.do_enter_user_name(TestData_common.user_name6, 'USER')
    if not TestData_common.USER1_ENABLED:
        pytest.fail(reason='Unable to enter User Name..')
    print('Entered User name as: ', TestData_common.user_name6)
    time.sleep(2)

def test_003_enter_password(self):
    """Enter Password for Collateral"""
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.AGREE_BTN_FOUND:
        pytest.fail(reason="Site Minder AGREE button is not found...")
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.USER1_ENABLED:
        pytest.fail(reason='Unable to enter User Name..')
    self.loginPage = LoginPage(self.driver)
    self.loginPage.do_enter_password(TestData_common.password, 'PASS')
    if not TestData_common.PASSWORD_ENABLED:
        pytest.fail(reason='Unable to enter Password..')
    print('Entered password:', TestData_common.password)
    time.sleep(2)

def test_004_click_login_button(self):
    """Click LOGIN button"""
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.AGREE_BTN_FOUND:
        pytest.fail(reason="Site Minder AGREE button is not found...")
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.USER1_ENABLED:
        pytest.fail(reason='Unable to enter User Name..')
    if not TestData_common.PASSWORD_ENABLED:
        pytest.fail(reason='Unable to enter Password..')
    self.loginPage = LoginPage(self.driver)
    time.sleep(2)
    self.loginPage.do_click_login_button()
    if not TestData_common.LOGIN_BTN_FOUND:
        pytest.fail(reason='Unable to click LOGIN button..')
    print('Clicked LOGIN button')
    time.sleep(20)

def test_005_check_title(self):
    """Check Window Title"""
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.AGREE_BTN_FOUND:
        pytest.fail(reason="Site Minder AGREE button is not found...")
    if not TestData_common.URL_FOUND:
        pytest.skip(reason='Collateral UAT seems to be down...')
    if not TestData_common.USER1_ENABLED:
        pytest.fail(reason='Unable to enter User Name..')
    if not TestData_common.PASSWORD_ENABLED:
        pytest.fail(reason='Unable to enter Password..')
     if not TestData_common.LOGIN_BTN_FOUND:
         pytest.fail(reason='Unable to click LOGIN button..')
    self.loginPage = LoginPage(self.driver)
    time.sleep(2)
    title = self.driver.title
    if title == 'Citco Collateral':
        print('Successfully logged into Collateral')
        self.driver.get_screenshot_as_file('dashboard.png')
    else:
        TestData_common.SITE_MINDER_ERR = True
        self.driver.get_screenshot_as_file('test_cpty_SiteMinderError.png')
        pytest.fail(reason='Site Minder error:Not logged into Collateral UAT..')
    time.sleep(2)

There are many buttons and dropdowns and I don't want to check them in every test case, that would be too lengthy. Instead I am thinking of using fixtures to check for failure conditions like unable to click a button, unable to click a dropdown etc. But not sure how should I implement it in the test suite.

Any help is much appreciated.

0

There are 0 best solutions below