如何将参数传递给 Pytest 中的 Selenium 测试函数?
How to pass arguments to Selenium test functions in Pytest?
我想让我的测试更灵活。例如,我有一个 _test_login_ 可以与多个不同的登录凭据一起重复使用。我如何将它们作为参数传递而不是对它们进行硬编码?
我现在拥有的:
from selenium import webdriver
import pytest
def test_login():
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys("someLogin")
pwBox.send_keys("somePW")
如何用更灵活的东西替换最后两行中的字符串文字?
我想要这样的东西:
from selenium import webdriver
import pytest
def test_login(specifiedEmail, specifiedPW):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(specifiedEmail)
pwBox.send_keys(specificedPW)
您能否通过调用脚本来解释如何执行此操作:
pytest main.py *specifiedEmail* *specifiedPW*
尝试使用sys.arg
.
import sys
for arg in sys.argv:
print(arg)
print ("email:" + sys.argv[2])
print ("password:" + sys.argv[3])
代码如下所示:
from selenium import webdriver
import pytest
import sys
def test_login(specifiedEmail, specifiedPW):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(sys.argv[2])
pwBox.send_keys(sys.argv[3])
实现此目的的另一种方法是在 pytest 中使用 'request'。
def pytest_addoption(parser):
parser.addoption("--email", action="store", default="myemail@email.com", help="Your email here")
parser.addoption("--password", action="store", default="strongpassword", help="your password")
from selenium import webdriver
import pytest
def test_login(request):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(request.config.getoption("--email"))
pwBox.send_keys(request.config.getoption("--password"))
在命令提示符下你可以使用-
pytest --email="email@gmail.com" --password="myPassword"
pytest --password="mysecondPassword" --email="email2@gmail.com"
pytest --email="email@gmail.com"
通过这种方法,你有两个好处。
- 命令更加人性化。
- 您可以更方便地设置默认值。
我想让我的测试更灵活。例如,我有一个 _test_login_ 可以与多个不同的登录凭据一起重复使用。我如何将它们作为参数传递而不是对它们进行硬编码?
我现在拥有的:
from selenium import webdriver
import pytest
def test_login():
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys("someLogin")
pwBox.send_keys("somePW")
如何用更灵活的东西替换最后两行中的字符串文字?
我想要这样的东西:
from selenium import webdriver
import pytest
def test_login(specifiedEmail, specifiedPW):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(specifiedEmail)
pwBox.send_keys(specificedPW)
您能否通过调用脚本来解释如何执行此操作:
pytest main.py *specifiedEmail* *specifiedPW*
尝试使用sys.arg
.
import sys
for arg in sys.argv:
print(arg)
print ("email:" + sys.argv[2])
print ("password:" + sys.argv[3])
代码如下所示:
from selenium import webdriver
import pytest
import sys
def test_login(specifiedEmail, specifiedPW):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(sys.argv[2])
pwBox.send_keys(sys.argv[3])
实现此目的的另一种方法是在 pytest 中使用 'request'。
def pytest_addoption(parser):
parser.addoption("--email", action="store", default="myemail@email.com", help="Your email here")
parser.addoption("--password", action="store", default="strongpassword", help="your password")
from selenium import webdriver
import pytest
def test_login(request):
driver = webdriver.Chrome()
driver.get("https://semantic-ui.com/examples/login.html")
emailBox = driver.find_element_by_name("email")
pwBox = driver.find_element_by_name("password")
emailBox.send_keys(request.config.getoption("--email"))
pwBox.send_keys(request.config.getoption("--password"))
在命令提示符下你可以使用-
pytest --email="email@gmail.com" --password="myPassword"
pytest --password="mysecondPassword" --email="email2@gmail.com"
pytest --email="email@gmail.com"
通过这种方法,你有两个好处。
- 命令更加人性化。
- 您可以更方便地设置默认值。