這章總結selenium在UI測試方面的用法html
import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class PythonOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_search_in_python_org(self): driver = self.driver driver.get("http://www.python.org") self.assertIn("Python", driver.title) elem = driver.find_element_by_name("q") elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." not in driver.page_source def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()
首先建立一個類 PythonOrgSearch ,其中 test_search_in_python_org 方法中寫的是一個測試用例,像JUnit中@before@after同樣的做用,能夠用setUp和tearDown函數。這兩個函數分別表示在每一個測試用例執行前須要作的操做和執行後須要作的操做。不用在每一個測試用例中都進行聲明,只要寫在兩個函數中就會自動調用python
參考 http://selenium-python.readthedocs.io/navigating.htmlweb