[Selenium] 在Grid模式下打印出当前Case是在哪台Node上运行

AAAbstractFlow()

public void getComputerNameOfNode(WebDriver driver){
		String CIHub = Environment.getHub();
		String hub = (CIHub.split("//")[1].split(":")[0]);
		String port = (CIHub.split(":")[2].split("/")[0]);
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); 
		
		String sessionUrl = "http://" + hub + ":" + port+ "/grid/api/testsession?session="+((RemoteWebDriver) driver).getSessionId();
		HttpPost httpPost = new HttpPost(sessionUrl); 
//		logger.info("Http post request is : "+httpPost.getRequestLine());
		
		try{
			//Execute HTTP request
            HttpResponse httpResponse = closeableHttpClient.execute(httpPost);  
            //Get HTTP response
            HttpEntity entity = httpResponse.getEntity();  
            //Response status
//            logger.info("HTTP status:" + httpResponse.getStatusLine());  
            //Check if response is null
            if (entity != null) {  
//            	logger.info("Content encoding:" + entity.getContentEncoding()); 
            	String jsonString = EntityUtils.toString(entity);
//            	logger.info("Response content:" + jsonString);
            	JSONObject jsonObject = JSONObject.fromObject(jsonString);
            	String proxyID=jsonObject.getString("proxyId");
            	String node = (proxyID.split("//")[1].split(":")[0]);
            	logger.info("The case is running on this node :" + node);
            }
		}
		catch(IOException e){
			e.printStackTrace();  
		}
		finally{
			try{
				closeableHttpClient.close();  
			}
			catch(IOException e){
				 e.printStackTrace();  
			}
		}
    }  

 

AAAbstractTest()

public class AAAbstractTest extends AbstractTest {
	private static final Logger logger = Logger.getLogger(AAAbstractTest.class);
	
	@BeforeMethod(alwaysRun=true)
	public void beforeTest(ITestContext context, Method method){
		WebDriver driver = getDriver(method);
		try{	
			LoginFlow flow = new LoginFlow(getDriver(method));
			if(Util.isUseGrid()){
				flow.getComputerNameOfNode(driver);
			}
			UserBean userBean = getUser(method);
			flow.login(userBean);
			logger.info(method.getName());
			
			String loginUser = userBean.getName().toString() + "/" + userBean.getPwd().toString();
			logger.info("Login user is : "+loginUser);
		}
		catch(Exception e){
			logger.info("[AAAbstractTest--beforeTest]==" + method.getName());
		}
		
	}
	
}

  

你可能感兴趣的:([Selenium] 在Grid模式下打印出当前Case是在哪台Node上运行)