python - 如何通过特定的浏览器作为参数。它的错误:py.test:错误:无法识别的参数:--browser IE
问题描述
我是 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: None`` rootdir: C:\Users\Radha.Maravajhala\ PycharmProjects\RadhaSelenium\Tests
它没有识别 --browser 争论。
我如何使用特定的浏览器运行我的程序作为 py.test 命令中的参数。请帮忙。
我的代码:
主测试.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"
会议.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 脚本的参数有一些误解。
如果要在多个浏览器上运行脚本,则需要在代码中使用多个 Web 驱动程序。在您上面的代码中,我可以看到您只使用 IE 网络驱动程序。
另一件事,我没有看到您在哪里检查和使用从命令行传递的参数。
我建议您使用 IF..elif 条件并尝试检查其中的命令行参数。根据参数的值,您可以尝试为任何特定的 Web 浏览器执行代码段。
例子:
#!/usr/bin/python
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
if __name__ == "__main__":
main(sys.argv[1:])
参考:
推荐阅读
- ruby-on-rails - 如何使用 active_record gem 使用相同的适配器(postgres)建立与多个数据库的连接?
- php - 无法搜索数组值
- java - 在 30 秒循环中每 5 秒打印一次当前日期
- laravel - Laravel Blade 视图未显示从控制器传递的数据
- maven - 如何使用 pom.xml 在 JAVA 项目的 ci/cd 管道中配置 selenium 记录播放测试?
- javascript - JavaScript 永久链接 URL
- java - android 9上相机“1”的Sinch VideoCaptureThread问题
- python-3.x - 我收到错误消息:ValueError: no enough values to unpack (expected 2, got 1)。可能是什么问题呢?
- sms - 如何在使用可变字母数字发件人 ID 时发送给多个收件人(没有循环)?
- node.js - 如何在 nodejs 中使用“npm mssql”检索多个记录集?