Skip to content

Tag: selenium

Mobile Application Testing

Mobile Application Testing

Quote:
If anybody wants to use / copy this tutorial to their Website or Blog please feel free to contact me at my personal email id: rhbroms@gmail.com
Manual Testing

Contents:

1. What is Mobile Application Testing?

2. Difference Between Mobile Testing and Mobile Application Testing.

3. Mobile Application Extensions and Small intro about Android Platform.

4. How to Test a Mobile Application?

Automation Testing

Click on the below Topics to Lean Automation Testing.

It will take only 2-3 days to learn If you are good at Java.

1. Robotium for Android Application Testing.

 

1. What is Mobile Application Testing?

It is somewhat similar to software testing but the testing will be performed on a mobile device instead of performing on a system.

Types of Mobile application Testing are: 

1. Installation testing.

2. Functional Testing.

3. Memory Leakage Testing.

4. Interrupt Testing.

5. Usability testing.

6. Certification Testing.

7. Performance Testing.

8. Integration Testing.

9. System Testing.

10. Accessibility Testing.

11. Adhoc(Monkey) Testing.

The all above points will be discussed in chapter 4:

 

2. Difference Between Mobile Testing and Mobile Application Testing.

Mobile Testing

– Mobile Testing is testing of Mobile Handsets or Devices.

– Here we mainly concentrate on Native applications(Blot wares).

– Testing all the core like SMS, Voice calls, Connectivity(Bluetooth), Battery(Charging), Signal receiving, Network are working correctly.

– Testing is conducted on both hardware and software.

Mobile Application Testing

– Mobile Application Testing is the testing of mobile applications which we are making as third party for the targeted mobile handset.

– Some core feature of the mobile are tested just to see that your application has not created any side effects on the device functionality.

 

3. Mobile Application Extensions and Small intro about Android Platform.

Below are some Mobile Platforms given with respective File Extensions

1. Android – .apk (Android application package file)

2. Iphone – .ipa (iOS application archive file)

3. Symbian – .sis (Software Installation Script)

4. J2ME – .jad and .jar (Java Application Descriptor and Java ARchive)

5. RIM – .alx and .cod

6. Windows Mobile – .cab

7. Bada – .jad and .jar (Java Application Descriptor and Java ARchive)

Android Platform

Testing Purpose we should have to know about the android architecture as well as about Android Versions. Details On this discussed below:

Architecture of Android:

Android operating system is a stack of software components which is roughly divided into five sections and four main layers as shown below in the architecture diagram:

Image and video hosting by TinyPic

Linux kernel
At the bottom of the layers is Linux – Linux 2.6 with approximately 115 patches. This provides basic system functionality like process management, memory management, device management like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at such as networking and a vast array of device drivers, which take the pain out of interfacing to peripheral hardware.

Libraries

On top of Linux kernel there is a set of libraries including open-source Web browser engine WebKit, well known library libc, SQLite database which is a useful repository for storage and sharing of application data, libraries to play and record audio and video, SSL libraries responsible for Internet security etc.

Android Runtime
This is the third section of the architecture and available on the second layer from the bottom. This section provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual Machine specially designed and optimized for Android.

The Dalvik VM makes use of Linux core features like memory management and multi-threading, which is intrinsic in the Java language. The Dalvik VM enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine.

The Android runtime also provides a set of core libraries which enable Android application developers to write Android applications using standard Java programming language.

Application Framework
The Application Framework layer provides many higher-level services to applications in the form of Java classes. Application developers are allowed to make use of these services in their applications.

Applications
You will find all the Android application at the top layer. You will write your application to be installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc.

Android Application Activity Lifecycle

The life cycle of an activity with main methods as shown below:

Image and video hosting by TinyPic

onCreate() This is the first callback and called when the activity is first created.

onStart() This callback is called when the activity becomes visible to the user.

onResume() This is called when the user starts interacting with the application.

onPause() The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.

onStop() This callback is called when the activity is no longer visible.

onDestroy() This callback is called before the activity is destroyed by the system.

onRestart() This callback is called when the activity restarts after stopping it.

If you know this much it is enough to Go ahead with Mobile application testing in Android platform.

 

4. How to Test a Mobile Application?

1. Installation testing:

To Verify application can be installed successfully.

Different ways of Installing an application to Android Device:

1(a). OTA(Over The Air) : We can install our application by using android play store app on the mobile or else by browsing into Google playstore website. This can be done using WiFi as well as by using GPRS connection.

1(b). Sideloading From Your PC : To do this we need ADB(Android Debug Bridge). This is a very simplest way to install any 3rd party app to your device, most of the time we test engineers prefer this way only.

Just download and Install Android SDK to your system and follow the below steps:

1. Download the application (APK) file to a folder of your choice on your computer.

2. Connect the phone to the computer through the USB port.

3. Enable installation of non-market applications on your phone:

a. On your phone, open the Applications menu (i.e., the tab in the bottom center of the
phone’s home screen) and click on the Settings application, then select Applications
from the Settings menu.

b. Make sure that the first selection, Unknown Sources, is checked so that you can install
an APK file from a location other than the Android Market. A warning message about
the phone being vulnerable to attack will be displayed. Select OK to continue if you
trust the APK file you are about to download. You can change this setting back after the
current installation is complete.

4. Turn on “USB debugging” on your phone: open the Applications menu and select Development,
then enable USB debugging.

5. Open a command/terminal window:

• On Windows, select Start > Run> then type ‘cmd’.
• On Mac OS X, open the Applications folder, select Utilities, then Terminal.
• On Linux, select Applications > Accessories > Terminal.

6. In the command/terminal window, enter the following command:

Code:

adb install <APK_FILE_PATH><APK_FILE>

2. Functional Testing:

Testing the functionality of the application with respect to given requirements of the application.

2(a). Application LOGO Test: Testing the logo of the application to check whether the app which is under test have used the proper logo as per the requirement specification or not.

2(b). Application Splash Screen Test: To verify that when user selects application logo in application manager splash is displayed.
Note that Splash do not remain for more than 3 seconds.

2(c). Application Background Image Test: To test whether the application under test is using the proper Background image as per given requirement specification or not.

2(d). Font style / size / color: Testing the Font size/style/color as per requirement specification.

2(e). Layout: Testing the layouts with respect to given requirement specification.

2(f). Back Key: Verify that back key should navigate the user to previous screen or should close the application with proper exit popup dialog(Again it depends on Requirement specification).

2(g). Application Start/Restart: 
i. Find the application icon and select it
ii. Observe the application launch In the timeline defined.
iii. Application must not take more than 25s to start

2(h). Exit Application: Verify that user is able to exit from application with every form of exit modes like Flip, Slider, End Key or Exit option in application and from any point.

3. Memory Leakage Testing

Memory leakage happens when a computer program or application is unable to manage the memory it is allocated resulting in poor performance of the application and the overall slowdown of the system. As mobile devices have significant constraints of available memory, memory leakage testing is crucial for the proper functioning of an application.

4. Interrupt Testing

An application while functioning may face several interruptions like incoming calls or network coverage outage and recovery. The different types of interruptions are:

  • Incoming and Outgoing SMS and MMS.
  • Incoming and Outgoing calls.
  • Incoming Notifications.
  • Battery Removal.
  • Cable Insertion and Removal for data transfer.
  • Network outage and recovery.
  • Media Player on/off.
  • Device Power cycle.

An application should be able to handle these interruptions by going into a suspended state and resuming afterwards.

5. Usability testing

Checking the user-friendliness of the application is known as Usability testing.

We have to verify if the application is achieving its goals and getting a favorable response from users. The application should have to give the results in very few steps or in few clicks.

The best example for this is: If you are booking a movie ticket by using some application, user should get the ticket booked in a very short amount of time / only by using 3-4 activities maximum!!

6. Certification Testing

Use this command : (Jarsigner is in your Java bin folder goto java->jdk->bin path in cmd prompt)

Code:

$ jarsigner -verify -verbose -certs my_application.apk

If you see “CN=Android Debug”, this means the .apk was signed with the debug key generated by the Android SDK (means it is unsigned), else u will find something for CN. for more details http://developer.android.com/guide/p…p-signing.html

How do developer signs their apps: 

Dev creates a keystore file by using the below cmd:

Code:

$ keytool -genkey -v -keystore my-release-key.keystore
-alias alias_name -keyalg RSA -keysize 2048 -validity 10000

To show you how it works I have given a screen shot of creating one below:

Image and video hosting by TinyPic

After generating key we have to follow the following steps:

  • Right click on project > Android Tools > Export Signed Application Package
  • The Export Android Application wizard appears.
  • Select the project I want to export, click Next.
  • The Keystore Selection screen appears.
  • Select the keystore and enter the password.

Image and video hosting by TinyPic

After this just give the path of the .apk file that has to be released.

Image and video hosting by TinyPic

7. Performance Testing

This testing process is undertaken to check the performance and behavior of the application under certain conditions such as low battery, bad network coverage, low available memory, simultaneous access to application’s server by several users and other conditions. Performance of an application can be affected from two sides: application’s server side and client’s side. Performance testing is carried out to check both.

This is done by performance test engineers. They use specific tools for this, for example: LoadRunner, Apache JMeter etc…

8. Integration Testing

Testing the data flow between two modules(activities) is called to be as Integration Testing.

This is done after finishing of Functional Testing.

The reason why we do Integration Testing is to make sure that all the activities are connected together properly or not.

9. System Testing

It is a end to end testing of the application where in Testing Environment is similar to production.

Here we test the data flow from the beginning to the end, If something is broken we have to report it as a bug.

We perform system testing after finishing of Integration Testing.

We perform system testing to make sure that end to end scenarios of the application is working properly or not.

10. Accessibility Testing

Accessibility testing is a subset of usability testing where the users under consideration have disabilities that affect how they use the application. The end goal, in both usability and accessibility, is to discover how easily people can use application and feed that information back into improving future designs and implementations.

This testing is done after completion of system testing.

11. Adhoc Testing(Monkey Testing)

Testing the application in a random way is called to be as Adhoc or Monkey Testing.

Monkey Testing is done after completion of full software testing and reporting of all the bugs is done. And also when the product is almost stable.

The reason why we perform this testing is,

  • To some how brake the product.
  • To find more number of bugs.
  • To make the software more stable.
Leave a Comment

Perl for Android automation

The tutorials are designed for beginners who have basic knowledge of below points:

1. Mobile application testing.

2. Basic adb(Android Debugging Bridge)commands .

3. If you know basics of automation testing, it would be a plus.

What Will I Learn?

How to automate Android devices also learn how to write automation scripts for Mobile testing (Native application). You will also learn performance testing mainly stress testing.

What you need to install?

1. Install adb drivers(device drivers) click here to download.

2. Install active perl, click here to download.

3. Install notepad++(Optional) click here to download.

Table of contents  –

  1. What is Stress testing wrt a mobile handset and Introduction to perl.
  2. What is adb(Android Debugging Bridge)
  3. How to establish interactions between Perl scripts and android device using adb commands.
  4. Some important scripts to perform stress testing on android native apps.
  5. Advantages and disadvantages.

1. What is Stress testing with respect to a mobile handset?

Testing the stability and response time of an application by applying load which is more than that of the designed number of users. For example we will make the messaging application to send / receive 10,000 msgs continuously for a period of time, by this we are applying stress to the application which in-turn might make the application brake/fail.

1a. What is Perl?

Perl was created by Larry Wall in 1987 when he was working on a bug reporting system “AWK”- a programming language he was using for the purpose was not helping him much. He is still the chief architect and developer of Perl. If we want to define Perl in one statement: Perl is a high level, interpreted, dynamic programming language.

NOTE: In this tutorial I will not teach more about Perl language, But will soon write a tutorial on the same. 🙂

2.What is adb(Android Debugging Bridge) ?

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:

  • A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
  • A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.

You can find the adb tool in <sdk>/platform-tools/.

When you start an adb client, the client first checks whether there is an adb server process already running. If there isn’t, it starts the server process. When the server starts, it binds to local TCP port 5037 and listens for commands sent from adb clients—all adb clients use port 5037 to communicate with the adb server.

The server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections.

Enabling adb Debugging

In order to use adb with a device connected over USB, you must enable USB debugging in the device system settings, under Developer options in system settings.

Syntax

You can issue adb commands from a command line on your development machine or from a script. The usage is:

adb [-d|-e|-s <device serialNumber>] <command>

3.How to establish interaction 

between Perl scripts and adb ?

Android packages which defined in Perl helps to interact with adb and similarly adb interacts with android device. Android packages has package manager as well as activity manager,package manager help to trigger a package and activity manager use in order to trigger any event or operation.Thus, Perl establishes connection between android packages using adb.

Example:

system(“adb -s $dev_id shell am start -a com.android.intent.action.MAIN -n com.android.mms/.ui.ComposeMessageActivity”);

system: Is used to provide the input. Here we perl will take the use of adb and adb intern does the job.

-s: Is used to provide the name of specific device which the user want to execute the test scripts.

Shell: adb provides a Unix shell that you can use to run a variety of commands on an
emulator or connected device. The command binaries are stored in the file system of the emulator or device, at /system/bin/…

Two of the most common command tools are activity manager (am) and package manager (pm). For more: Click here.

am start -a com.android.intent.action.MAIN : Activity Action: Start as a main entry point, does not expect to receive data. Here -a means action.

-n com.android.mms/.ui.ComposeMessageActivity: triggering activity. -n means component name.

4. Example script for applying stress to the device under test:-

Device LCD ON/OFF stress test –

print “###############LCD ON/OFF###########”;
system(“adb devices”); #prints the connected device id’s
print “Enter device id:”;
$dev_id=<STDIN>; #takes the input from user
for($i=1; $i<=50;$i++)
{
print “\\n********* LCD on off Iteration $i on device : $dev_id **********\\n”;
system(“adb -s $dev_id shell input keyevent 26”);
sleep 1; #device sleeps for 1 second
system(“adb -s $dev_id shell input keyevent 82”);
}

 

Device Camera image capture stress test –

print “\\n########Click an Image#########\\n”;
system(“adb shell input tap 300 400”); #give the x and y axis value as per the camera icon is displayed on the device screen.

for($i=1;$i<=100;$i++)
{
print “\\n ************ iteration $i ************\\n”;
system(“adb shell input keyevent 66”);
}

Device Browser stress test –

print “\\n########BROWSER#######\\n”;
system(“adb devices”);
print “Enter device id:”;
$dev_id=<STDIN>;
chomp($dev_id);
for($i=1;$i<=5;$i++)
{
if($i==1){

print “\\n******************* Iteration $i ************** \\n”;
system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘google.co.in’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘androidpolice.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);
}

if($i==2){

print “\\n******************* Iteration $i ************** \\n”;

system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘rhbroms.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘in.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);

}

if($i==3){

print “\\n******************* Iteration $i ************** \\n”;

system(“adb shell am start -a com.android.intent.action.MAIN -n com.android.browser/.BrowserActivity”);
system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘skype.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input tap 173 70”);
system(“adb -s $dev_id shell input text ‘apple.com’ “);
system(“adb -s $dev_id shell input keyevent 66”);

sleep 5;

system(“adb -s $dev_id shell input keyevent 3”);

}

else {
print “end”;
}
}

5. How to execute these scripts –

  1. After the complete installation of perl, write your scripts using notepad ++ and save the file with .pl extension.
  2. Connect your android device.
  3. Double click on the saved .pl file and you are good to go.

6. Advantages of using Perl for stress testing –

  1. It is open source, and platform Independent.
  2. Easy to understand and interpret.
  3. Faster execution.
  4. Can be used for Non-functional testing mainly.

7. Disadvantages of using Perl for stress testing –

  1. Not good for Automating the Functional test cases.
    As it is time consuming to write the scripts and we have other
    automation tools which does the job in few steps.
Leave a Comment

SELENIUM

 

SELENIUM by Ravi H Basawa

 

Quote:

This will be a Full Tutorial on Selenium Automation Tool
I will be updating this thread as I get time. SO NO ETA’s.

Quote:

If anybody wants to use / copy this tutorial to their Website or Blog please feel free to contact me at my personal email id: edy.434@gmail.com
or at our official Website: www.rhbroms.com

Contents:

1. What is Selenium?

2. What all we need before we get it started?

3. How to setup Selenium for Eclipse?

4. Simple Selenium Test Script for google.co.in

5. Execution using Junit

5a. Execution using TestNG

6. DataDriven Testing using POI jar files

7. Issues or challenges with Selenium

 

1. What is Selenium?

Selenium is an open source ( It’s free !!!!) automation testing tool used for automating websites under any browser(not all).

Selenium supports the following browsers: IE, FireFox(Default), Chrome, Safari, Opera.

We write Selenium WebDriver Scripts in Eclipse. These scripts are written in Java language.

Things that you should be good at Java are: Constructors, Overriding, Overloading, Constructor Chaining, Interface, Inheritance, Abstract Class and UpCasting – DownCasting concepts are enough to write Selenium WebDriver Script.

2. What all we need before we get it started?

1. Windows 7 / 8

2. Java

3. Eclipse ((Juno or ADT) what I have worked on !! )

4. selenium-server-standalone-2.38.0

5. TestNG plugin for Eclipse

6. POI jar files

Downloads:

POI jar files: POI.zip

Selenium Jar file

3. How to setup Selenium for Eclipse?

1. Open Eclipse goto Help -> Install new software.. -> give work with edittext field value as ” TestNG – http://beust.com/eclipse ” -> select all -> Next -> Finish

2. when you create a new test project inside its build path add selenium webdriver jar files. I will show it in next chapter.

4. Simple Selenium Test Script for Google website

1. Create new java project with name Selenium Test as shown below:

2. Now we have to add the selenium Jar file to the build path ( Goto project -> Properties -> Java Build path ) as shown in below screen shot and add the downloaded selenium-server-standalone-2.38.0.jar file. by clicking on the add external Jar’s button. After this goto “Order and Export” tab click on “Select All” button and click “ok” button.

3.After completion of the above step now create a new java class ( Goto Project -> New -> Class) as shown in below ss and give name as TestWebDriver

4. After this now we will write a code to open Mozilla FireFox Browser and also open Google website inside it. The code for this is as below:

Quote:

package com.rhb.selenium; // package name what I have given. It can differ with yours

import org.openqa.selenium.WebDriver; // Automatically imported by eclipse [This is for invoking the WebDriver class from selenium API]
import org.openqa.selenium.firefox.FirefoxDriver; // Automatically imported by eclipse [This is for invoking the FireFoxDriver class from selenium API]

public class TestWebDriver {

static WebDriver driver = null; // initialization

//the below method is made static to call this method inside our package without creating any instance for it.
public static void setup(){

driver = new FirefoxDriver(); // inbuilt method called from selenium class
driver.get(“http://www.google.co.in”); // opens the website which is written inside the braces.
}

}

5. Now Lets create another java class called FunLib and inside this what we do is we will try to enter some text into the edit text box of the Google website and also we will click on the search button. the code for this is as below:

Quote:

import org.openqa.selenium.By;

public class FunLib extends TestWebDriver {

public static void Search() throws InterruptedException {

driver.findElement(By.xpath(“//div[@id=’hplogo’]”)).click(); // clicks on the Google Logo
Thread.sleep(3000); // sleeps for 3000 milliseconds
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(“RHBROMS”); // type as RHBROMS in the edit text field
driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // Click on the Search button
Thread.sleep(3000); // again sleeps for 3000 milliseconds
driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // Click on the first link that is rhbroms.com

System.out.println(“button got clicked”); // displays the msg on console.
}

}

6. Okay now you might be thinking as how to identify the edit text field as well as other elements.!! Its easy for that we need some Extensions for FireFox they are as below:

7. After installing these Extensions we now see how to use them to Identify the objects. Thing is we don’t use all these but some, It again depends on which tool u want to use!!. which one is best for you.

8. First I will show you how to Identify the Google Logo by using Xpath. To do this open FireFox and open Google website and right click on Google Logo and select View XPath. After this you will see something like as shown below:

9. As you can see from the image as XPath is given as : id(‘hplogo’) but thing is how to use this in our code.. Simple just add the tag type in this case it is <div> (The <div> tag defines a division or a section in an HTML document.) the changes what u have to do is as shown in below Screen Shot.

10. Now the same goes for other objects too..!! If you find any doubts on this feel free to ask me by either commenting here or by emailing me.

11. Now we will see how to execute our first Automation Script in the next chapter.

5. Execution using Junit

1. First we will see how to execute our script via JUnit Suit.

2. Let us create a new JUnit test case (project -> New -> Junit test case) as shown in below screen shot:

3. Now let us give the Junit test case name as “Junit” as shown below in the screen shot. NOTE:Uncheck setup and teardown options.

4. Okay as we are done with creating new Junit test case just do the below editing:

Quote:

package com.rhb.selenium; // package name

import junit.framework.TestCase; // auto import

import org.testng.annotations.Test; // auto import to give annotations that are necessary

public class Junit extends TestCase {

@Test
public void test1() throws Exception{

Main m = new Main(); // creating instance for Main class as “m”

m.setup(); // as setup is a method which is declared under Main class we can access it using access specifier “m”
m.Search(); // as Search is a method which is declared under Main class we can access it using access specifier “m”

}

}

5. To run the above JUnit Test right click on Junit.java file -> RunAs -> Junit Test

6. When you run the Junit test FireFox runs automatically in WebDriver mode as shown below and all the operations that we have specified in the Main class will be performed. Screen Shot of this is as shown below:

7. The below snap shows the test case is pass and the color is changed to Green. And also we got a msg at console as “button got clicked” what we have written in FunLib.java !!

8. The Result at FireFox WebDriver should be as shown below:

9. Okay now we will see how to execute using TestNG ( Easy one 😛 )

5a. Execution using TestNG

1. Don’t worry it is very easy compared to JUnit.

2. I hope you have installed the plugin of TestNG to your Eclipse.

3. Lets Create a new java class with name TestNGSuite1. Copy paste the below code after creation:

Quote:

package com.rhb.selenium;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatE xception;
import org.testng.annotations.Test;

public class TestNGSuite1 {

@Test
public void testngtest() throws FileNotFoundException, InvalidFormatException, IOException, Exception {

Main m = new Main(); // instance of Main class
m.setup(); // call for setup method
m.Search(); // call for Search method
}

}

3. Right click on TestNGSuite1.java file -> TestNG -> Convert to TestNG as shown in below screen shot:

4. Now you will see as below Screen Shot, here TestNG is converting our java file to executable xml format.

5. Now click on Finish button and you will see a Testing.xml file as shown below:

6. Now Just right click on the xml file -> RunAs -> TestNG Suite as shown below:

7. Finally you will see the final result as below:

6. DataDriven Testing using POI jar files

1.We go for data driven testing when we have some modules that need to be tested for multiple values. For example in a application which has a login page with username and password field we have to test these edit text boxes for multiple inputs, It can be a number or it also can be a string or both together.

2. Here I will take Google.com as example and show you how to extract data from a excel file and push the same to Google website.

3. First we will create an excel file with some data present in it to test our application. (An Excel file for testing our app is attached here)

4. To extract data from Excel we use “FileInputStream” Class by which we can create/delete and modify a file.

5. Now add POI.jar files to Java Build Path same as how we added Selenium Jar file.

6. After adding of POI jar files to Java Build Path you will see them added as shown below:

7. After this is done we will create a new java class and will give the name as “DataDriveTest” and extend it from “TestWebDriver” Class.

8. Now what we will do is we will open google.com enter “search” value as “RHBROMS” ( 1st value from the excel sheet ) click on the first link and then we will click on back button of the browser and then we will clear the search box and enter “xda developers” ( 2nd value from the excel sheet ) and click on first link that will be xda developers website.

9. The code for this is as written below with explanation.

Quote:

package com.rhb.selenium;

// below package imports are for File I/O operations

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

// below packages are for excel sheet operations

import org.apache.poi.openxml4j.exceptions.InvalidFormatE xception;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;

public class DataDriveTest extends TestWebDriver {

public static void test() throws Exception, IOException,
FileNotFoundException,InvalidFormatException {

String cellval1 = null; // set the current value of cellval1 as NULL
String cellval2 = null; // set the current value of cellval2 as NULL

FileInputStream fis= new FileInputStream(“E:\\Test.xls”);

Workbook wb = WorkbookFactory.create(fis); // creates object for workbook Test.xls

Sheet ws = wb.getSheet(“Sheet1”); // opens Sheet1 from workbook Test.xls

int rc = ws.getLastRowNum(); // counts the number of rows which are used

for(int i=1; i<=rc; i++)
{

Row r = ws.getRow(i); // ponts to the i’th ROW

for (int j=0;j==0;j++){

{
Cell c = r.getCell(j); // points to the j’th Column
cellval1=c.getStringCellValue(); // gets the data from J’th cell

Cell c1 = r.getCell(j+1); // ponts to the J+1’th column
cellval2=c1.getStringCellValue(); // gets the data from J+1’th cell

driver.findElement(By.xpath(“//div[@id=’hplogo’]”)).click(); // Clicks on the google logo
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(cellval1); // take the first value from first column and paste it at the search box

driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // clicks on the search button

Thread.sleep(3000); // sleeps for 3000milisecs
driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // clicks on first link

driver.navigate().back(); // clicks on the back button of the browser

driver.findElement(By.xpath(“//input[@name=’q’]”)).clear(); // clears the search box
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(cellval2); // enters the second value from the excel sheet

driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // clicks on the search button again

Thread.sleep(3000);
driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // clicks on the first link

}

}

}

}
}

10. Now to execute this we have to do some editing at Main.java file as:

Code:
public class Main extends DataDriveTest {
public static void main(String args[]) throws FileNotFoundException, InvalidFormatException, IOException, Exception{
		
		setup();
		test();
		
	}

11. And also in TestNGSuit1.java file as:

Code:
@Test
public void testngtest() throws FileNotFoundException, InvalidFormatException, IOException, Exception {
	
	Main m = new Main();
	m.setup();
	m.test();
}

12. Now as usual convert the TestNGSuit1 file to TestNG and execute.

13. To see your TestNG results go to your eclipse workspace and open selenium project inside that you will find test-output folder. As shown in below Screen Shot:

14. For Generation of Graphical and more clean Results we use TestNG xslt with ANT. lets see how to do it in Next Chapter

6. TestNG xslt with ANT

1. Download Ant from here: http://ant.apache.org/

2. Unzip it and rename the folder as ant.

3. Set ANT_HOME to environmental variables.( In windows 7 Right click on Computer -> properties -> “Advance system setting”
-> Choose Advanced Tab
-> Press Environment Variables Button
-> In the System Variables, click New Button

Give the Variable Name:ANT_HOME
Give the Value: E:\ant
Click OK )

as shown in Below Screen Shot.

4. Set ANT_HOME path,
go to path
Give the Value C:\ANT\bin
Click OK

5. To check ANT works properly or not
In the command prompt, type:

Code:
ant -version

you will see as below Screen Shot:

6. Now download testng-xslt from HERE

7. After this extract the downloaded zip file and go to testNG-xslt -> src -> main -> resources and copy the file testng-results.xsl and also copy this file from testNG-xslt -> lib that is saxon-8.7.jar and keep them at any folder for time being.

8. Now go to your project workspace and goto SeleniumTest -> test-output and paste testing-results.xsl that you copied.

9. and now goto eclipse and add saxon-8.7.jar to buildpath.

NOTE: the thing is you have to keep all your jar files in a same folder as I have kept at jar folder as shown below in my setup:

10. Now after doing all this create new xml file and call it as Build.xml

11. After creating this just copy paste the below code:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="SeleniumTest" default="compile" basedir=".">


<path id="cp">
    <pathelement path="bin" />
        <fileset dir="jars"/>
</path>

<!-- for compiling  -->
 <target name="compile">
        <javac classpathref="cp" srcdir="src" destdir="bin"/>
</target>

<!-- for running  -->
<target name="run" depends="compile">
    <java classpathref="cp" classname="org.testng.TestNG" args="testng.xml"/>
</target>

<!-- for report generation -->
  <target name="report" depends="run">
    <xslt in="./test-output/testng-results.xml" style="./test-output/testng-results.xsl" out="./test-output/testng-xslt.html">
     <param expression="${basedir}/test-output/" name="testNgXslt.outputDir" />
     <classpath refid="cp"/>
    </xslt>
    </target>
</project>

12. After this save the file.

13. Now right click on the project and do as below and select TestNG:

14. Now Run the Build.xml file. and your results will be stored at Index.html at test-output folder.

To be continued .. NEXT will be Maven with TestNG!!

Leave a Comment