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

2024-04-19 07:41:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我不想在代码中硬编码被测应用程序的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

Tags: texthttpstestselfurldefhtmldriver
1条回答
网友
1楼 · 发布于 2024-04-19 07:41:30

有一个内置模块argparse。查看教程by the link

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

结果如下: $python3 prog.py帮助 用法:prog.py[-h]echo

positional arguments:
  echo

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

相关问题 更多 >