【问题标题】:How to get platform name and browser name of the node machine - selenium grid - Java如何获取节点机器的平台名称和浏览器名称 - selenium grid - Java
【发布时间】:2021-04-14 18:38:36
【问题描述】:

我正在五台不同的机器上运行我的测试用例,并通过范围报告获取报告。但在报告中,我无法确定测试用例在哪台机器上失败。所以我需要在每次测试的范围报告中打印平台名称和浏览器名称。谁能帮我解决一下?

【问题讨论】:

标签: java selenium selenium-webdriver selenium-grid


【解决方案1】:

这是您提出的一个非常好的和有趣的问题。您要在范围报告中添加两件事,即运行测试的浏览器和主机名

  1. 浏览器:据我了解,您必须在自动化框架的 DesiredCapabilities 中传递浏览器名称。因此,无需从节点中进行选择,您只需在范围报告中添加该值即可。
  2. 主机名:为了得到这个,我做了一些研发并且成功了。这有点棘手,您可以在设置部分添加此代码,它将返回节点 IP 和端口。在这里,

主机名是我的集线器 IP

端口是我的集线器端口

如果您遇到任何问题,请尝试此代码并恢复

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

import com.google.gson.JsonObject;
import com.google.gson.JsonStreamParser;

public class TestGrid {

    public static void main(String[] args) throws MalformedURLException {
        String hostname = "10.10.4.176";
        String port = "4441";
        
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("firefox");
        capabilities.setPlatform(Platform.WINDOWS);
        
        WebDriver driver = new RemoteWebDriver(new URL("http://"+hostname+":"+port+"/wd/hub"),capabilities);
        
        SessionId sessionid = ((RemoteWebDriver) driver).getSessionId();
        driver.get("http://"+hostname+":"+port+"/grid/api/testsession?session="+sessionid);
        String nodeDetails = driver.findElement(By.xpath("//pre")).getText();
        
        JsonStreamParser parser = new JsonStreamParser(nodeDetails);
        JsonObject obj = parser.next().getAsJsonObject();
        
        System.out.println("Node name: "+obj.get("proxyId").getAsString());
        
        String browserName = capabilities.getBrowserName().toLowerCase();
        System.out.println("Browser name: "+browserName);

        String os = capabilities.getPlatform().toString();
        System.out.println("Platform name: "+os);
        
        driver.get("http://www.google.com");
        driver.manage().window().maximize();
    }

}

【讨论】:

  • 感谢您的回答,您能帮我如何从 DesiredCapabilities 获取浏览器和平台名称。
  • 请检查以上更新的代码。如果你喜欢这个答案,你可以投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2017-02-05
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多