1

I wamt to make my tests more flexible. For example I have a _test_login_ that could be reused with multiple different login credentials. How do I pass them as arguments instead of hard-coding them?

What I have right now:

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")

How can I replace the string literals in the last two lines with something more flexible?

I want to have something like this:

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)

Could you explain how to do this by calling the script as:

pytest main.py *specifiedEmail* *specifiedPW*

CrispJam
  • 159
  • 1
  • 6
  • 16

2 Answers2

2

Try to use sys.arg.

import sys
for arg in sys.argv:
    print(arg)
print ("email:" + sys.argv[2])
print ("password:" + sys.argv[3])

Here is how your code will look like:

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])
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • What if use a different number of pytest related arguments each time? For example sometimes I want to specify `-v` or `-s` – CrispJam Mar 14 '19 at 14:36
  • 1
    if you are sure the username and password are lost 2 params. Then you can use `sys.argv[-2]` for email, `sys.argv[-1]` for password. – supputuri Mar 14 '19 at 14:43
  • what do I type in the console when calling this script then? – CrispJam Mar 14 '19 at 15:49
  • `pytest main.py myemail@email.com mypassword` – supputuri Mar 14 '19 at 15:51
  • 1
    I tried `pytest main.py -v -s -m n --driver Chrome some@email.com somepassword` and got the following eror `file not found: some@email.com` Could the issue be that I'm using the _pytest-selenium_ plugin? – CrispJam Mar 14 '19 at 16:03
  • Hi @CrispJam, did you solve the error that `file not found: some@email.com`? – BrambleXu Apr 26 '20 at 03:21
0

Another way to achive this is by using 'request' in 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"))

In the command prompt you can use -

pytest --email="email@gmail.com" --password="myPassword"
pytest --password="mysecondPassword" --email="email2@gmail.com"
pytest --email="email@gmail.com" 

You get two advantages by this approch.

  1. The command is more user friendly.
  2. You can set a default value more convinently.
Ben
  • 104
  • 5