Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Python REST API Response With UI Verification

In Selenium Python web test automation, we may need to verify REST API response against UI displayed content. In other words, what the REST API returned should be the same as what the UI displayed on the webpage. In this example, I use the open weather data api for testing. We load the api url on the webpage and get the api response for the same url and compare the response with the UI content. Here is the URL;

http://api.openweathermap.org/data/2.5/weather?q=Baltimre,us&APPID=70926ddfd37fdf454548b8db13695995

When we open the above URL with the Chrome browser, we get the following content on the UI. 

 

 
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"cmc stations","main":{"temp":279.219,"pressure":1025.61,"humidity":65,"temp_min":279.219,"temp_max":279.219,"sea_level":1032.41,"grnd_level":1025.61},"wind":{"speed":4.17,"deg":255.501},"clouds":{"all":0},"dt":1456610617,"sys":{"message":0.0102,"country":"US","sunrise":1456573287,"sunset":1456613832},"id":4347778,"name":"Baltimore","cod":200

In our test, we verify that (1) in the UI content, the city name should be "Baltimore"; (2) API response should be the same as UI displayed text;

Create a Python file and save it as "SeleniumPythonRestApiUiUnitTest.py" and write the following script. 

import unittest
import json
import time
import urllib2
from selenium import webdriver
class RestApiUiTest(unittest.TestCase):

    def setUp(self):
        #http://api.openweathermap.org/data/2.5/weather?q=Baltimre,us&APPID=70926ddfd37fdf454548b8db13695995
        #define Api URL and API Key
        self.ApiUrl = "http://api.openweathermap.org/data/2.5/weather"
        self.ApiKey="70926ddfd37fdf454548b8db13695995"
        #define browser instance Chrome
        self.driver=webdriver.Chrome()
        #maximize browser window
        self.driver.maximize_window()
        print "Current Test: "+self._testMethodName

    def test_weather_api_by_city_name1(self):
        """Get weather data by city name and assert on the string output"""
        driver=self.driver
        #navigate to the UI
        driver.get(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        time.sleep(5)
        #get the ui element
        ui_element=driver.find_element_by_tag_name("pre")
        #get the weather data
        weather_data=ui_element.text
        print weather_data
        #assert response
        self.assertTrue("Baltimore" in weather_data)

    def test_weather_api_by_city_name2(self):
        """Get weather data by city name and assert on the JSON output"""
        driver=self.driver
        #navigate to the UI
        driver.get(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        time.sleep(5)
        #get the ui element
        ui_element=driver.find_element_by_tag_name("pre")
        #get the weather data
        weather_data=ui_element.text
        print weather_data
        #loads text as json
        json_data=json.loads(weather_data)
        #get the key "name" value
        city_name=json_data["name"]
        print("city name is:"+city_name)
        #assert city name
        self.assertTrue(city_name=="Baltimore")
        
    def test_weather_api_ui_comparison(self):
        """Compare UI and API Output"""
        driver=self.driver
        #navigate to the UI
        driver.get(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        time.sleep(5)
        #get the ui element
        ui_element=driver.find_element_by_tag_name("pre")
        #get the weather data
        weather_data=ui_element.text
        print weather_data
        #get api response
        testurl=(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        print testurl
        response=urllib2.urlopen(testurl)
        #assert ui display with api response
        self.assertEqual(response.read(),weather_data)
    def tearDown(self):
        self.driver.close()
        print " ----- test is over ---------- "
        
if __name__ == "__main__":
    unittest.main()

Run the above script with the command line below and observe the test result on the console. All 3 tests passed.

C:\Python27>python SeleniumPythonRestApiUiUnitTest.py
Current Test: test_weather_api_by_city_name1
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":280.38,"pressure":1019,"humidity":38,"temp_min":279.15,"temp_max":281.75},"visibility":16093,"wind":{"speed":2.6,"deg":240},"clouds":{"all":1},"dt":1456611805,"sys":{"type":1,"id":1328,"message":0.0423,"country":"US","sunrise":1456573287,"sunset":1456613832},"id":4347778,"name":"Baltimore","cod":200}
 ----- test is over ----------
.Current Test: test_weather_api_by_city_name2
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":280.38,"pressure":1019,"humidity":38,"temp_min":279.15,"temp_max":281.75},"visibility":16093,"wind":{"speed":2.6,"deg":240},"clouds":{"all":1},"dt":1456611805,"sys":{"type":1,"id":1328,"message":0.0423,"country":"US","sunrise":1456573287,"sunset":1456613832},"id":4347778,"name":"Baltimore","cod":200}
city name is:Baltimore
 ----- test is over ----------
.Current Test: test_weather_api_ui_comparison
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":280.38,"pressure":1019,"humidity":38,"temp_min":279.15,"temp_max":281.75},"visibility":16093,"wind":{"speed":2.6,"deg":240},"clouds":{"all":1},"dt":1456611805,"sys":{"type":1,"id":1328,"message":0.0423,"country":"US","sunrise":1456573287,"sunset":1456613832},"id":4347778,"name":"Baltimore","cod":200}
http://api.openweathermap.org/data/2.5/weather?q=Baltimore,us&APPID=70926ddfd37fdf454548b8db13695995
 ----- test is over ----------
.
----------------------------------------------------------------------
Ran 3 tests in 24.057s

OK

C:\Python27>