【问题标题】:Getting error while launching the browser in Pytest by passing arguments in command line通过在命令行中传递参数在 Pytest 中启动浏览器时出错
【发布时间】:2022-06-15 12:03:38
【问题描述】:
#this is conftest files

import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = None


@pytest.fixture(autouse=True)
def setup(request, browser, url):
    global driver
    if browser == "chrome":
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
    elif browser == "firefox":
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    elif browser == "edge":
        driver = webdriver.Edge(EdgeChromiumDriverManager().install())
    driver.get(url)
    driver.maximize_window()
    request.cls.driver = driver
    yield
    driver.close()


# this will get the value of CLI/Hooks
def pytest_addoption(parser):
    parser.addoption("--browser")
    parser.addoption("--url")


# This will return the browser value to setup method
@pytest.fixture(scope="class", autouse=True)
def browser(request):
    return request.config.getoption("--browser", action='store')


# This will return the url value to setup method
@pytest.fixture(scope="class", autouse=True)
def url(request):
    return request.config.getoption("--url", action='store')


#This is the file where all xpaths are defined

import time

#defined xpath for all the page element in that page
class ErrorMessage:

    myAccount_xpath = "//span[text()='My Account']"

    registerAccount_xpath = "//a[text()='Register']"

    submit_xpath = "//input[@type='submit']"

    FnameError_Xapth = "//div[text()='First Name must be between 1 and 32 characters!']"

    LnameError_Xpath = "//div[text()='Last Name must be between 1 and 32 characters!']"

    EmailError_Xpath = "//div[text()='E-Mail Address does not appear to be valid!']"

    TelError_Xpath = "//div[text()='Telephone must be between 3 and 32 characters!']"

    PwdError_Xpath = "//div[text()='Password must be between 4 and 20 characters!']"


    def __init__(self,driver):
        self.driver = driver

#Passsing xpath to the method
    def error_name(self):
        self.driver.find_element_by_xpath(self.myAccount_xpath).click()
        time.sleep(2)
        self.driver.find_element_by_xpath(self.registerAccount_xpath).click()
        time.sleep(2)
        self.driver.find_element_by_xpath(self.submit_xpath).click()
        time.sleep(2)
        fnerror = self.driver.find_element_by_xpath(self.FnameError_Xapth).text
        assert fnerror == "First Name must be between 1 and 32 characters!"
        lnerror = self.driver.find_element_by_xpath(self.LnameError_Xpath).text
        assert lnerror == "Last Name must be between 1 and 32 characters!"
        emailerror = self.driver.find_element_by_xpath(self.EmailError_Xpath).text
        assert emailerror == "E-Mail Address does not appear to be valid!"
        telerror = self.driver.find_element_by_xpath(self.TelError_Xpath).text
        assert telerror == "Telephone must be between 3 and 32 characters!"
        pwderror = self.driver.find_element_by_xpath(self.PwdError_Xpath).text
        assert pwderror == "Password must be between 4 and 20 characters!"

#这个文件是定义测试用例的地方

import pytest

from pages.loginfail import ErrorMessage


@pytest.mark.usefixtures("setup")
class LoginFail:

    def __init__(self):
        self.error = None

#从 xpath 类导入方法来运行测试用例

    # @pytest.fixture(autouse=True)
    def test_ra_003(self):
        self.error = ErrorMessage(self.driver)
        self.error.error_name()




#I am passing below codes in command line 
python -m pytest -m test_LoginFail.py --browser firefox --url "https://demo.opencart.com/"


And getting below errors while executing
#ERROR: usage: __main__.py [options] [file_or_dir] [file_or_dir] [...]
#__main__.py: error: unrecognized arguments: --browser --url https://demo.opencart.com/
  #inifile: None
  #rootdir: F:\PycharmPrjects\POMSelenium\testscripts

如果我使用单个浏览器启动,相同的代码将正常工作。请帮我调试这个问题。我尝试了谷歌,但我没有得到任何解决方案。在传递 --browser 和 --url 参数时,我遇到了错误。没有得到我在这里做错了什么。

【问题讨论】:

    标签: python selenium pytest


    【解决方案1】:

    我在 selenium 4.2.0、pytest 7.1.2 中也遇到了同样的问题

    def pytest_addoption(parser):
        parser.addoption(
            "--browser_name", action="store", default="chrome"
        )
    
    
    @pytest.fixture(scope="class")
    def setup(request):
        global driver
        browser_name = request.config.getoption("browser_name")
        if browser_name == "chrome":
            driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
        elif browser_name == "firefox":
            print("Firefox driver")
        elif browser_name == "IE":
            print("IE driver")
        driver.get("https://rahulshettyacademy.com/angularpractice/")
        driver.maximize_window()
    
        request.cls.driver = driver
        yield
        driver.close()
    

    enter image description here

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多