Python+selenium登录断言

from selenium import webdriver
from time import sleep
import unittest

class TestLogin(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.implicitly_wait(3) # 隐性等待时间为3秒
self.driver.get(“http://localhost/phpwind/index.php”)
sleep(2)

def test_login(self):
    driver=self.driver
    #先清空用户名输入框
    driver.find_element_by_id('nav_pwuser').clear()
    #输入用户名
    driver.find_element_by_id('nav_pwuser').send_keys('test1')
    #清空密码输入框
    driver.find_element_by_name('pwpwd').clear()
    #输入密码
    driver.find_element_by_name('pwpwd').send_keys('123456')
    sleep(1)
    driver.find_element_by_name('head_login').click()
    sleep(2)
   #第一种断码方法
    a=driver.current_url  # current_url 方法可以得到当前页面的URL
    b="http://localhost/phpwind/index.php"
    self.assertEqual(a,b,msg='123sdfgh')
    3第二种if断言
    if (a == b):
        print(u"测试成功,结果和预期结果匹配!")
    else:
      print(u"测试失败,结果和预期结果不一致!")

    #第三种通过try抛出异常进行断言判断
    try:
        assert 'http://localhost/phpwind/index.php' == a
        print("测试成功")
    except Exception as e:
        print("测试失败", format(e))

def tearDown(self):
    self.driver.quit()

if name==“main”:
unittest.main()
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41793209/article/details/88806828