????How to Write Test Code
??????????????????selenium webdriver??????????????????????У?
????from selenium import webdriver
????webdriver????????????е?WebDriver????????????????????selenium webdriver??????????????????????????????
????driver = web driver.Firefox()
????driver.get("http://www.google.com")
???????????Firefox WebDriver???????driver.get?????????????????и?????URL??????????????д???????????????????????????????????????????
????????????????????????????????????????Щ???????ж??
????assert "Google" in driver.title
?????????????ж??????html <title>???????????“Google”????????????????????????????????ж????????????????????檔
????????????????????????????????λ?????????????????Щ?????н???????????????????????????????????????????:

 

searchInput = driver.find_element_by_id("lst-ib")
searchInput.send_keys("selenium")
searchBtn = driver.find_element_by_name("btnK")
searchBtn.click()

 

????WebDriver?????????λ???????????

 ????click()??send_keys()????selenium.webdriver.common.action_chains.ActionChains?????????????????????click()?????????????????send_keys()??????????????????Щ??????????????????????????API??
????????????????????????????????driver.close()????driver.quit()?????????????????????close???????tab??quit?????????????????
????A Simple Demo
???????????????????????????????д?????????????????????????????Щ??????????????????????python??????unittest????????д????????????????????????unittest???????????terminal?п?????????檔

 

import unittest
from selenium import webdriver
class GooglePageTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_click_all_links_on_page(self):
driver = self.driver
driver.get("http://www.google.com")
length = len(driver.find_elements_by_tag_name("a"))
for i in range(0??length):
links = driver.find_elements_by_tag_name("a")
if links[i].is_displayed():
links[i].click()
driver.back()
self.assertIn("Google" or "YouTube"??driver.title)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()

??????δ?????????????unittest????????????????????test case????д?????????????????????Google????????????<a>??????????????????????title?????????????????????????????????????????????terminal?п????????????檔