初学者在这里!我有一些基本功能,其中包括一些我想重用的硒代码。但是,我不确定如何合并此代码,因为我的驱动程序是在固定装置内初始化的。我该如何解决这个问题?
这是我的conftest.py代码
@pytest.fixture(scope="class")
def setup(request):
options = Options()
driver = webdriver.Chrome(options=options)
options.add_experimental_option("detach", True)
request.cls.driver = driver
driver.get(url)
driver.maximize_window()
pyautogui.typewrite(username)
pyautogui.press('tab')
pyautogui.typewrite(password)
pyautogui.press('enter')
time.sleep(3)
yield
driver.quit()
@pytest.mark.usefixtures("setup")
class BaseTest:
pass
这是我的 test.py 代码的一个小样本
class TestLandingPage(BaseTest):
def test_profilenav(self):
profile = self.driver.find_element(by=By.XPATH,
value="/html/body/div[1]/div/div[1]/div[2]/div[3]/div[3]/div[1]/div/a[1]")
if profile.is_displayed():
profileDisplayed = "Profile Icon is displayed"
print(profileDisplayed)
buttonDisplayed.append(profileDisplayed)
else:
profileNotDisplayed = "Profile Icon is not displayed"
print(profileNotDisplayed)
buttonNotDisplayed.append(profileNotDisplayed)
if profile.is_enabled():
profileEnabled = "Profile button is enabled"
print(profileEnabled)
buttonEnabled.append(profileEnabled)
else:
profileDisabled = "Profile button is disabled"
print(profileDisabled)
buttonDisabled.append(profileDisabled)
profile.click()
profileClickAction = "Profile passed"
print(profileClickAction)
clickActionPassed.append(profileClickAction)
returnToHome()
profileIcon()
这是我想在我的 pytest 代码中使用的函数,但不知道放在哪里/如何适应
def returnToHome():
time.sleep(2)
logo = driver.find_element(by=By.CSS_SELECTOR, value='[alt="banner"]')
if logo.is_displayed():
print("Logo return to home is displayed")
else:
print("Logo return to home is not displayed")
logo.click()
time.sleep(2)
def profileIcon():
profileIcon = driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[1]/div[2]/div[3]")
if profileIcon.is_displayed():
print("Profile Icon button is displayed")
else:
print("Profile Icon button is not displayed")
profileIcon.click()
print("Profile Icon passed")
time.sleep(1)
您也许可以使用 https://github.com/seleniumbase/SeleniumBase 作为您的
pytest
+ selenium
框架。有页面对象模型的示例,其中 driver
(通过 sb.driver
)被传递给需要它的方法。例如。安装 pytest
后,下面的脚本将与 seleniumbase
一起运行。
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class LoginPage:
def login_to_swag_labs(self, sb, username):
sb.open("https://www.saucedemo.com")
sb.type("#user-name", username)
sb.type("#password", "secret_sauce")
sb.click('input[type="submit"]')
class MyTests(BaseCase):
def test_swag_labs_login(self):
LoginPage().login_to_swag_labs(self, "standard_user")
self.assert_element("div.inventory_list")
self.assert_element('div:contains("Sauce Labs Backpack")')
此外,还有一种
sb
夹具格式。示例:
import pytest
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
@pytest.mark.usefixtures("sb")
class Test_UseFixtures:
def test_usefixtures_on_class(self):
sb = self.sb
sb.open("seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
sb.assert_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.highlight("#image1")
sb.click_link("Sign out")
sb.assert_text("signed out", "#top_message")
将该固定格式与页面对象模型相结合后,可能看起来像这样:
import pytest
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class LoginPage:
def login_to_swag_labs(self, sb, username):
sb.open("https://www.saucedemo.com")
sb.type("#user-name", username)
sb.type("#password", "secret_sauce")
sb.click('input[type="submit"]')
@pytest.mark.usefixtures("sb")
class Test_UseFixtures:
def test_usefixtures_on_class(self):
sb = self.sb
LoginPage().login_to_swag_labs(sb, "standard_user")
sb.assert_element("div.inventory_list")
sb.assert_element('div:contains("Sauce Labs Backpack")')