Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何分离测试和夹具_Python_Selenium_Pytest - Fatal编程技术网

Python 如何分离测试和夹具

Python 如何分离测试和夹具,python,selenium,pytest,Python,Selenium,Pytest,我在PyCharm中有一个示例项目——由一个简单的测试组成,该测试检查登录到给定的正确工作空间。它有web\u驱动程序目录,目录中有chromedriver,目录中有conftest.py,目录中有用于测试的webdriver设置,目录中有实际测试的tests.py,f.e conftest.py tests.py 现在的问题是将测试分为页面初始化部分-def setup,将实际测试执行部分-def test\u correct\u workspace分为不同的类和文件(类似于页面对象模式) 所

我在PyCharm中有一个示例项目——由一个简单的测试组成,该测试检查登录到给定的正确工作空间。它有
web\u驱动程序
目录,目录中有
chromedriver
,目录中有
conftest.py
,目录中有用于测试的webdriver设置,目录中有实际测试的
tests.py
,f.e

conftest.py

tests.py

现在的问题是将测试分为页面初始化部分-
def setup
,将实际测试执行部分-
def test\u correct\u workspace
分为不同的类和文件(类似于页面对象模式)

所以
conftest.py
的基数应该相同,并将
test.py
除以

page.py

test.py

但可以肯定的是,它不会以这种形式工作:

1) 不知何故,
driver\u get
应该导入到页面初始化文件中,并将转发器导入到
\u初始化\u
-

2) 不知何故,页面初始化应该与其他文件中的测试实现相链接-


不知道如何在页面对象模式中的不同文件之间组织所有这些导入,应避免在测试类中直接引用驱动程序。您可以将page.py作为基类来拥有通用方法。可以将设置移动到其他页面,例如login.py。此设置方法应返回您尝试验证的页面。然后,测试方法应该使用这些页面对象进行验证。我将登录作为conftest.py中的fixture,然后在测试和其他fixture中使用它。举个例子,下面是我给大家的一个概述。你应该多读一点

我建议使用插件,使用selenium和pytest可以减少大量样板代码

conftest.py

@fixture
def selenium(): 
     # pytest-selenium provides this fixture & you can override it if required.
    return selenium

@fixture
def home(selenium):
    #do login
    return HomePage



通过以下方式,在更多临终者的帮助和解释下,设法找到了解决方案

1) conftest中的fixture需要通过
pages.py中我的登录页面的初始化webdriver,并使用内置
request
fixture使用此驱动程序初始化此页面类。因此正确的
conftest.py
将如下所示:

import pytest
import os
from selenium import webdriver
from pages import SigninPage

@pytest.fixture(scope='class', autouse=True)
def driver_get(request):
    request.cls.webdriver = webdriver.Firefox(executable_path=os.path.join("web_drivers", "geckodriver"))
    request.cls.signin = SigninPage(request.cls.webdriver)
    yield request.cls.webdriver

    def fin():
        request.cls.webdriver.quit()
    request.addfinalizer(fin)
class SigninPage(object):
    def __init__(self, web_driver):
        self.driver = web_driver
        self.driver.get("https://slack.com/signin")

        self.input_field = self.driver.find_element_by_xpath("//input[@type='text' and @id='domain']")
        self.continue_button = self.driver.find_element_by_xpath("//button[@id='submit_team_domain']")

    def enter_workspace(self):
        self.input_field.send_keys("test")
        self.continue_button.click()
        return LogInPage(self.driver)


class LogInPage(object):
    def __init__(self, web_driver):
       self.driver = web_driver
       self.login_page = self.driver.find_element_by_xpath("//h1[@id='signin_header']")
2) “pages.py”包括带有接收到的webdriver的
登录页面的init,进入工作区的输入字段和继续按钮,实际执行此操作的方法
进入工作区
,并返回带有要检查的
登录页面
字段的
登录页面
,因此它看起来像:

import pytest
import os
from selenium import webdriver
from pages import SigninPage

@pytest.fixture(scope='class', autouse=True)
def driver_get(request):
    request.cls.webdriver = webdriver.Firefox(executable_path=os.path.join("web_drivers", "geckodriver"))
    request.cls.signin = SigninPage(request.cls.webdriver)
    yield request.cls.webdriver

    def fin():
        request.cls.webdriver.quit()
    request.addfinalizer(fin)
class SigninPage(object):
    def __init__(self, web_driver):
        self.driver = web_driver
        self.driver.get("https://slack.com/signin")

        self.input_field = self.driver.find_element_by_xpath("//input[@type='text' and @id='domain']")
        self.continue_button = self.driver.find_element_by_xpath("//button[@id='submit_team_domain']")

    def enter_workspace(self):
        self.input_field.send_keys("test")
        self.continue_button.click()
        return LogInPage(self.driver)


class LogInPage(object):
    def __init__(self, web_driver):
       self.driver = web_driver
       self.login_page = self.driver.find_element_by_xpath("//h1[@id='signin_header']")
3) 最后,
test.py
由两部分组成-从
SigninPage
调用方法
enter\u workspace
进入工作区,打开
LogInPage
,然后检查登录是否实际显示:

class TestSlackWorkspace(object):
    def test_workspace(self):
        login = self.signin.enter_workspace()
        assert login.login_page.is_displayed(), "Missing!"
它仍然需要改进,但问题已经解决。多谢各位

test_login.py

def test_login_successful(home):  #Use the home fixture
     assert home.is_displayed("xyz")
import pytest
import os
from selenium import webdriver
from pages import SigninPage

@pytest.fixture(scope='class', autouse=True)
def driver_get(request):
    request.cls.webdriver = webdriver.Firefox(executable_path=os.path.join("web_drivers", "geckodriver"))
    request.cls.signin = SigninPage(request.cls.webdriver)
    yield request.cls.webdriver

    def fin():
        request.cls.webdriver.quit()
    request.addfinalizer(fin)
class SigninPage(object):
    def __init__(self, web_driver):
        self.driver = web_driver
        self.driver.get("https://slack.com/signin")

        self.input_field = self.driver.find_element_by_xpath("//input[@type='text' and @id='domain']")
        self.continue_button = self.driver.find_element_by_xpath("//button[@id='submit_team_domain']")

    def enter_workspace(self):
        self.input_field.send_keys("test")
        self.continue_button.click()
        return LogInPage(self.driver)


class LogInPage(object):
    def __init__(self, web_driver):
       self.driver = web_driver
       self.login_page = self.driver.find_element_by_xpath("//h1[@id='signin_header']")
class TestSlackWorkspace(object):
    def test_workspace(self):
        login = self.signin.enter_workspace()
        assert login.login_page.is_displayed(), "Missing!"