Selenium 2 WebDriver Advanced
  • Preface
  • 1. API Testing Using Selenium WebDriver
    • 1.1 Code Snippet for API testing
    • 1.2 SoapUI
      • 1.2.1 Add selenium jars to soapUI
      • 1.2.2 Run selenium tests
      • 1.2.3 Data Driven Testing using SoapUI
  • 2. Advanced Profiles for Browsers
    • 2.1 Profile for Firefox Browser
    • 2.2 Profile for Chrome Browser
    • 2.3 Profile for IE Browser
    • 2.4 Profile for Safari Browser
    • 2.5 Profile for Opera Browser
  • 3. TestNG
    • 3.1 Sample TestNG class
    • 3.2 Parallel execution
    • 3.3 Execution of tests from batch file
  • 4. ExtentReports
    • 4.1 Basics
    • 4.2 Sample Extent Reports
  • 5. Advanced tricks
    • 5.1 Keystroke handling
    • 5.2 Screenshot capture
    • 5.3 Get HTML Source of WebElement
Powered by GitBook
On this page

Was this helpful?

  1. 1. API Testing Using Selenium WebDriver

1.1 Code Snippet for API testing

Previous1. API Testing Using Selenium WebDriverNext1.2 SoapUI

Last updated 5 years ago

Was this helpful?

Here i am just conferring how to parse and How to convert JsonObject to Java Object.So that you could use it your live projects using selenium.

Application contentType : Json

Objective : To validate Address : Chicago, IL, USA.

GoogleMaps API :

We can easily parse Json using “json-lib-2.4-jdk15.jar” Download from here…!

Please consider the below code as a reference snippet:

package code;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class ReadJsonObject {

    @Test
    public void aptTesting() throws Exception {
    try {
        URL url = new URL(“http://maps.googleapis.com/maps/api/geocode/json?address=chicago&sensor=false&#;);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod(“GET”);
        conn.setRequestProperty(“Accept”, “application/json”);

        if (conn.getResponseCode() != ) {
            throw new RuntimeException(” HTTP error code : ”+ conn.getResponseCode());
        }
        Scanner scan = new Scanner(url.openStream());
        String entireResponse = new String();
        while (scan.hasNext())
        entireResponse += scan.nextLine();
        System.out.println(“Response : “+entireResponse);
        scan.close();

        JSONObject obj = new JSONObject(entireResponse );
        String responseCode = obj.getString(“status”);
        System.out.println(“status : ” + responseCode);

        JSONArray arr = obj.getJSONArray(“results”);
        for (int i = 0; i < arr.length(); i++) {
            String placeid = arr.getJSONObject(i).getString(“place_id”);
            System.out.println(“Place id : ” + placeid);
            String formatAddress = arr.getJSONObject(i).getString(“formatted_address”);
            System.out.println(“Address : ” + formatAddress);

            //validating Address as per the requirement
            if(formatAddress.equalsIgnoreCase(“Chicago, IL, USA”))
            {
                System.out.println(“Address is as Expected”);
            }
            else
            {
                System.out.println(“Address is not as Expected”);
            }
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

That’s it, now you know how to convert JsonObject to Java Object and use it in your Selenium snippet.

You can practice using API :

http://maps.googleapis.com/maps/api/geocode/json?address=chicago&sensor=false
http://restcountries.eu/rest/v1