zoukankan      html  css  js  c++  java
  • 用unittest框架编写登录博客园的案例(登陆成功和登陆失败)

    使用参数化的方式来实现

    代码:

    #coding:utf-8
    from selenium import webdriver
    import unittest,time
    def login(driver, username, password):#此处的driver是个形参,如果不在此处定义就不可以使用

      #点击登录的操作
        driver.find_element_by_link_text("登录").click()
    #输入用户名
    driver.find_element_by_css_selector("#input1").send_keys(username)
    time.sleep(2)
    #输入密码
    driver.find_element_by_css_selector("#input2").send_keys(password)
    # 点击登录
    time.sleep(2)
    driver.find_element_by_id("signin").click()

    class Test_Log(unittest.TestCase):
    def setUp(self):
    url="https://www.cnblogs.com/"
    self.option = webdriver.ChromeOptions()
    self.option.add_argument('lang=zh_CN.UTF-8')
    self.option.add_argument('User-Agent:"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"')
    self.driver = webdriver.Chrome(chrome_options=self.option)
    self.driver.get(url)
    def test_1log(self): #登录失败
    #调用login函数,并向它传参
    login(driver=self.driver,username=u"uname",password=u"falsepwd")
    time.sleep(2)
    #实际结果
    actual_result=self.driver.find_element_by_css_selector("#tip_btn").text
    print("实际结果为:"+actual_result)
    time.sleep(2)
    #预期结果
    expected_result = u"用户名或密码错误 联系 contact@cnblogs.com"
    print("预期结果为:" + expected_result)
    time.sleep(2)
    self.assertEqual(actual_result,expected_result,msg="实际结果(%s)与预期结果(%s)不一致"%(actual_result,expected_result))

    def test_2log(self): #登录成功
    #调用login函数,并向它传参
    login(driver=self.driver,username=u"uname",password=u"truepwd")

    time.sleep(2)
    #实际结果
    actual_result=self.driver.find_element_by_css_selector("#span_userinfo>a:nth-child(1)").text
    print("实际结果为:"+actual_result)
    #预期结果
    expected_result="女林"
    print("预期结果为:"+expected_result)
    self.assertEqual(actual_result,expected_result,msg="实际结果(%s)与预期结果(%s)不一致"%(actual_result,expected_result))

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

    if __name__=="__main__":
    unittest.main()

    结果:

    
    
  • 相关阅读:
    springBoot jpa 分页
    springBoot jpa 表单关联查询
    springBoot 登录拦截器
    SpringBoot 封装返回类以及session 添加获取
    SpringBoot 数据库操作 增删改查
    IDEA SpringBoot +thymeleaf配置
    IDEA Spring Boot 项目创建
    php判断手机段登录,以及phpcms手机PC双模板调用
    简单爬虫,查博客浏览量
    [51nod1357]密码锁 暨 GDOI2018d1t2
  • 原文地址:https://www.cnblogs.com/linbao/p/7601316.html
Copyright © 2011-2022 走看看