如何在conftest.py

时间:2019-10-01 19:44:07

标签: python-3.x selenium pytest fixtures

我的pytest /硒代码需要帮助。

我的 conftest.py 文件中包含以下代码。

import pytest
from base.webdriverfactory import WebDriverFactory
from pages.login_page import LoginPage

@pytest.fixture(scope="class")
def oneTimesetUp(request, browser):
   print("Running one time setUp")
   wdf = WebDriverFactory(browser)
   driver = wdf.getWebDriverInstance()
   lp = LoginPage(driver)
   lp.login("haykpo", "Aaaa4321")

   if request.cls is not None:
       request.cls.driver = driver
   yield driver
   lp.log_out_from_wp_page()
   driver.quit()
   print("Running one time tearDown")

这是我使用此灯具的测试文件

@pytest.mark.usefixtures("oneTimesetUp", "tearDown")
class TestPublishWithOfflinewAsset:

    @pytest.fixture(autouse=True)
    def objectSetup(self, oneTimesetUp):
        self.pub = TestPublishofflineWOMedia(self.driver)

    def test_publish_with_off_w_media(self):
        self.pub.select_project('Test cases - Avallain')
        self.pub.check_lo_plays_normally(15292)

我知道yield后面的所有内容都将被编码为tearDown。因此,我写了这两行是为了从应用程序注销并在测试执行结束时退出浏览器。

lp.log_out_from_wp_page()
driver.quit()

但是,问题是,当我开始测试时以及在测试执行过程中浏览器焦点切换到iframe时,注销部分(而不是有趣的退出部分)总是在执行几个步骤之后执行(或者可能只是一个巧合)不知道iframe是否有罪)。我如何编写代码以退出拆卸日志或在达到预期的产量后但不执行测试时想要的任何内容?

有关更多详细信息,这里是带有代码的详细文件。 以下是我的登录页面

from base.basepage import BasePage

class LoginPage(BasePage):

    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

    ### Locators ###
    _name_field = "form1:Name"
    _password_field = "form1:Password"
    _login_button = "form1:loginButton"
    _work_packages = "wpButton"
    _user_logout_dropdown_button = 'rolebutton_button'
    _logout_button = 'blogout'


    def log_out_from_wp_page(self):
        self.elementClick(locator=self._user_logout_dropdown_button)
        self.driver.implicitly_wait(5)
        self.elementClick(locator=self._logout_button)

下面是webdriver factory

class WebDriverFactory:

    def __init__(self, browser):
        self.browser = browser

    def getWebDriverInstance(self):
        """
       Get WebDriver Instance based on the browser configuration

        Returns:
            'WebDriver Instance'
        """
        global driver
        baseURL = "https://example.com"
        if self.browser == "iexplorer":
            # Set ie driver
            driver = webdriver.Ie()
        elif self.browser == "firefox":
            driver = webdriver.Firefox()
        elif self.browser == "chrome":
            # Set chrome driver
            driver = webdriver.Chrome()
            #driver.set_window_size(1440, 900)
        else:
            driver = webdriver.Chrome()
        # Setting Driver Implicit Time out for An Element
        driver.implicitly_wait(10)
        # Maximize the windows
        driver.maximize_window()
        # Loading browser with App URL
        driver.get(baseURL)
        return driver 

下面的是BasePsge

class BasePage(SeleniumDriver):

   def __init__(self, driver):

        super(BasePage, self).__init__(driver)
        self.driver = driver

0 个答案:

没有答案