Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

python - Unable to use page instances on testfile using pytest fixture

Below code is based on Page Object pattern using pytest and fixtures, I have created a conftext.py file for initilizing the browser, have created a pages and a test file

This is a conftext file

"""conftest.py file"""
import pytest
from selenium import webdriver

@pytest.fixture(params=["chrome"], scope='class')
def init_driver(request):
    if request.param == "chrome":
        web_driver = webdriver.Chrome(executable_path="...")
    if request.param == "firefox":
        web_driver = webdriver.Firefox(executable_path="...")
    request.cls.driver = web_driver
    yield
    web_driver.close()

This is test_Base.py file

"""test_Base.py file"""
import pytest
@pytest.mark.usefixtures("init_driver")
class BaseTest:
    pass    

This is Some Page file

"""ABCPage.py file"""
from Pages.BasePage import BasePage

class ABCPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)

    def some_function1(self):
        ...
        ...

    def some_function2(self):
        ...
        ...

This is a test file:

"""test_ABC.py file"""
import pytest
Tests.test_Base import BaseTest
Pages.ABCPage import ABCPage
Pages.DEFPage import DEFPage

class Test_ABC(BaseTest):
    @pytest.fixture(scope='module')
    def instances(self):
        abcPage = ABCPage(self.driver)
        defPage = DEFPage(self.driver)
        return abcPage, defPage
    
    def test_verify_something(instances):
        abcPage.some_function1()
    
    def test_verify_something_in_defpage(instances):
        defPage.some_function11()

Getting a below error on compilation and also at run time:

  """Getting ERROR"""
  def test_verify_something(instances):
  abcPage.some_function1()
  E NameError: name 'abcPage' is not defined
  Also 
  E AttributeError: 'Test_ABC' object has no attribute 'some_function1'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...