I'm testing a GWT Web Application similar to Paint. In this, there is a test case on which I am working on and I have to test this on a different machine using Selenium Grid. Along with that, I'm testing this test case on 2 browsers by executing them parallelly using TestNG. Now, the problem I am facing in this test case is that I have to take a screenshot of the screen and then save it locally in the folder created by me in the Current Working Directory. But the screenshot taken by the 2nd browser during testing updates the screenshot taken by the 1st browser since I have only 1 class (to be tested) for both of the browsers.
All I want is that I would be able to take a screenshot and save them with different names in my local directory. I searched a lot on the Internet but none of the methods worked out for me and therefore, I am confused on how to do this.
Meanwhile, here is my code.
Firstly, there is a Browser class where I have given the node address along with the capabilities of the browsers.
package testNgPackage;
public class Browser {
//ThreadLocal will provide thread-safe tests
protected ThreadLocal<RemoteWebDriver> threadLocal = null;
@BeforeTest
@Parameters("browser")
public void setup(String browser) throws MalformedURLException{
String nodeMachine = "http://xxx.xxx.xxx.xxx:5555/wd/hub"; //Providing the IP address of Node
if(browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe");
DesiredCapabilities capability = null;
capability = DesiredCapabilities.chrome();
capability.setPlatform(Platform.VISTA);
capability.setBrowserName("chrome");
threadLocal = new ThreadLocal<RemoteWebDriver>();
threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability));
}
else if(browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe");
DesiredCapabilities capability = null;
capability = DesiredCapabilities.firefox();
capability.setPlatform(Platform.VISTA);
capability.setBrowserName("firefox");
threadLocal = new ThreadLocal<RemoteWebDriver>();
threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability));
}
}
public WebDriver getDriver() {
return threadLocal.get();
}
@AfterTest
public void closeBrowser(){
getDriver().close();
}
}
Then, there is a TestNGClass which contains the code to be tested.
package testNgPackage;
public class TestNGClass extends Browser{
@Test
public void testCanvas() throws InterruptedException{
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
getDriver().get("XXXX"); //Here I have specified the URL
WebDriverWait wait = new WebDriverWait(getDriver(), 10);
wait.until(ExpectedConditions.titleContains("xxxxx"));
wait.until(ExpectedConditions.elementToBeClickable(By.id("yyyyy")));
Thread.sleep(3000);
/*
After that, there is a code to click on certain elements and then take the screenshot of the canvas.
I won't mention the code here because of the privacy concerns of the company. Along with that, it is useless and wastage of time to mention it here.
*/
//Given below is the main code which describes how I've specified the file location of Images.
File elementImage;
File fileBase = new File("./src/Images/baseDrawing.png");
File fileActual = new File("./src/Images/actualDrawing.png");
try {
elementImage = this.takeElementScreenshot(canvas,"png");
FileUtils.copyFile(elementImage, fileActual);
}catch(IOException e) {
e.printStackTrace();
}
this.compareImage(fileBase,fileActual);
Thread.sleep(6000);
}
public File takeElementScreenshot(WebElement element, String imageFormat) throws IOException{
/*
Code to take the Element Screenshot. The code in this function is correct and I'm not mentioning it here to reduce the reading time of the users.
*/
}
public void compareImage(File fileBase, File fileActual) {
/*
Code to compare the images by comparing them pixel by pixel. Not mentioning it here.
*/
}
}
This is the testng.xml file in which I have specified the test name to be run along with the parameter.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests" thread-count="2">
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="testNgPackage.TestNGClass"/>
</classes>
</test>
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="testNgPackage.TestNGClass"/>
</classes>
</test>
</suite>
Since I'm a beginner in TestNG, therefore, I am confused how to fix my issue. Please help.