一、环境的搭建请参考博文http://blog.csdn.net/qq_32782059/article/details/72875225
二、本代码的运行环境配置如下:
1、selenium-server-standalone-3.4.0.jar
2、selenium-2.53.0
备注:selenium3.*版本同selenium-server不兼容,有Bug。
3、对于火狐浏览器,geckodriver.exe需要selenium-3.3.1以上的版本,因此在本文讲述的环境中不能测试通过。
4、在IE中,会出现如下错误:NoSuchElementException: Message: Unable to find element with id,需要修改IE的设置,如下:
5、在IE中,还需要确保所有区域的保护模式设置一样,如下:
即Internet、Local Intranet、Trusted Sites、Restricted sites这四个区域要全部设置得一样。
备注:webdriver可以从http://docs.seleniumhq.org/download/页面下载
6、特定selenium版本的安装
a、pip uninstall selenium 卸载程序
b、pip install selenium=2.53 安装2.53版本的selenium
7、python测试代码如下:
#coding = utf-8
import time from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #google浏览器 capabilities = DesiredCapabilities.CHROME #微软浏览器 #capabilities = DesiredCapabilities.INTERNETEXPLORER #火狐浏览器 #capabilities = DesiredCapabilities.FIREFOX capabilities["platform"] = "ANY" driver = webdriver.Remote(command_executor="http://localhost:4444/wd/hub", desired_capabilities=capabilities ) """ 如果grid的hub可以接收到消息,但是测试不成功,则可以使用本地方法来调试。 如driver = webdriver.Ie()来进行本地测试, """ #driver = webdriver.Ie() #driver.implicitly_wait(3) driver.get("https://www.baidu.com") driver.find_element_by_id("kw").send_keys("python") driver.find_element_by_id("su").click() time.sleep(5) driver.quit()
8、同时在多个节点上运行测试用例。
启动hub:java -jar selenium-server-standalone-3.4.0.jar -role hub
启动节点1:java -jar selenium-server-standalone-3.4.0.jar -role node -port 5555 -browser browserName=chrome,maxInstances=5 -hub http://localhost:4444/grid/register
节点1支持5个谷歌浏览器实例。
启动节点2:java -jar selenium-server-standalone-3.4.0.jar -role node -browser "browserName=internet explorer" -port 5556 -hub http://localhost:4444/grid/register
节点2支持1个微软浏览器实例。
下面是测试代码:
#coding = utf-8 import time from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from threading import Thread #浏览器配置信息 browsers = [ DesiredCapabilities.INTERNETEXPLORER, DesiredCapabilities.CHROME ] # 创建webdriver驱动 def createDriver(caps): return webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=caps) def start_test(driver): #driver.implicitly_wait(3) time.sleep(3) driver.get("http://www.baidu.com") driver.find_element_by_id("kw").send_keys("python") driver.find_element_by_id("su").click() #driver.implicitly_wait(3) time.sleep(3) driver.quit() #开始分布式测试(多个节点同时运行。) def test_on_nodes(): threads = [] for bw in browsers: driver = createDriver(bw) t = Thread(target=start_test, args=(driver,)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print("测试运行完成") if __name__ == '__main__': #print(DesiredCapabilities.INTERNETEXPLORER) test_on_nodes()