使用selenium grid的hub做分發,且可查看分發后的服務器IP地址


背景:借助selenium 的grid做分布式運行,進行分發任務,(目前不做多瀏覽器的操作,只對谷歌瀏覽器進行操作)

目前在A服務器(http://10.40.6.24:4444)上注冊了一個hub,在B和C服務器分別注冊node,且都綁定在A服務器上,如圖

看很多貼子在使用grid中是指定某台服務器的IP地址運行,如下方式(我另一篇貼也有提到過)

driver = new RemoteWebDriver(new URL("http://10.40.2.113:5555/wd/hub"), capabilities);

而這樣,就只能自己去指定IP運行,我的目的是想通過hub節點,自己去分發任務,到某台電腦去運行,那該如何操作,實現方式

driver = new RemoteWebDriver(new URL("http://10.40.6.24:4444/wd/hub"), capabilities); 

注hub使用時,也是使用/wd/hub,如果你在Jenkins運行時出錯,請查看分發IP和你Jenkins運行的IP是否一樣,如果一樣,根本就沒法做分發

查看分發時運行服務服務器IP

使用了hub做分發,你不通過服務器查看,你不知道他分配到那台服務器,所以還需要調用一個接口,

"http://10.40.6.24:4444/grid/api/testsession?session=" + Session
接口:hub地址/grid/api/testsession?session=

返回“proxyId”就是運行的ip地址,關鍵的代碼如下:

WebDriver driver = new RemoteWebDriver(new URL("http://10.40.6.24:4444/wd/hub"), capabilities);
				System.out.println("獲取的driver:"+driver);
				// 獲取driver的session
				String getSession = driver.toString().substring(driver.toString().indexOf("(") + 1,
						driver.toString().indexOf(")"));				
				// get請求,獲取在hub上分發到某台電腦運行
				Map<String, String> getIP = Pub
						.get("http://10.40.6.24:4444/grid/api/testsession?session=" + getSession);
				Iterator<Entry<String, String>> entries = getIP.entrySet().iterator(); 
				while (entries.hasNext()) { 
				  Entry<String, String> entry = entries.next(); 
				  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
				}
				// 從返回內容中獲取到需要的內容
				Gson gson = new Gson();
				Map<String, Object> map = new HashMap<String, Object>();
				map = gson.fromJson(getIP.get("Response"), map.getClass());
				String proxyId = (String) map.get("proxyId");
				System.out.println("獲取的ip為:" + proxyId);

  打印內容:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM