接上一节,说一下Grid的用法
1、启动hub
set JAVA_HOME=C:\NeoLanguages\Java\JDK\jdk_x86_1.8.0_77 set PATH=%JAVA_HOME%\bin;%PATH%;C:\NeoTest\TestSelenium\trunk\bin\x86\; set webdriver.gecko.driver=C:\NeoTest\TestSelenium\trunk\bin\x86\geckodriver.exe java -jar ../lib/selenium-server-standalone-3.3.1.jar -role hub -hubConfig hubConfig.json -debug true pause
hubConfig.json
{ "port": 4444, "newSessionWaitTimeout": -1, "servlets" : [], "withoutServlets": [], "custom": {}, "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher", "throwOnCapabilityNotPresent": true, "cleanUpCycle": 5000, "role": "hub", "debug": false, "browserTimeout": 0, "timeout": 1800 }
2、启动node
set JAVA_HOME=C:\NeoLanguages\Java\JDK\jdk_x86_1.8.0_77 set PATH=%JAVA_HOME%\bin;%PATH%;C:\NeoTest\TestSelenium\trunk\bin\x86\; set webdriver.gecko.driver=C:\NeoTest\TestSelenium\trunk\bin\x86\geckodriver.exe java -jar ../lib/selenium-server-standalone-3.3.1.jar -role node -nodeConfig node01Config.json -debug true pause
node01Config.json
{ "capabilities": [ { "browserName": "firefox", "maxInstances": 5, "seleniumProtocol": "WebDriver" } ], "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", "maxSession": 5, "port": 5555, "register": true, "registerCycle": 5000, "hub": "http://192.168.130.178:4444", "nodeStatusCheckTimeout": 5000, "nodePolling": 5000, "role": "node", "unregisterIfStillDownAfter": 60000, "downPollingLimit": 2, "debug": false, "servlets" : [], "withoutServlets": [], "custom": {} }
3、可以在hub中看到注册情况
http://localhost:4444/grid/console
这个地方一定要注意,每个node上的浏览器及操作系统信息,向hub发送指令时,必须符合该信息
4、运行脚本
TestHelloWorldHub.py
# !C:\Languages\Python\Python27\python.exe # -*- coding: utf-8 -*- ''' Created on 2016-10-22 @author: Hansen HelloWorld sample for NeoSelenium ''' from NeoSelenium import initEngine from NeoSelenium import deInitEngine from NeoSelenium import initEngineHub #一个简单的查询测试 def neohope_search_test(): try: #myEngine = initEngine('ie32') #myEngine = initEngine('ie64') #myEngine = initEngine('chrome') #myEngine = initEngine('ff32') #myEngine = initEngine('ff64') #myEngine = initEngineRemote() myEngine = initEngineHub() base_url = "https://www.neohope.com" myEngine.get(base_url + "/") myEngine.find_element_by_name("s").clear() myEngine.find_element_by_name("s").send_keys("Metabase") myEngine.find_element_by_css_selector("button.search-submit").click() myEngine.implicitly_wait(1000) #print(myEngine.find_elements_by_xpath("//div[@id='content']/article")) queryResultLenght = len(myEngine.find_elements_by_xpath("//div[@id='content']/article")) #print(queryResultLenght) #应该是1但现在是10 assert queryResultLenght==1 finally: #deInitEngine(myEngine) print("test end") #start here neohope_search_test()
NeoSelenium.py
#!C:\Languages\Python\Python27\python.exe # -*- coding: utf-8 -*- ''' Created on 2017-04-07 @author: Hansen NeoSelenium ''' import os from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities NPath = None x86Path = "C:\NeoTest\TestSelenium\\trunk\\bin\\x86;" x64Path = "C:\NeoTest\TestSelenium\\trunk\\bin\\x64;" ''' selenium remote engine init ''' def initEngineRemote(): engine = webdriver.Remote(command_executor="http://192.168.130.178:4444/wd/hub", desired_capabilities=DesiredCapabilities.FIREFOX) return engine ''' selenium remote engine init ''' def initEngineHub(): engine = webdriver.Remote(command_executor="http://192.168.130.178:5555/wd/hub", desired_capabilities={ "browserName": "firefox", "platform": "VISTA" }) return engine ''' selenium engine deInit ''' def deInitEngine(engine): engine.close() engine.quit()