python+selenium实现自动化登陆测试

python+selenium实现自动化登陆测试,第1张

登陆界面如下

实现代码如下(添加了Mac端的通知处理)

import os
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

username = ("name", "phonenum")
psw = ("name", "password")
loginbutton = ("id", "login")
message = ("id", "message")
print("message", message)
driver = webdriver.Chrome()
driver.maximize_window()
c = 0  # 记录有多少个测试通过


def find(locator):
    """driver为返回的实例,3秒为超时总时长,循环查询的间隙时间默认0.5秒,lambda表达式创造匿名函数x为传入参数,':'后边为函数体
       WebDriverWait为显示等待,直到until里边定位到元素"""
    element = WebDriverWait(driver, 3).until(lambda x: x.find_element(*locator))
    return element


def send(locator, text):
    find(locator).send_keys(text)


def click(locator):
    find(locator).click()


def show_notification(title, text):
    os.system("""
              osascript -e 'display notification "{}" with title "{}"'
              """.format(text, title))


def login_right(c):
    driver.get("http://localhost:8080/")
    send(username, "15196080968")
    send(psw, "123")
    click(loginbutton)
    result = find(message).text
    exp1 = "登录成功"
    driver.delete_all_cookies()
    if result == exp1:
        print("test1:" + result)
        return c + 1
    else:
        print("test1:测试不通过")
        return c


def login_none(c):
    driver.get("http://localhost:8080/")
    click(loginbutton)
    result = find(message).text
    exp2 = "请输入手机号码"
    driver.delete_all_cookies()
    if result == exp2:
        print("test2:" + result)
        return c + 1
    else:
        print("test2:测试不通过")
        return c


def login_withoutname(c):
    driver.get("http://localhost:8080/")
    send(psw, "123")
    click(loginbutton)
    result = find(message).text
    exp3 = "请输入手机号码"
    driver.delete_all_cookies()
    if result == exp3:
        print("test3:" + result)
        return c + 1
    else:
        print("test3:测试不通过")
        return c


def login_withoutpsw(c):
    driver.get("http://localhost:8080/")
    send(username, "32532432")
    click(loginbutton)
    result = find(message).text
    exp4 = "请输入密码"
    driver.delete_all_cookies()
    if result == exp4:
        print("test4:" + result)
        return c + 1
    else:
        print("test4:测试不通过")
        return c


def login_wrong(c):
    driver.get("http://localhost:8080/")
    send(username, "32532432")
    send(psw, "12412")
    click(loginbutton)
    result = find(message).text
    exp5 = "账号不存在"
    driver.delete_all_cookies()
    if result == exp5:
        print("test5:" + result)
        return c + 1
    else:
        print("test5:测试不通过")
        return c


if __name__ == '__main__':
    c = login_right(c)
    c = login_none(c)
    c = login_withoutname(c)
    c = login_withoutpsw(c)
    c = login_wrong(c)
    driver.quit()
    show_notification("LoginTest", str(c) + " test is passed")

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/570827.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存