I'm running my test cases in five different machines and getting report through extent report. But in report I'm not able to identify in which machine the test case get failed. So I need to print the platform name and browser name in the extent report for each test. Can any one help me with the solution?
Asked
Active
Viewed 1,412 times
-2
-
Your questions are already answered here [How to get browser name using Selenium WebDriver with Java?](https://stackoverflow.com/questions/35258079/how-to-get-browser-name-using-selenium-webdriver-with-java/35263148) and here [How do I programmatically determine operating system in Java?](https://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java) – Meet K. Jan 09 '21 at 04:56
-
correct, but when running the test using selenium grid in different nodes, it displays the platform (OS) of the HUB machine. I need the platform of the node machines. How to over come this? – Naveen Prasath P Jan 09 '21 at 05:29
-
Please elaborate more on the question. Share your code, your debugging results, etc. – Meet K. Jan 09 '21 at 05:38
1 Answers
0
It is a very nice and interesting question asked by you. There are two things, which you want to add in the Extent report i.e. Browser and hostname on which your test is run
- Browser : As per my understanding you must be passing the browser name in the DesiredCapabilities in your automation framework. So, there is no need to pick from the node, you can simply add that value in the Extent report.
- Hostname : To get this I did some R&D and it was successful. It is bit tricky and you can add this code in setup section and it will return you the Node IP and Port. Here,
hostname is my Hub IP
port is my Hub port
Kindly try this code and revert if you face any issue
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();
}
}

Sarabjeet Singh
- 1
- 2
-
Thanks for your answer, can u help me how to get browser and platform names from DesiredCapabilities. – Naveen Prasath P Jan 09 '21 at 06:01
-
Kindly check the above updated code. You can upvote if you liked the answer – Sarabjeet Singh Jan 09 '21 at 06:17