Skip to main content

Development

Using Selenium with Java and TestNG (4)

Simple Script

In this blog, I‘ll introduce how to use selenium to create a simple script. You will find that selenium is very easy to use.

Before we start, let me introduce the basics of automated testing. It is very simple, only 3 steps:

  1. Get the expected Result
  2. Get the Actual Result
  3. Assertion, compare the expected result and actual result

For selenium script, there are also 3 steps:

  1. Locate an element
  2. Operate an element
  3. Assertion

Here is a small example (this example may not work if the website gets a newer version):

package simplewebtest.test;

 

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class TestBaiduHome {

 

@Test

public void searchSomething(){

 

WebDriver driver=new FirefoxDriver();//open firefox

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//the time out period is set to 10 seconds

driver.get(“http://www.baidu.com”);//open the url

driver.findElement(By.id(“kw1”)).sendKeys(“GitHub”);//input search keyword

driver.findElement(By.id(“su1”)).click();//click the search button

 

String aResult=driver.findElement(By.xpath(“.//*[@id=’4′]/h3/a”)).getText();//get the text of 4th search result

assert aResult.contains(“GitHub”);//assertion

driver.findElement(By.xpath(“.//*[@id=’4′]/h3/a”)).click();//Open the 4th search result on baidu

 

//switch to the new window

for(String winHandle : driver.getWindowHandles()){

driver.switchTo().window(winHandle);

}

 

String aTitle=driver.getTitle();//get title

System.out.println(“current widnow title is:”+aTitle);//print it

assert aTitle.contains(“GitHub”);//assertion

 

}

}

 

Explanation:

WebDriver driver=new FirefoxDriver();

It creates a new instance of firefox web driver. If you want to use IE, then input “new IE driver” instead of “new FirefoxDriver”.

 

driver.get(“http://www.baidu.com”);

It’s simple to navigate to a url. If it fails, please check your web driver jar files’ version to make sure they are the latest.

 

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Set the timeout period to 10 seconds. It works for all the following test steps.

 

driver.findElement(By.id(“kw1”)).sendKeys(“GitHub”);// input search keyword

driver.findElement(By.id(“su1”)).click();//click the search button

First, find an element by id”kw1”, then input some text with the sendkeys method.

How do you find the id? You can use firebug on firefox. If the element you want to locate doesn’t have an id, you can locate it with name, className, xpath or css.

Usually we use FirePath (a firefox plugin) to create an xpath expression for elements which are hard to locate.

 

String aResult=driver.findElement(By.xpath(“.//*[@id=’4′]/h3/a”)).getText();//Get the text of 4th search result

assert aResult.contains(“GitHub”);// assertion

Get the actual result and compare it with the expected result.

We won’t use anything else to compare the results but testNG assertion. We don’t need to throw an exception because testNG will do it, and the test result will be on the testNG reports if we use testNG assertion.

 

for(String winHandle : driver.getWindowHandles()){

driver.switchTo().window(winHandle);

}

This is the code that makes web driver switch to a new window. If you want to operate the old window, you can save its winHandle.

 

The full code of my small test framework build with selenium is here: https://github.com/zhangting85/simpleWebtest

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Colin Zhang

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram