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

登陆界面如下

实现代码如下(添加了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")

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值