How to take the Screen shot in Selenium Grid

Here is the code that you can use to take the screen shot when running the script using Selenium Grid or Remote machine

public void takeScreenshot() throws IOException {
Random rand = new Random();
int num = rand.nextInt(150);
File scrFile = null;
Integer numNoRange = rand.nextInt();
WebDriver underlyingDriver = driver.getWrappedDriver();
if (browser.contains("Remote")) {
try {
WebDriver augmentedDriver = new Augmenter().augment(underlyingDriver);
scrFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
String filename = numNoRange.toString();
copyFile(scrFile, new File("location were you want the file to be saved" + filename + ".png")); //Change location were you want the file to be saved
} catch (Exception n) {
LOGGER.warn("Failed to take screen shot of remote browser");
}
}
if (scrFile == null) {
try {
scrFile = ((TakesScreenshot) underlyingDriver).getScreenshotAs(OutputType.FILE);
String filename = numNoRange.toString();
copyFile(scrFile, new File("location were you want the file to be saved" + filename + ".png")); //Change location were you want the file to be saved
} catch (NullPointerException n) {
fail(n.getMessage());
}
}
}

Wait for at-least one element condition Selenium Webdriver

There are a lots of situations in your automation scripts where you are waiting for an element to be verified on the page and you may get an error and the script still waits for the element to be loaded on the page rather than catching the error.
Here is the script that will do that and you can also stipulate the timeout for the condition.
The Script is coded in Java Selenium Webdriver and used the Java 5 for loop.

public void waitForOneElementPresent(int i, By... by) throws InterruptedException {
boolean found = false;
for (int second = 0; second <= i; second++) { if (second >= i) {
break;
}
for (By singleBy : by) {
if (isElementPresent(singleBy)) {
found = true;
}
}
if (found) {
break;
}
Thread.sleep(1000);
}
}

Read a XML test data in Java

Here is the code to read the xml test data coded in Java.

import java.io.File;
import java.io.FileInputStream;
import

java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class ReadXmlData {
public String value;

public String data(String node) {
try {
File file = new File("../TestData.xml"); // XML file path
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
if (((String) enuKeys.nextElement()).contains(node)) {
value = properties.getProperty(node);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
}

Highlight element with Selenium Webdriver

Here is the code to highlight the element using Selenium Webdriver. You will have to replace the driver.findElement to findElement. I have also give the example of using findElement.

public WebElement findElement(By by) {
WebElement elem = driver.findElement(by);
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript("arguments[0].style.border='1px solid yellow'", elem);
}
return elem;
}


public void type(By by, String value) throws InterruptedException {
try {
findElement(by).clear();
findElement(by).sendKeys(value);
} catch (Exception e) {
waitForSpinner();
findElement(by).clear();
findElement(by).sendKeys(value);
}
}

waitForElementPresents method to check for multiple elements on a webpage

waitForElementPresents method is used to check for multiple elements on a webpage at one time. Java Selenium Webdriver.
[code]
public void waitForElementPresents(int i, By… by) throws InterruptedException, IOException {
for (int second = 0; second <= i; second++) {
if (second >= i) {
takeScreenshot();
break;
}
if (areAllElementsPresent(by)) {
for (By singleBy : by) {
pass("Found:"+singleBy);
}
break;
}
Thread.sleep(1000);
}
}
[/code]
[code]
public boolean areAllElementsPresent(By… by) {
for (By singleBy : by) {
if (!isElementPresent(singleBy)) {
return false;
}
}
return true;
}
[/code]