在Selenium Python中Pytest遇到此错误。

huangapple go评论70阅读模式
英文:

Facing this error in Pytest on selenium python

问题

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(setup)
obj.header_test(setup)

如果我从参数中删除了"self"并运行程序,它会显示错误,test_first() 接受1个位置参数,但提供了3个。

英文:
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)

If I remove the "self" from parameter and run the program it showing me error that, test_first() takes 1 positional arguments but 3 were given

答案1

得分: 1

以下是翻译好的部分:

"one of easier way would be to use webdriver from selenium"
"使用Selenium中的webdriver是更简单的方法之一"

"driver = webdriver.Chrome()"
"driver = webdriver.Chrome()"

"and remove"
"并移除"

"driver = setup['driver']"
"driver = setup['driver']"

"browser = setup['browser']"
"browser = setup['browser']"

"and then get rid of setup as parameter."
"然后不再需要将setup作为参数传递。"

"The final code will look something like this:"
"最终的代码将类似于这样:"

"from pytest import mark"
"from pytest import mark"

"from selenium import webdriver"
"from selenium import webdriver"

"from selenium.webdriver.common.by import By"
"from selenium.webdriver.common.by import By"

"driver = webdriver.Chrome()"
"driver = webdriver.Chrome()"

"@mark.execute"
"@mark.execute"

"class First_Tests:"
"class First_Tests:"

"def test_first():"
"def test_first():"

"driver.get('https://shuftipro.com/')"
"driver.get('https://shuftipro.com/')"

"driver.maximize_window()"
"driver.maximize_window()"

"def header_test():"
"def header_test():"

"solution = driver.find_element(By.ID, 'menu-item-72751')"
"solution = driver.find_element(By.ID, 'menu-item-72751')"

"solution.click()"
"solution.click()"

"if driver.current_url == 'https://shuftipro.com/solutions/':"
"if driver.current_url == 'https://shuftipro.com/solutions/':"

"print('land on solution page.')"
"print('进入到解决方案页面。')"

"print('land on wrong page.')"
"print('进入到错误的页面。')"

"obj = First_Tests()"
"obj = First_Tests()"

"obj.test_first()"
"obj.test_first()"

"obj.header_test()"
"obj.header_test()"

英文:

one of easier way would be to use webdriver from selenium

driver = webdriver.Chrome()

and remove

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

and then get rid of setup as parameter.
The final code will look something like this:

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()

huangapple
  • 本文由 发表于 2023年2月6日 21:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定