0

我使用 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']
4

1 回答 1

1

这是您的测试的预期失败。

这条线

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

断言您访问过的页面的标题应该是“testomg - Google Search”。它实际上是“谷歌”。

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

尝试添加

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

就在您将键发送到查询框元素之后。

于 2013-03-20T08:03:05.213 回答