【发布时间】:2020-05-02 15:58:44
【问题描述】:
我是 Sellinium/Python 的新手,并且很少进行练习。 我想用 Internet Explorer 运行这个程序,所以在命令提示符下,我给出了以下命令:
py.test -s -v mainTest.py --browser IE
当我运行上面的命令时,它显示错误为:
错误:用法:py.test [options] [file_or_dir] [file_or_dir] [...] py.test:错误:无法识别的参数:--browser IE inifile:无`` 根目录:C:\Users\Radha.Maravajhala\PycharmProjects\RadhaSelenium\Tests
它不是识别 --browser 争论。
如何使用特定浏览器运行我的程序作为 py.test 命令中的参数。请帮忙。
我的代码:
mainTest.py
import os
import pytest
import unittest
from executionEngine.DriverScript import driverScript
from Utilities.Constants import Constants
from selenium import webdriver
class mainTest(driverScript):
def main(self):
print("Main Test started...")
print("Selenium webdriver Version: %s" % (webdriver.__version__))
driver = driverScript('IE')
driver.getbrowserInstance()
m = mainTest()
m.main()
DriverScript.py
import os
import pytest
import unittest
from pip._internal.configuration import Configuration
from selenium import webdriver
from selenium.webdriver import Ie
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.ie.options import Options
from selenium.webdriver.common.keys import Keys
#import selenium.webdriver
from Utilities import Constants
class driverScript():
def __init__(self,browser=None):
if browser is None:
browser = {}
else:
self.browser = browser
print(self.browser)
self.constants = Constants.Constants()
# Method to invoke browser
def getbrowserInstance(self):
# create a new IE session
print("Browser invoke started")
if (self.browser=='IE'):
capabilities = DesiredCapabilities.INTERNETEXPLORER
print(capabilities["platform"])
print(capabilities["browserName"])
driver_location = self.constants.path_ie_driver
os.environ["webdriver.ie.driver"] = driver_location
ie_options = Options()
ie_options.ignore_protected_mode_settings = True
driver = webdriver.Ie(driver_location, options=ie_options)
print("Browser is Invoked")
driver.get("http://www.amazon.co.uk")
driver.quit()
elif (self.browser=='Chrome'):
capabilities = DesiredCapabilities.CHROME
print(capabilities["platform"])
print(capabilities["browserName"])
driver_location = self.constants.path_chrome_driver
os.environ["webdriver.chrome.driver"] = driver_location
driver = webdriver.Chrome(driver_location)
print("Chrome Browser is Invoked")
driver.get("http://www.amazon.co.uk")
driver.quit()
常量.py:
class Constants():
#Driver Locations
path_ie_driver = "C:\\Selenium\\Drivers\\IEDriverServer.exe"
path_chrome_driver = "C:\\Selenium\\Drivers\\chromedriver.exe"
confest.py
class Constants():
import pytest
from executionEngine.DriverScript import driverScript
# Send request variable to the fixture
# Browser value will be set from request.config.getoption (from command
# prompt)
@pytest.yield_fixture(scope="class")
def invoke_browser(request,browser):
wdf = driverScript.getbrowserInstance(browser)
driver = wdf.getbrowserInstance()
# Set class attribute and assign the variable
if request.cls is not None:
request.cls.driver = driver
yield driver
driver.quit()
创建 2 个解析器以从命令提示符获取值
def pytest_addoption(parser):
parser.addoption("--browser")
返回参数值
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
【问题讨论】:
-
您在这里显示的只是一个命令。你的python脚本里面有什么?如果您发布脚本示例会更好。它可以帮助我们更好地理解问题。如果您不传递参数,那么您的脚本是否可以正常运行?让我们知道您指的是哪些文档。我们将尝试检查它以找到如何正确实施它。
-
感谢您的回复迪帕克吉。我已经用我的代码更新了通话。请看一看。
-
我可以看到您在代码中传递了 IE 驱动程序的路径。之后,您不需要将浏览器作为参数传递,因为您在代码中使用了哪个驱动程序。它将启动该特定浏览器。在没有任何参数的情况下执行 python 脚本时是否遇到任何错误?
-
Deepak ji,没有参数,脚本运行良好,网站在 IE 中打开,正如预期的那样。不过,我打算用 Chrome 和 Edge 扩展脚本。当我使用这两个浏览器进行扩展时,我可能需要选择一个特定的浏览器来从命令提示符运行。这就是为什么我试图将浏览器作为参数传递并查看它是如何工作的。脚本适用于单个浏览器。但是当我用 Chrome 和 Edge 扩展它时,我该如何管理它?这就是为什么我试图将它作为参数传递。请指导我如何处理多个浏览器,我可以在运行时选择特定的浏览器。
标签: python internet-explorer browser arguments command