In this example you will learn how to use Selenium WebDriver
with TestNG and write test result into an Excel file with Apache POI
library. The key feature of the sample is that you use Selenium
Java WebDriver that will launch a browser and run each test
sequentially. Each test has description in the test methods. After test
is done, you can see test result both in TestNG output html file and an
Excel file as shown below.
Test 1 (launchSiteAndLogin)
1. Go to http://seleniumsubbu.blogspot.in/index.php
2. Enter "test" in the Username field
3. Enter "xyz" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.
Test 2 (openUserSettingPage)
1. Click on the Settings link on the top of the page
2. Enter "test" in the Username field
3. Enter "xyz" in the Password filed
4. Click on the Login button
5. Verify that Account Prefernces page displayed.
Test 3 (ChangeUserSettings)
1. Click on the radio button near friends need my authorization to add me
2. Click on the save button
3. Verify that the text "preference saved" displayed.
Test4 (Logout)
1. Click on the Logout button
2. Verify that Login button displayed.
You also need to add poi-3.9-20121203.jar (Apache poi jar) file in the Build Path as external JARs.
Step 2: write the UserSettingTest.java code as follows
Step 3: Create a testng.xml file to run the test suite as TestNG Suite.
Step 4: right click on the testng.xml file and select run as TestNG Suite.
Step 5: Check the test result on the console or in the test-output folder. Generally, open the index.html under the test-output folder to view the result.
The result file indicated that all the tests passed. With this pattern, you can expand the test suite with many test modules.You can also see the TestResult.xls file to view the test result. When you open the excel file, you will see the test result as shown below.
Test Step Id | Action | Expected Result | Actual Result |
1 | navigate to site and login | site opens and login success | Pass |
2 | navigate to User Settings Page | Page Displayed | Pass |
3 | User can change settings | Settings changed | Pass |
4 | User can logout | Logout successfull | Pass |
Model your test cases
Before you start automation, you need to know what are the
tests and what you are going to validate. Let's break the test suite
into 4 test cases. Test 1 (launchSiteAndLogin)
1. Go to http://seleniumsubbu.blogspot.in/index.php
2. Enter "test" in the Username field
3. Enter "xyz" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.
Test 2 (openUserSettingPage)
1. Click on the Settings link on the top of the page
2. Enter "test" in the Username field
3. Enter "xyz" in the Password filed
4. Click on the Login button
5. Verify that Account Prefernces page displayed.
Test 3 (ChangeUserSettings)
1. Click on the radio button near friends need my authorization to add me
2. Click on the save button
3. Verify that the text "preference saved" displayed.
Test4 (Logout)
1. Click on the Logout button
2. Verify that Login button displayed.
Automation Implementation
Step 1: Create a Java Project named
"SeleniumMasterWebDriverTestNg", add a package named
"com.seleniummaster.testsuite". In the build path, add the
selenium-java-2.39.0.jar file and all the jar files under the lib folder
as a reference. Add the TestNG class named UserSettingTest and add
@BeforeClass and @AfterClass annotations. See the code in Step 2 for
reference.You also need to add poi-3.9-20121203.jar (Apache poi jar) file in the Build Path as external JARs.
package com.seleniummaster.testsuite; |
Step 4: right click on the testng.xml file and select run as TestNG Suite.
Step 5: Check the test result on the console or in the test-output folder. Generally, open the index.html under the test-output folder to view the result.
The result file indicated that all the tests passed. With this pattern, you can expand the test suite with many test modules.You can also see the TestResult.xls file to view the test result. When you open the excel file, you will see the test result as shown below.
Test Step Id | Action | Expected Result | Actual Result |
1 | navigate to site and login | site opens and login success | Pass |
2 | navigate to User Settings Page | Page Displayed | Pass |
3 | User can change settings | Settings changed | Pass |
4 | User can logout | Logout successfull | Pass |