Selenium在脚本中执行时运行相同的测试两次 - Python

时间:2013-09-24 18:09:13

标签: python unit-testing selenium selenium-webdriver repeat

我有一个python脚本运行不同的selenium WebDrivers,它已导出到python中。脚本和webdrivers做我想让他们做的事情。当脚本执行webdriver时,webdriver将执行它所需的操作然后返回并继续脚本,但随后它将暂停并且firefox将再次弹出并再次执行相同的webdriver任务。这不会影响我从Web驱动程序或脚本获得的结果,但它会显着增加我的运行时间。

首次执行webdriver后,shell将返回:

Ran 1 test in 192.680s
OK

然而,当它意外地重复webdriver时它只返回一个句点“。”

以下是webdriver的代码,

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class WebdriverViewas(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = #website I'm interested in visiting
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_webdriver_viewas(self):
        #Here I just tell the webdriver what to do, I doubt this is causing the problem

    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__':

    try:
        unittest.main() 
    except SystemExit as inst:
        if inst.args[0] is True: # raised by sys.exit(True) when tests failed
            raise

我怀疑这个问题可以通过unittest模块解决,但我在研究中没有发现任何事情,也没有找到任何有类似问题的人。

0 个答案:

没有答案