Skip to main content
 首页 » 编程设计

python之使用 Selenium webdriver 进行 Web2py 测试

2024年11月01日32kuangbin

这里是基本问题。我正在学习 [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) 

请您参考如下方法:

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