zoukankan      html  css  js  c++  java
  • 初识unitest框架

    1、借助IED录制脚本

    2、导出脚本,选择用Python语言

    将脚本导出,保存为 baidu.py ,通过 python IDLE 编辑器打开

    引入unittest框架解释,见代码的的注释

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    from selenium.common.exceptions import NoAlertPresentException
    import unittest, time, re #引入unittest框架
    
    class Baidu(unittest.TestCase):
        #setUp 用于设置初始化的部分,在测试用例执行前,这个方法中的函数将先被调用。这里将浏览器的调用和 URL 的访问放到初始化部分
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "http://start.firefoxchina.cn/"
            self.verificationErrors = [] #脚本运行时,错误的信息将被打印到这个列表中
            self.accept_next_alert = True
        
    #test_baidu 中放置的就是我们的测试脚本了,因为我们执行的脚本就在这里
        def test_baidu(self): 
            driver = self.driver
            driver.get(self.base_url + "/")
            driver.find_element_by_id("search-key").clear()
            driver.find_element_by_id("search-key").send_keys("selenium webdriver")
            driver.find_element_by_id("search-submit").click()
        
    #is_element_present 函数用来查找页面元素是否存在,在这里用处不大,通常删除。因为判断页面元素是否存在一般都加在 testcase 中
        def is_element_present(self, how, what):
            try: self.driver.find_element(by=how, value=what)
            except NoSuchElementException as e: return False
            return True
        
       #对异常弹出窗口的处理 
        def is_alert_present(self):
            try: self.driver.switch_to_alert()
            except NoAlertPresentException as e: return False
            return True
        
        #关闭警告和对得到文本框的处理
        def close_alert_and_get_its_text(self):
            try:
                alert = self.driver.switch_to_alert()
                alert_text = alert.text
                if self.accept_next_alert:
                    alert.accept()
                else:
                    alert.dismiss()
                return alert_text
            finally: self.accept_next_alert = True
            
        #tearDown 方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出浏览器等
        def tearDown(self):
            self.driver.quit()
            #这个是难点,对前面 verificationErrors 方法获得的列表进行比较;如查verificationErrors 的列表不为空,输出列表中的报错信息。
    #而且,这个东西,也可以将来被你自己更好的调用和使用,根据自己的需要写入你希望的信息 self.assertEqual([], self.verificationErrors) #unitest.main()函数用来测试 类似以 test 开头的测试用例 if __name__ == "__main__": unittest.main()

      

  • 相关阅读:
    js常用方法收集
    Jquery的常用使用方法
    jQuery css()选择器使用说明
    解决IE6,边框问题
    HTML问题集锦及笔记
    我的第一个chrome扩展(3)——继续读样例
    我的第一个chrome扩展(0)——目标
    我的第一个chrome扩展(2)——基本知识
    我的第一个chrome扩展(1)——读样例,实现时钟
    2の奇妙用法
  • 原文地址:https://www.cnblogs.com/101718qiong/p/7411928.html
Copyright © 2011-2022 走看看