Selenium Techniques

List of Selenium techniques to handle webpage problems: (Web Driver code examples)

Handling Authentication Popup

baseUrl = “””

Mouse Hover
import org.openqa.selenium.interactions.*;

Action builder;
Actions hover = new Actions(driver);
WebElement objectOnScreen = driver.findElement(By.linkText(“Administration”));
builder =;

Scroll Window
import import org.openqa.selenium.JavascriptExecutor;

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(“window.scrollTo(0,Math.max(document.documentElement.scrollHeight,” + “document.body.scrollHeight,document.documentElement.clientHeight));”);

Generating Random Number
import java.util.Random;

Random rand = new Random();
int pickNum = rand.nextInt(49) + 1;
//Get the next random number from 1 to 49

Typing Left Padded Characters
import org.apache.commons.lang3.StringUtils;

driver.findElement(“MainContent_txtNotes”)).sendKeys(StringUtils.leftPad(“EndOfChars”, 999, ‘*’));
//add 999 characters ending with EndofChars and preceded by asterisks

Handling Popup Windows
import java.util.Set;;

String mainWindowHandle=driver.getWindowHandle();
String popupHandle = null;
//Popup window event has occurred now: user opens new window at this point
Set s = driver.getWindowHandles();
Iterator ite = s.iterator();
while (ite.hasNext())
popupHandle =;
//swtich back to default window

Clicking OK or Accept on an popup Alert Window
import org.openqa.selenium.Alert;

private Alert alert;

Junit Assert:
import org.junit.Test;
import static org.junit.Assert.*;

public class TestAssertions {

public void testAssertions() {
//test data
String str1 = new String (“abc”);
String str2 = new String (“abc”);
String str3 = null;
String str4 = “abc”;
String str5 = “abc”;
int val1 = 5;
int val2 = 6;
String[] expectedArray = {“one”, “two”, “three”};
String[] resultArray = {“one”, “two”, “three”};

//Check that two objects are equal
assertEquals(str1, str2);

//Check that a condition is true
assertTrue (val1 val2);

//Check that an object isn’t null

//Check that an object is null

//Check if two object references point to the same object

//Check if two object references not point to the same object

//Check whether two arrays are equal to each other.
assertArrayEquals(expectedArray, resultArray);
Next, let’s create a java class file name in C:\ > JUNIT_WORKSPACE to execute Test case(s)

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestAssertions.class);
for (Failure failure : result.getFailures()) {


driver.navigate().forward();   //forward on browser
driver.navigate().back();  //back on browser
driver.navigate().refresh();  //refresh browser


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s