在Selenium python上面对Pytest中的这个错误

h7appiyu  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(128)
import time
import self as self
from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from setuptools import setup
@mark.execute
class First_Tests:

    def test_first(self, setup):
        driver = setup['driver']
        browser = setup['browser']
        driver.get("https://shuftipro.com/")
        driver.maximize_window()

    def header_test(self, setup):
        driver = setup['driver']

        # Click on solution in header
        solution = driver.find_element(By.ID, "menu-item-72751")
        solution.click()
        if driver.current_url == "https://shuftipro.com/solutions/":
            print("land on solution page.")
        else:
            print("land on wrong page.")
obj = First_Tests()
obj.test_first(self, setup)
obj.header_test(self, setup)

如果我从参数中删除“self”并运行程序,它会显示错误:test_first()接受1个位置参数,但实际上给出了3个

f4t66c6m

f4t66c6m1#

其中一个更简单的方法是使用selenium中的webdriver
driver = webdriver.Chrome()
并除去了

driver = setup['driver']
browser = setup['browser']

然后去掉setup作为参数。最后的代码看起来像这样:

from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
@mark.execute
class First_Tests:

    def test_first(self):
        driver.get("https://shuftipro.com/")
        driver.maximize_window()

    def header_test(self):
        # Click on solution in header
        solution = driver.find_element(By.ID, "menu-item-72751")
        solution.click()
        if driver.current_url == "https://shuftipro.com/solutions/":
            print("land on solution page.")
        else:
            print("land on wrong page.")

obj = First_Tests()
obj.test_first()
obj.header_test()

相关问题