我不想在代码中对被测应用程序的 URL 进行硬编码,而是将其作为命令行中的参数传递。还有用户名、密码。
目前我收到以下错误
错误:找不到文件:
pytest -v -s --html=.\Reports\report.html test_practise.py https://testurl
下面是我的登录功能:
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
最佳答案
有一个内置模块argparse 。查看教程by the link .
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.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
关于python - 如何直接从命令行登录应用程序,即通过在 Pytest 的命令行中提供 URL、用户名、密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58723920/