1.建立 python_org_search.py:javascript
#!/usr/bin/env python from selenium import webdriver from selenium.webdriver.common.keys import Keys # 建立Chrome驅動實例 driver = webdriver.Chrome() # 啓動瀏覽器而且導航到指定URL driver.get("http://www.python.org") # 斷言「Python」出如今瀏覽器標題 assert "Python" in driver.title # 定位name屬性爲「q"的元素 elem = driver.find_element_by_name("q") # 清空文本輸入框,輸入文字"pycon",而後回車 elem.clear() elem.send_keys("pycon") elem.send_keys(Keys.RETURN) # 斷言"No results found."不包含在當前頁面源代碼中 assert "No results found." not in driver.page_source # 關閉瀏覽器 driver.close()
2.運行腳本java
python python_org_search.py
selenium package 自己並不提供測試框架,咱們能夠經過Python’s unittest module 來編寫組織咱們的測試用例,以下實例 「python.org 搜索功能的測試用例」 node
test_python_org_search.py:python
#!/usr/bin/env python import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class PythonOrgSearch(unittest.TestCase): """ 用例類繼承unittest.TestCase """ def setUp(self): """ 用例初始化,每一個用例執行以前調用,一般用於初始化測試環境""" self.driver = webdriver.Chrome() def test_search_in_python_org(self): """測試用例XXX,方法命名一般以test_開頭""" 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() # 執行測試用例集
運行測試用例web
python test_python_org_search.py
.
----------------------------------------------------------------------
Ran 1 test in 9.171schrome
OK瀏覽器
Selenium Grid 能夠在不一樣的主機上創建主節點(hub)和分支節點(node),能夠使主節點上的測試用例在不一樣的分支節點上運行。對於不一樣的節點來講,能夠搭建不一樣的測試環境(操做系統,瀏覽器),從而使一份測試用例獲得不一樣環境下的執行結果。微信
Grid2再也不提供單獨的包,其功能已經集成到SeleniumServer中。session
一、啓動主節點hub:要使用遠程WebDriver,必須先啓動Selenium Server,啓動後,以下提示,則說明主節點hub已經成功啓動框架
D:\selenium>java -jar selenium-server-standalone-3.5.0.jar -role hub 08:39:26.494 INFO - Selenium build info: version: '3.5.0', revision: '8def36e068' 08:39:26.495 INFO - Launching Selenium Grid hub 2017-08-21 08:39:27.269:INFO::main: Logging initialized @1003ms to org.seleniumhq.jetty9.util.log.StdErrLog 08:39:27.288 INFO - Will listen on 4444 2017-08-21 08:39:27.352:INFO:osjs.Server:main: jetty-9.4.5.v20170502 2017-08-21 08:39:27.385:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0 2017-08-21 08:39:27.386:INFO:osjs.session:main: No SessionScavenger set, using defaults 2017-08-21 08:39:27.389:INFO:osjs.session:main: Scavenging every 600000ms 2017-08-21 08:39:27.399:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@64f6106c{/,null,AVAILABLE} 2017-08-21 08:39:27.420:INFO:osjs.AbstractConnector:main: Started ServerConnector@10dba097{HTTP/1.1,[http/1.1]}{0.0.0.0:4444} 2017-08-21 08:39:27.422:INFO:osjs.Server:main: Started @1156ms 08:39:27.423 INFO - Nodes should register to http://10.1.102.68:4444/grid/register/
2.啓動分支節點(node):
java -jar selenium-server-standalone-3.5.0.jar -role node -port 5555
java -jar selenium-server-standalone-3.5.0.jar -role node -port 5556
啓動成功,可見以下提示
D:\selenium>java -jar selenium-server-standalone-3.5.0.jar -role node -port 5555 08:40:10.588 INFO - Selenium build info: version: '3.5.0', revision: '8def36e068' 08:40:10.589 INFO - Launching a Selenium Grid node 2017-08-21 08:40:11.283:INFO::main: Logging initialized @941ms to org.seleniumhq.jetty9.util.log.StdErrLog 08:40:11.328 INFO - Driver class not found: com.opera.core.systems.OperaDriver 08:40:11.349 INFO - Driver provider class org.openqa.selenium.safari.SafariDriver registration is skipped: registration capabilities Capabilities [{browserName=safari, version=, platform=MAC}] does not match the current platform VISTA 08:40:11.372 INFO - Using the passthrough mode handler 2017-08-21 08:40:11.395:INFO:osjs.Server:main: jetty-9.4.5.v20170502 2017-08-21 08:40:11.418:WARN:osjs.SecurityHandler:main: ServletContext@o.s.j.s.ServletContextHandler@6a28ffa4{/,null,STARTING} has uncovered
http methods for path: / 2017-08-21 08:40:11.430:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@6a28ffa4{/,null,AVAILABLE} 2017-08-21 08:40:11.445:INFO:osjs.AbstractConnector:main: Started ServerConnector@7714e963{HTTP/1.1,[http/1.1]}{0.0.0.0:5555} 2017-08-21 08:40:11.460:INFO:osjs.Server:main: Started @1118ms 08:40:11.460 INFO - Selenium Grid node is up and ready to register to the hub 08:40:11.477 INFO - Starting auto registration thread. Will try to register every 5000 ms. 08:40:11.477 INFO - Registering the node to the hub: http://localhost:4444/grid/register 08:40:11.620 INFO - The node is registered to the hub and ready to use
經過主節點控制器能夠看到鏈接到hub的全部node信息 http://127.0.0.1:4444/grid/console
3.經過Remote配置測試用例在節點上執行。
#!/usr/bin/env python # -*- coding: utf-8 -*- from selenium.webdriver import Remote # 定義主機與瀏覽器 lists = { 'http://127.0.0.1:5555/wd/hub': 'chrome', 'http://127.0.0.1:5556/wd/hub': 'firefox', } # 經過不一樣的瀏覽器執行腳本 for host, browser in lists.items(): print(host, browser) driver = Remote(command_executor=host, desired_capabilities={ 'platform': 'ANY', 'browserName': browser, 'version': '', 'javascriptEnabled': True }) driver.get("http://www.baidu.com") driver.find_element_by_id("kw").send_keys(browser) driver.find_element_by_id("su").click()
上面啓動的hub與node都是在同一臺主機上,要想在其餘主機上啓動node,則必須知足如下要求:
啓動步驟:
1.啓動本地hub主機(假設本機IP爲:10.1.102.68)
java -jar selenium-server-standalone-3.5.0.jar -role hub
2.啓動遠程node主機(假設IP地址爲:10.1.102.75)
java -jar selenium-server-standalone-3.5.0.jar -role node -port 5555 -hub http://10.1.102.68:4444/grid/register