Java教程

8.selenium - 完成管理员验证码登录测试用例

本文主要是介绍8.selenium - 完成管理员验证码登录测试用例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# coding:utf-8

from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from util import util


class TestAdminLogin(object):

    # 初始化,先登录,网页最大化
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://120.25.209.187:12306/jpress/admin/login')
        self.driver.maximize_window()

    # 测试 admin 登录成功
    def test_admin_login_success(self):
        username = 'admin'
        pwd = '123456'
        # title
        expect = 'JPress后台'
        # 页面刷新
        self.driver.refresh()
        # 获取验证码
        code = util.get_code(self.driver, 'captchaImg')
        print(code)

        self.driver.find_element_by_name('user').send_keys(username)
        self.driver.find_element_by_name('pwd').send_keys(pwd)
        self.driver.find_element_by_name('captcha').send_keys(code)
        self.driver.find_element_by_class_name('btn').click()

        WebDriverWait(self.driver, 5).until(EC.title_is(expect))
        assert self.driver.title == expect

        sleep(3)
        self.driver.quit()

    def test_admin_login_code_error(self):
        username = 'admin'
        pwd = '123456'
        code = '555'
        # alert 弹窗
        expect = '验证码不正确,请重新输入'

        self.driver.find_element_by_name('user').send_keys(username)
        self.driver.find_element_by_name('pwd').send_keys(pwd)
        self.driver.find_element_by_name('captcha').send_keys(code)
        self.driver.find_element_by_class_name('btn').click()

        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == expect
        alert.accept()

        sleep(3)


if __name__ == '__main__':
    test = TestAdminLogin()
    test.test_admin_login_code_error()
    test.test_admin_login_success()

这篇关于8.selenium - 完成管理员验证码登录测试用例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!