简体   繁体   English

如何直接从命令行登录应用程序,即在 Pytest 的命令行中提供 URL、用户名、密码

[英]How to login to an application directly from command line, i.e by providing URL, username, password in the command line of a Pytest

Instead of hard coding the URL of the application under test in the code, I would like to pass it as an argument in the command line.我不想在代码中对被测应用程序的 URL 进行硬编码,而是想在命令行中将其作为参数传递。 Also, the username, password.还有用户名、密码。

Currently I get below error目前我得到以下错误

ERROR: file not found:错误:找不到文件:

pytest -v -s --html=.\Reports\report.html test_practise.py https://testurl pytest -v -s --html=.\Reports\report.html test_practise.py https://testurl

Below is my Login function:以下是我的登录 function:

class LoginPage():

    def __init__(self, driver):
        self.driver = driver
        self.url = 'https://test-url'


def go(self):
    self.driver.get(self.url)    


def enter_username(self,text):
    username_xpath = "//input[@id='emailOrUsername']"
    WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, username_xpath))).send_keys(text)


def enter_password(self,text):    
    password_xpath = "//input[@id='pass']"
    WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, password_xpath))).send_keys(text)


def click_login_button(self):    
    login_btn_xpath = "//span[contains(text(),'Login')]"
    self.driver.find_element_by_xpath(login_btn_xpath).click()

def get_login_btn_text(self):
    login_btn_xpath = "//span[contains(text(),'Login')]"
    login_btn_text = self.driver.find_element_by_xpath(login_btn_xpath).text
    return login_btn_text

There is a built-in module argparse .有一个内置模块argparse Check the tutorial by the link .通过链接查看教程。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)

And here is a result: $ python3 prog.py --help usage: prog.py [-h] echo结果如下: $ python3 prog.py --help 用法:prog.py [-h] echo

positional arguments:
  echo

optional arguments:
  -h, --help  show this help message and exit
$ python3 prog.py foo
foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM