Print

Jackson Json Mapping In Selenium Testing Java

Many web applications provide API for external clients to use in their internal applications. When testing APIs with json response, QA engineers need to verify Json response. The verification includes two parts. (1) What UI displayed should be equal to the API response for the same request input. (2) Verify each message in the Json response. 

When verifying UI against response, you can just simply compare strings, however, for reading each message in the Json response, you need to use Jackson Json Mapper, so that Json parsing will be very easy. 

In this article, we use the Open Weather API as an example to test the web response for this url "api.openweathermap.org/data/2.5/weather?q=London,uk" should be the same as the API response. For verifying UI with API response, please visit related articles on this website. This article mainly focus on how to use Jackson Json Mapper to parse information. 

Step 1: observing Json response. 

Navigate to the site "api.openweathermap.org/data/2.5/weather?q=London,uk" and you will see the following Json response. 

{"coord":{"lon":-0.13,"lat":51.51},"sys":{"type":1,"id":5091,"message":0.1971,"country":"GB","sunrise":1414824893,"sunset":1414859596},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"cmc stations","main":{"temp":285.69,"pressure":1012,"humidity":76,"temp_min":284.15,"temp_max":287.15},"wind":{"speed":4.6,"deg":200},"clouds":{"all":88},"dt":1414881000,"id":2643743,"name":"London","cod":200}

Step 2: observe Json response structure in Json viewer to see its object hierarchy (you can use notepad++ Json viewer) 

After running the following code implementation as JUnit test, you will see the following results. 

Country Name is: GB
Sun rise 2014-11-01 02:54:53 GMT-04:00 Sun Set 2014-11-01 12:33:16 GMT-04:00
Current Date Time is   2014-11-01 18:30:00 GMT-04:00
Main weather is Clouds
City Name is: London
City Id is: 2643743
City coordinate-0.13Longtitude 51.51 Latitude

Project Implementation

Step 3: create a Java Maven project and add dependencies "com.fasterxml.jackson.core","junit",  and "org.seleniumhq.selenium" in the pom.xml file. The file structure looks like this below. 

 

Step 4: write the following code for each java class. 

Print

Test API With Selenium Java

Many web applications provide API for external clients to use in their internal applications. When testing APIs, QA engineers need to verify that what UI displayed should be equal to the API response for the same request input.  In this article, we use the Open Weather API as an example to test the web response for this url "api.openweathermap.org/data/2.5/weather?q=London,uk"   should be the same as the API response. The code is written in Java. 

Step 1: create a Java project as shown below.  In the Build Path, add Selenium and JUnit libraries; download apache jar file from the link "org.jbundle.util.osgi.wrapped.org.apache.http.client-4.1.2.jar" and add the file as External JARS. 

Print

Using Page Factory in Selenium Web Driver

Using Page Factory in Selenium web test automation makes the test easy to manage by defining page object.  In another words, Page Object model is a pattern that displays user interface as a class. In addition to user interface, functionality of the page is also described in this class. The page will be utilized in the test class. In actual implementation, we follow the workflows. 

1. First define some page element fields on a PageObject class. These should be WebElements or List of Web Elements. 

2. Second, define some functionality of the page as method. 

3. Third, in the test class, we need to initialize the PageObject and use its methods.   

In this example below, we verify the test case below. 

Test Case:

Authorized users should be able to change "Maximum Tags in Cloud" in Mahara Demo Site after logging in. 

Implementation

Print

Selenium Database Test

In every web application's back end, there is a database like SQL Server, My SQL, Oracle or other types of database support functions. When using Selenium to automate UI testing, we need to make sure any user data entered from the UI should be captured in the database. We can use database retrieve statement to retrieve data and then use the data to verify what is entered by the user. 

Considering the example of a user registration. User registers himself/herself with a username and password. By establishing a DB connection and retrieving data from the DB, we can validate the username entered in the UI should match the username in the database. 

Test Case: verify that user login information should be saved in the database when a user successfully registered in a site. 

In this screenshot below, first two users were in the database. A new test user is entered from the application UI, so we will verify the username. 

Print

Using Object Map in Selenium Web Driver Automation

In Selenium test automation, defining web page objects in a central place is very important and helpful to maintain the application for any kind of UI changes over time. In this example, we will create an Object Map for Selenium Login Test. We will create a properties file named "objectmap.properties" under a folder named "ObjectPropertiesFile" and store all the page element object definition in this file. When tests are running, the test application reads object locator type and locator value from the file and interact with the web elements. 

 Login Test Case:

1. Go to http://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Step 1: on Java Eclipse, create a project named "SeleniumMasterObjectMap" and add the two classes below. Make sure add Selenium Java webdriver jar files as references in the project build path. 

(1) ObjectMap.java

(2) ObjectMapLoginTest.java (TestNG Class)

and also create a folder named "ObjectPropertiesFile" and add a file named "objectmap.properties". 

Step 2: write the following code in the ObjectMap.java class

Print

Write Selenium Web Driver Test Result To XML File

In some cases, test engineers need to export or save Selenium WebDriver test result in XML file format.  In this example below, I will explain how to write test results as a xml file. 

in this example, the test result is written as a file named "TestResult.xml". 

Login Test Case:

1. Go to http://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Step 1: on Eclipse, create a Java Project called SeleniumMasterLoginTestXmlExport. Add a package named com.seleniummaster.testresultXmlproject and then add three classes as listed below:

  • LoginTest class (this is a TestNG class)
  • TestCases class (this class has object definition for test cases like test case id, test case name and test case status
  • TestResultXmlUtility class (this class creates xml file)

and also, in the build path, add Selenium libraries and Apache Pdfbox library as reference jar files. 

 

Print

How to Generate PDF Report in Selenium WebDriver

In some cases, test engineers need to export or save Selenium WebDriver test result in Pdf file format.  In this example below, I will explain how to write test results as a pdf file. 

Login Test Case:

1. Go to http://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Step 1: on Eclipse, create a Java Project called SeleniumMasterLoginTestPdfExport. Add a package named com.seleniummaster.testresultpdfproject and then add two classes:

  • LoginTest class (this is a TestNG class)
  • PdfUtility class

and also, in the build path, add Selenium libraries and Apache Pdfbox library as reference jar files. 

Step 2: open PdfUtility.java file and write the code below

 

Print

Write Selenium WebDriver TestNG Result to Excel File

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 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://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" 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 "XXXX" in the Password filed
4. Click on the Login button
5. Verify that Account Prefernces page displayed.

Print

Comparing Pdf Documentations With Selenium

Some web applications generate pdf files, so when automating tests, comparing pdf files at the run time is very important. In this example, I explain how to use Selenium Web Driver to compare two pdf files. We have two pdf files, which are stored on the web server. 

http://www.seleniummaster.com/sitecontent/images/Selenium_Master_Test_Case_Base_Template.pdf

http://www.seleniummaster.com/sitecontent/images/Selenium_Master_Test Case_Modified_Template.pdf

The two pdf files above has a small differences. In the Modified_Template.pdf file, I inserted a time stamp at the very bottom of the modified pdf file, so the only different is the time stamp. 

Print

Selenium Web Driver with TestNG in Java

In this example you will learn how to use Selenium WebDriver with TestNG. The key feature of the sample is that you use Selenium Java WebDriver that will launch a browser and run each test sequencially. Each test has description in the test methods. 

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://www.seleniummaster.com/seleniummastertestapp/index.php
2. Enter "test" in the Username field
3. Enter "XXXX" 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 "XXXX" 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 Implementaion