| | 1 | == selenium访问资源timeout时应对方案 == |
| | 2 | |
| | 3 | 当我们selenium访问资源时,可能会网络超时,这个网络超时的原因可能是资源中的依赖的某一资源无法访问,或网络异常。 |
| | 4 | [[BR]] |
| | 5 | 还有可能是瞬间无法访问,或selenim不够稳定引起的。如果是瞬间无法访问或selenim缺乏健壮性引起的,我们是可以避免因为网络超时引起的测试失败。 |
| | 6 | [[BR]] |
| | 7 | 为了重现网络超时,我们将超时时间设置短一点,设置5秒,如下: |
| | 8 | {{{ |
| | 9 | ...... |
| | 10 | self.driver.set_page_load_timeout(5) |
| | 11 | ...... |
| | 12 | }}} |
| | 13 | 接下来,我们去访问版块页 |
| | 14 | {{{ |
| | 15 | ...... |
| | 16 | driver = self.driver |
| | 17 | driver.get(self.base_url + self.config["test.forum"]) |
| | 18 | #断言主题管理块 |
| | 19 | self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "div.forum_admin")) |
| | 20 | ...... |
| | 21 | |
| | 22 | }}} |
| | 23 | 测试失败,并抛出如下异常信息 |
| | 24 | {{{ |
| | 25 | Traceback (most recent call last): |
| | 26 | File "admin_menu_forum.py", line 24, in test_admin_menu |
| | 27 | driver.get(self.base_url + self.config["test.forum"]) |
| | 28 | File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 177, |
| | 29 | t |
| | 30 | self.execute(Command.GET, {'url': url}) |
| | 31 | File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 165, |
| | 32 | ecute |
| | 33 | self.error_handler.check_response(response) |
| | 34 | File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 1 |
| | 35 | check_response |
| | 36 | raise exception_class(message, screen, stacktrace) |
| | 37 | TimeoutException: Message: u'Timed out waiting for page load.' ; Stacktrace: |
| | 38 | at FirefoxDriver.prototype.get/< (file:///c:/users/pc/appdata/local/temp/tmpnfrz_d/extensions/fxdriver@googlec |
| | 39 | m/components/driver_component.js:8767) |
| | 40 | at WebLoadingListener/e (file:///c:/users/pc/appdata/local/temp/tmpnfrz_d/extensions/fxdriver@googlecode.com/c |
| | 41 | nts/driver_component.js:3330) |
| | 42 | at WebLoadingListener/< (file:///c:/users/pc/appdata/local/temp/tmpnfrz_d/extensions/fxdriver@googlecode.com/c |
| | 43 | nts/driver_component.js:3337) |
| | 44 | at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/pc/appdata/local/temp/tmpnfrz_d/extensions/f |
| | 45 | r@googlecode.com/components/driver_component.js:396) |
| | 46 | }}} |
| | 47 | 一般我们将超时时间设为20秒,如果超时,我们可以让selenium去重试, |
| | 48 | {{{ |
| | 49 | ....... |
| | 50 | def retry_when_timeout(driver, url, times = 3): |
| | 51 | u''' 访问超时,重试,默认重试3次 ''' |
| | 52 | for i in range(0, times): |
| | 53 | try: |
| | 54 | print "access %s, times %s" % (url, i+1) |
| | 55 | driver.get(url) |
| | 56 | except TimeoutException, e: |
| | 57 | continue |
| | 58 | else: |
| | 59 | break |
| | 60 | }}} |
| | 61 | 使用示例如下: |
| | 62 | {{{ |
| | 63 | ...... |
| | 64 | driver = self.driver |
| | 65 | functions.retry_when_timeout(driver,self.base_url + self.config["test.forum"]) |
| | 66 | #断言主题管理块 |
| | 67 | self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "div.forum_admin")) |
| | 68 | ...... |
| | 69 | }}} |
| | 70 | |
| | 71 | |
| | 72 | |