2

这里的基本问题。我正在关注 [Marco Laspe] ( http://killer-web-development.com ) 编写的关于创建 web2py 应用程序的精彩教程。但是努力解决我的测试失败(使用 Selenium)。

这是我的关于页面...

<!DOCTYPEhtml>
<html>
  <head>
    <title>aaa</title>
  </head>
  <body>
    <h1>blah</h1>
  </body>
</html>

和测试功能...

def test_has_right_title(self):
    title = self.browser.find_element_by_tag_name('title')
    self.assertEqual('aaa', title.text)

当测试我得到...

======================================================================
FAIL: test_has_right_title (test_static_pages.TestPrivacyPage)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\web2py\applications\pitch\fts\test_static_pages.py", line 40, in test
_has_right_title
    self.assertEqual('aaa', title.text)
AssertionError: 'aaa' != u''

----------------------------------------------------------------------
Ran 10 tests in 37.497s

FAILED (failures=3)

有人知道我要去哪里错了吗?其他测试工作正常(包括在下面)

def setUp(self):
    self.url = ROOT + '/pitch/default/privacy'
    get_browser=self.browser.get(self.url)

def test_can_view_privacy_page(self):
    response_code = self.get_response_code(self.url)
    self.assertEqual(response_code, 200)

def test_has_right_heading(self):        
    heading = self.browser.find_element_by_tag_name('h1')
    self.assertIn('Pitch.Me Privacy Policy', heading.text)
4

1 回答 1

2

Selenium API 没有获取标签,而是在Driver类中提供了一个函数来获取标题。尝试..

def test_has_right_title(self):
    title = self.browser.title
    self.assertEqual('aaa', title)

或重构为

def test_has_right_title(self):
    self.assertEqual('aaa', self.browser.title) # assuming you don't need it anywhere else
于 2013-10-09T20:59:26.803 回答