Selenium Techniques

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

Handling Authentication Popup

baseUrl = “https://username:password@www.google.com””
driver.get(baseUrl);

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

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

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;
System.out.println(pickNum);
//Get the next random number from 1 to 49

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

driver.findElement(By.id(“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();
//Handler
while (ite.hasNext())
popupHandle = ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
//swtich back to default window
driver.switchTo().window(mainWindowHandle);

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

private Alert alert;
driver.switchTo().alert().accept();

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

public class TestAssertions {

@Test
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
assertNotNull(str1);

//Check that an object is null
assertNull(str3);

//Check if two object references point to the same object
assertSame(str4,str5);

//Check if two object references not point to the same object
assertNotSame(str1,str3);

//Check whether two arrays are equal to each other.
assertArrayEquals(expectedArray, resultArray);
}
}
Next, let’s create a java class file name TestRunner.java 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()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}

Navigation

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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