Automated Testing Using Selenium Webdriver and TestNG
Hello all, Welcome back. So this blog is with regard to automated testing using Selenium Web driver and TestNG.
Before talking about the the procedure of performing the testing functionalities I'll explain you in a nutshell on what automated testing is
As the name suggests Automated testing is the testing done without manually entering data, without the direct involvement of a user, performed with a testing tool. Here virtual users are created.
Wikipedia says "In software testing, test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes with predicted outcomes."
So coming back to our testing procedure...
Out intention- To test the functionalities of www.gov.lk website.
What do we use?
Because TestNg can generate reports(in a readable format as shown below) based on our Selenium test results. Web driver has no native mechanism for generating reports.
So let's start.
Prior requirement-
You have to download the following files and folders from the site http://www.seleniumhq.org/download/
Then, since we are hoping to test the script with TestNG and in order to create an empty test case create a TestNG class. For that,
Right click on Source Packages>>New>>Other>>Unit Test>>TestNG Test Case>>Give the class name you prefer>>Click Finish
Here I've given the name MyFirst.
Then import the files that we have downloaded previously. For that,
Right click libraries>>Add JAR/Folder... and import the files.
I've named the folders and files as A,B,C for the convenience. A,B,C are in one zip folder. Extract it and import A and B separately.Next, Zip the "C" folder again and import it.
Now you're done with importing all the files.
Now let's see the test scenarios that we will try on.
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
*
* @author himashi123
*/
public class FirstTest {
WebDriver driver;
String baseURL = "http://gov.lk";
public FirstTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@BeforeMethod
public void setUpMethod() throws Exception {
driver = new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
}
@Test
public void verifyGPortal() throws Throwable {
String actualWelcomeTitle = driver.getTitle();
System.out.println(actualWelcomeTitle);
String portalWelcomeTitle = "Government of Sri Lanka - Welcome";
Assert.assertEquals(portalWelcomeTitle, actualWelcomeTitle);
driver.findElement(By.linkText("English")).click(); //Checking the language selection functionality
System.out.println("Successfully selected English language");
driver.findElement(By.id("mainSearch")).sendKeys("Train"); //Verification of search functionality
System.out.println("Successfully typed Train");
driver.findElement(By.id("mainSearch")).sendKeys(Keys.ENTER);
Thread.sleep(10000);
driver.switchTo().frame("contentFrame");
if(driver.findElement(By.xpath("//a[contains(text(),'Reservation of Seats in Trains')]")).isDisplayed()){//Checking the link Reservation of seats in trains
driver.findElement(By.xpath("//a[contains(text(),'Reservation of Seats in Trains')]")).click(); System.out.println("Successfully clicked reservation of seats in trains");
Thread.sleep(8000);
Set<String> allWindows = driver.getWindowHandles();
String newWindow = allWindows.iterator().next();
driver.switchTo().window(newWindow);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();
}
}
@AfterMethod
public void tearDownMethod() throws Exception {
driver.quit();
}
}
The phrases marked in red are TestNG annotations.
You can find more on TestNG annotations through the below link.
http://testng.org/doc/documentation-main.html
After typing the code run the test file using TestNG. For that right click on the code and click test File.
If the test ran successfully a test report as shown below will be generated.
The below table shows the test cases for the above 3 tests which we have run using TestNG.
Before talking about the the procedure of performing the testing functionalities I'll explain you in a nutshell on what automated testing is
As the name suggests Automated testing is the testing done without manually entering data, without the direct involvement of a user, performed with a testing tool. Here virtual users are created.
Wikipedia says "In software testing, test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes with predicted outcomes."
So coming back to our testing procedure...
Out intention- To test the functionalities of www.gov.lk website.
What do we use?
- An IDE (Eclipse/NetBeans etc.)- I use Netbeans here(should have installed JDK as well)
- Selenium WebDriver
- TestNG
Because TestNg can generate reports(in a readable format as shown below) based on our Selenium test results. Web driver has no native mechanism for generating reports.
So let's start.
Prior requirement-
You have to download the following files and folders from the site http://www.seleniumhq.org/download/
- selenium-server-standalone-2.53.1.jar
- selenium-java-2.53.1.jar >> A
- selenium-java-2.53.1.-srcs.jar>>B
- selenium-java-2.53.1 (folder)>>C
Then, since we are hoping to test the script with TestNG and in order to create an empty test case create a TestNG class. For that,
Right click on Source Packages>>New>>Other>>Unit Test>>TestNG Test Case>>Give the class name you prefer>>Click Finish
Here I've given the name MyFirst.
Then import the files that we have downloaded previously. For that,
Right click libraries>>Add JAR/Folder... and import the files.
I've named the folders and files as A,B,C for the convenience. A,B,C are in one zip folder. Extract it and import A and B separately.Next, Zip the "C" folder again and import it.
Now you're done with importing all the files.
Now let's see the test scenarios that we will try on.
- The language functionality
- The search functionality
- Select ‘Reservation of Seats in Trains’ services and verify the site is loading properly.
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
*
* @author himashi123
*/
public class FirstTest {
WebDriver driver;
String baseURL = "http://gov.lk";
public FirstTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@BeforeMethod
public void setUpMethod() throws Exception {
driver = new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
}
@Test
public void verifyGPortal() throws Throwable {
String actualWelcomeTitle = driver.getTitle();
System.out.println(actualWelcomeTitle);
String portalWelcomeTitle = "Government of Sri Lanka - Welcome";
Assert.assertEquals(portalWelcomeTitle, actualWelcomeTitle);
driver.findElement(By.linkText("English")).click(); //Checking the language selection functionality
System.out.println("Successfully selected English language");
driver.findElement(By.id("mainSearch")).sendKeys("Train"); //Verification of search functionality
System.out.println("Successfully typed Train");
driver.findElement(By.id("mainSearch")).sendKeys(Keys.ENTER);
Thread.sleep(10000);
driver.switchTo().frame("contentFrame");
if(driver.findElement(By.xpath("//a[contains(text(),'Reservation of Seats in Trains')]")).isDisplayed()){//Checking the link Reservation of seats in trains
driver.findElement(By.xpath("//a[contains(text(),'Reservation of Seats in Trains')]")).click(); System.out.println("Successfully clicked reservation of seats in trains");
Thread.sleep(8000);
Set<String> allWindows = driver.getWindowHandles();
String newWindow = allWindows.iterator().next();
driver.switchTo().window(newWindow);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();
}
}
@AfterMethod
public void tearDownMethod() throws Exception {
driver.quit();
}
}
The phrases marked in red are TestNG annotations.
You can find more on TestNG annotations through the below link.
http://testng.org/doc/documentation-main.html
After typing the code run the test file using TestNG. For that right click on the code and click test File.
If the test ran successfully a test report as shown below will be generated.
The below table shows the test cases for the above 3 tests which we have run using TestNG.
Test
ID
|
Test
Objective
|
Preconditions
|
Steps
|
Test
data
|
Expected
result
|
Actual
result
|
Pass/Fail
|
TS_01
|
To
test the language functionality
|
·
Successful connection to internet
· Availability of a browser to browse the web pages
·
Log in to www.gov.lk site
|
·
Click on the link ‘English’
|
-
|
Directed
to the page index.php successfully
|
Pass
|
|
TS_02
|
To
verify the search functionality
|
·
Successful connection to internet
·
Log in to www.gov.lk site and select ‘English’
language
|
·
Type ‘Train’ in the search bar
·
Click the search icon or press enter
|
Train
|
Display
all the services related to the word ‘train’.
|
Displayed
all the services related to the word ‘train’.
|
Pass
|
TS_03
|
To
select ‘Reservation of Seats in
Trains’ services and verify the site is loading properly.
|
·
Successful connection to internet
·
Log in to www.gov.lk site and select ‘English’
language
·
Search the word ‘Train’ in the search bar.
|
·
Click on the link ‘Reservation of seats in trains’
|
-
|
Should
load the page containing Reservation of Seats in Intercity and other Express
Trains in www.gic.gov.lk site
|
Successfully
loaded the page containing Reservation of Seats in Intercity and other
Express Trains in www.gic.gov.lk site
|
Pass
|
very helpful..good job himashi :)
ReplyDeleteThank you for the feedback Ayodya :)
Delete