【问题标题】:How to pass arguments to Selenium test functions in Pytest?如何将参数传递给 Pytest 中的 Selenium 测试函数?
【发布时间】:2019-03-14 14:14:34
【问题描述】:

我想让我的测试更加灵活。例如,我有一个 _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*

【问题讨论】:

    标签: python selenium pytest pytest-selenium


    【解决方案1】:

    尝试使用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 相关参数怎么办?例如有时我想指定-v-s
    • 如果您确定用户名和密码丢失了 2 个参数。然后您可以使用sys.argv[-2] 输入电子邮件,sys.argv[-1] 输入密码。
    • 那么调用这个脚本时我在控制台输入什么?
    • pytest main.py myemail@email.com mypassword
    • 我尝试了pytest main.py -v -s -m n --driver Chrome some@email.com somepassword 并得到了以下错误file not found: some@email.com 可能是我使用了 pytest-selenium 插件吗?
    【解决方案2】:

    实现此目的的另一种方法是在 pytest 中使用“请求”。

    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" 
    
    

    这种方法有两个好处。

    1. 该命令对用户更加友好。
    2. 您可以更方便地设置默认值。

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2020-07-02
      相关资源
      最近更新 更多