Selenium python格式化程序测试失败

时间:2013-03-20 07:11:34

标签: python selenium selenium-ide

我使用selenium ide创建测试并将其保存为python脚本(webdriver)。但是当我使用python运行它时。我遇到了一些错误   文件“/usr/local/lib/python2.7/dist-packages/PyUnit-1.4.1-py2.7.egg/unittest.py”,第273行,在failUnlessEqual中

这是使用python格式化程序在Selenium IDE中自动生成的代码。

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 Untitled(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.co.in/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_untitled(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("testomg")
        try: self.assertEqual("testomg - Google Search", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))

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

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            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()

结果

电子

ERROR: test_untitled (__main__.Untitled)

Traceback (most recent call last):
  File "testing2.py", line 40, in tearDown
    self.assertEqual([], self.verificationErrors)
  File "/usr/local/lib/python2.7/dist-packages/PyUnit-1.4.1-py2.7.egg/unittest.py", line 273, in failUnlessEqual
    raise self.failureException, (msg or '%s != %s' % (first, second))
AssertionError: [] != ['testomg - Google Search != Google']

1 个答案:

答案 0 :(得分:1)

这是您的测试预期失败。

这一行

try: self.assertEqual("testomg - Google Search", driver.title)

断言您访问过的网页标题应为“testomg - Google搜索”。它实际上是“谷歌”。

您的测试失败了。可能是因为在将搜索词输入Google搜索表单后,您没有按下输入或点击搜索按钮

尝试添加

driver.find_element_by_id("gbqfsa").click()

将密钥发送到查询框元素后。