zoukankan      html  css  js  c++  java
  • UI自动化之异常与截图处理

    对操作不成功时,希望能够继续执行其他操作,或者是,希望操作不成功时,能够写日志记录

    目录

    1、常见异常

    2、截图处理

    1、常见异常

    1.NoSuchElementException:没有找到元素

    2.NoSuchFrameException:没有找到iframe

    3.NoSuchWindowException:没找到窗口句柄handle

    4.NoSuchAttributeException:属性错误

    5.NoAlertPresentException:没找到alert弹出框

    6.ElementNotVisibleException:元素不可见

    7.ElementNotSelectableException:元素没有被选中

    8.TimeoutException:查找元素超时

     

    2、截图处理

    第一步:定义一个截图装饰器

    # coding:utf-8
    from selenium import webdriver
    class Screen(object):
        u'''返个应该截图功能的装饰器'''
        def __init__(self, driver):
            self.driver = driver
    
        def __call__(self, f):
            def inner(*args):
                try:
                    return f(*args)
                except:
                    import time
                    nowTime =time.strftime("%Y_%m_%d_%H_%M_%S")
                    self.driver.get_screenshot_as_file('%s.jpg' %nowTime)
                    raise
            return inner
    

     第二步:调用截图功能的装饰器

    import unittest
    class Test(unittest.TestCase):
        driver = webdriver.Firefox() # 全局参数 driver
        def setUp(self):
            self.driver.get("https://www.baidu.com")
        @Screen(driver)
        def test01(self):
            u'''返个是失败的案例'''
            self.driver.find_element_by_id("11kw").send_keys("python")
            self.driver.find_element_by_id("su").click()
        @Screen(driver)
        def test_02(self):
            u'''返个是通过的案例'''
            self.driver.find_element_by_id("kw").send_keys("yoyo")
            self.driver.find_element_by_id("su").click()
        def tearDown(self):
            self.driver.quit()
    if __name__ == "__main__":
        unittest.main()
    

      

  • 相关阅读:
    YOLOV2相对于YOLOV1的改进
    在训练过程中loss出现NaN的原因以及可以采取的方法
    出现梯度消失和梯度爆炸的原因及解决方案
    Batch Normalization 原理
    几种激活函数的对比(二)
    几种激活函数对比(一)
    Leetcode 830. Positions of Large Groups
    Leetcode 985. Sum of Even Numbers After Queries
    python中的赋值与拷贝(浅拷贝与深拷贝)
    Leetcode 665. Non-decreasing Array
  • 原文地址:https://www.cnblogs.com/weizhideweilai/p/9716080.html
Copyright © 2011-2022 走看看