Many, perhaps most, software applications today are written as web-based applications to be run in an Internet browser. Test automation is nothing but using a software tool to run repeatable tests for such web applications. There are many advantages of test automation. Most are related to the repeatability of the tests and the speed at which the tests can be executed. There are a number of commercial and open source tools available for assisting with the development of test automation. But ‘Selenium’ is possibly the most widely-used open source solution for this.
Selenium's tool suite includes the following:
Why choose ‘Selenium Webdriver’ for QA test automation?
'Selenium RC' and 'Selenium WebDriver' allow you to write automation scripts in programming languages with great ease. WebDriver is the successor to Selenium RC. Selenium RC has been officially deprecated in favor of Selenium WebDriver. Selenium IDE is a Record and Playback tool. Though, it’s very easy to use, but is highly unreliable. It also does not provide programming facility. Although the scripts recorded
by Selenium IDE can be saved for later use, it is not designed to run your test passes. The test cases will fail due to a small change in UI, or for things like change in name of a UI component. Selenium-Grid allows the Selenium RC solution to scale for large test suites and for test suites that must be run in multiple environments. Hence, Selenium Webdriver is the preferred tool to automate your testcases.
What is an automation framework?
A test automation framework is a set of concepts, and practices that provide support for automated software testing. It is a methodology built to successfully carry out test automation. If we do not have any frameworks, then it is difficult to get proper reports, handle checkpoints, or exception handling.
Which Automation framework to choose?
Now either you may choose from any of the frameworks available like JUnit & TestNG or you can design our own framework.
Junit is a unit testing framework or the Java programming language. TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc.
TestNG is a preferred framework of QA analysts as it allows you to generate test reports in both HTML and XML formats. TestNG enable you to group test cases easily which is not possible in JUnit.
Usually we want to integrate execution of automated test suites with build process. Ant is a software tool for automating software build processes. Ant can get source code from version control, compile source code, package compiled source code and resources, handle dependencies between its targets, run test cases, generate reports of test execution and much more. It is advisable to call your TestNG tests through Ant script, to integrate the testing with the build process. Using Ant you can also generate better reports than the default reports generated by TestNG.
Let’s get started with TestNG!
Here is a small tutorial to help QA professionals write Selenium test cases in TestNG, and calling the TestNG suite from Ant. You can use any of the programming language supported by Selenium 2. I have used Java in below tutorial.
Before getting started, install these software's
(if you don't have):
Steps to follow:
Step 1: Use Eclipse. Create a new Java project.
Step 2: Add Selenium server jar as external jar file to your project. To do this, right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add external jar'. Select the Selenium server jar file you have previously downloaded.
Step 3: Use TestNG library in your project: right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add Library'. Select TestNG. Click 'Ok'.
Step 4: A TestNG testsuite comprises of number of tests. A TestNG 'test' comprises of one or more classes, and a TestNG class consists of multiple 'Test Methods'.
Add a new Java class 'TestClass1' to the project. Following is a set of Selenium test cases written as TestNG methods. In the code below, I have written a set of test cases to test search functionality of the website http://www.floraindia.com. 4 test cases are added here for demonstration purpose. Add following code to the Java class.
Step 5: Add following TestNG testsuite to your project as 'TestNG.xml'
Step 6: Execute above TestNG testsuite by right clicking on the TestNG.xml and selecting Run as -> TestNG suite. The testcases are executed and the summary of test execution is shown in Eclipse's console. TestNG automatically generates report for your test execution. To see the report, go to your project directory, and select test-output folder. Open idex.html. As it can be seen in the below report, Testcase1,2 and 3 are passed. Testcase4 is failed.
Default Test execution Report generated by TestNG
Step 7: So TestNG suite was executed successfully! Now we want to use Ant to run the TestNG testsuite, instead of eclipse. We need to specify location of Selenium server jar and TestNG jar to Ant. Let's create a new folder in the project and put the jar files there.
i. Create a folder 'lib' in your Java project.
ii. Copy Selenium server jar to this 'lib' folder.
iii. Copy testng.jar too, in the same folder. In you Eclipse directory, go to plugins directory. There will be a directory like org.testng.eclipse_18.104.22.16830914_0724. Under this directory, in lib folder you will have testng.jar.
Step 8: Add Ant script with name build.xml to your project as follows.
Step 9: Finally, execute the Ant file. Go to command prompt, go to you project's directory. Type ant. It will start executing the ant script build.xml in your project. When the ant script is executed successfully, view the report generated by TestNG at same location i.e. index.html file in your project's test-output directory.
Happy QA with Selenium
This blog is written by Archana Mulay, Senior QA at Faichi Solutions. Faichi holds expertise in QA & provide end to end QE services to several clients. Automation testing is an integral part of Faichi QA to ensure quality in a speedy and reliable way. We have designed our own test automation framework using SELENIUM and TestNG. This framework enables us to consistently deliver quality solutions across all clients. Know more about our QA Services.
With inputs from Selenium official documentation http://docs.seleniumhq.org/docs.