selenium--unittest 框架/selenium--常见异常

selenium常见异常

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, ElementNotVisibleException, NoSuchElementException

1.NoSuchElementException:没有找到元素

2.NoSuchFrameException:没有找到iframe

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

4.NoSuchAttributeException:属性错误

5.NoAlertPresentException:没找到alert弹出框

6.lementNotVisibleException:元素不可见

7.ElementNotSelectableException:元素没有被选中

8.TimeoutException:查找元素超时


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
import unittest, time, re

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_baidu(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").send_keys("selenium webdriver")
        driver.find_element_by_id("su").click()
        driver.close()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, 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

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

框架分析

import  unittest  

相想使用unittest框架,首先要引入unittest 包,这个不多解释。

class Baidu(unittest.TestCase):

Baidu类继承unittest.TestCase 类,从TestCase类继承是告诉unittest模块的方式,这是一个测试案例。

扫描二维码关注公众号,回复: 1840276 查看本文章
def setUp(self):
  self.driver = webdriver.Firefox()
  self.base_url = "http://www.baidu.com/"

setUp 用于设置初始化的部分,在测试用例执行前,这个方法中的函数将先被调用。这里将浏览器的调用和URL的访问放到初始化部分。

self.verificationErrors = []

脚本运行时,错误的信息将被打印到这个列表中。

self.accept_next_alert = True

是否继续接受下一下警告(字面意思,没找到解释!)

 def test_baidu(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").send_keys("selenium webdriver")
        driver.find_element_by_id("su").click()

test_baidu中放置的就是我们的测试脚本了,这部分我们并不陌生;因为我们执行的脚本就在这里。

def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

is_element_present函数用来查找页面元素是否存在,在这里用处不大,通常删除。

因为判断页面元素是否存在一般都加在testcase中。

def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, 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

关闭警告和对得到文本框的处理,如果不熟悉python的异常处理和if 语句的话,请去补基础知识,这里不多解释。

def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

tearDown 方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出浏览器等。

self.assertEqual([], self.verificationErrors)是个难点,对前面verificationErrors方法获得的列表进行比较;如查verificationErrors的列表不为空,输出列表中的报错信息。

而且,这个东西,也可以将来被你自己更好的调用和使用,根据自己的需要写入你希望的信息。(rabbit 告诉我的)

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

unitest.main()函数用来测试 类中以test开头的测试用例

执行结果

这样一一分析下来,我们对unittest 框架有了初步的了解。运行脚本,因为引入了unittest 框架,所以控制台输出了脚本执行情况的信息。

>>> ========================= RESTART ================================
>>> 
.
----------------------------------------------------------------------
Ran 1 test in 10.656s

OK
>>> 

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/80851668