How (not) to test RESTful APIs with Selenium WebDriver

I’ve seen the question of how to do RESTful API testing with Selenium WebDriver come up a lot in various places. I’ve seen it mainly on LinkedIn, but a couple of weeks ago I was asked this question in person as well. So, as your ever helpful consultant, I thought I’d be a good idea to show you how it’s done.

First, let’s create a new browser driver object. For the sake of simplicity, I’ll use Firefox, because it doesn’t require setting up a separate driver. And yes, I know implicit waits aren’t the best waiting strategy, but I didn’t feel like writing an ExpectedCondition.

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Now, instead of browsing to a web page, we’ll simply browse to the RESTful API endpoint for which we want to check the response. I’ll use as an example here to look up some data for the US zip code 90210:


This gives us the following output:

Response from the API in JSON format

Let’s grab the text we see on screen and convert it to a JSON object (using the org.json package):

WebElement element = driver.findElement(By.xpath("//pre"));
JSONObject jsonObject = new JSONObject(element.getText());

Now that we have a JSON object containing the API response, we can extract specific elements and check their value:

String valueToCheck = jsonObject.get("country").toString();
Assert.assertEquals(valueToCheck, "United States");

As a final step, throw everything away, never do this again and please forget everything you’ve seen so far in this post. Selenium is not an API testing tool. It has never been, and it will never be. So please don’t try and force it to be.

Please stick to simulating UI interaction when using Selenium and use a dedicated, fit for purpose tool for API testing. As an example, REST Assured can perform the exact same check as the above. With a single line of code. Which is far better readable to boot:

public void doRestTestProperly() {
		body("country", equalTo("United States"));

So, here’s to hoping I did my part in exterminating questions that feature ‘Selenium’ and ‘API testing’ in the same sentence.

21 thoughts on “How (not) to test RESTful APIs with Selenium WebDriver

  1. Pingback: Java Web Weekly, Issue 144 | Baeldung

  2. “As a final step, throw everything away, never do this again” …..this was the best part.

    Hi Bas,
    I am still relatively new to API Testing. I have a query how to get the ‘RESTful API endpoint’.

    And as always a very helpful aritcle.

  3. Pingback: Testing Bits – 9/25/16 – 10/1/16 | Testing Curator Blog

  4. Hi Bas,

    Enjoyed your article. I wanted a doubt clarified and got it via “Selenium is not an API testing tool. It has never been, and it will never be. So please don’t try and force it to be.”


  5. After reading this article, I still don’ t understand why Selenium is not an API testing tool. It seems work very well. If I want to test RESTful API with browser cookies, Selenium is a good approach than API testing tool, isn’t it?

    • No.

      It’s like killing and consuming an elephant with a teaspoon. It might work in some cases, but that doesn’t mean it’s the way you should proceed.

      What about HTTP POSTs? PUTs? DELETEs? Not going to work. And I’m not even starting on how you’ll be creating slow and clunky tests for something that can be easily done with a proper API testing tool.

      Please, don’t.

  6. Hi Bas,

    Great!. This explanation cleared my so far doubts. The way you started and ended to explain this stuff is stunning.


  7. So I need to test a site that allows a user to enter a keyword and get a response. Behind the scenes is a simple rest api that returns json data and then displays the response nicely on the page.

    The rest api is getting data from multiple sources.

    I need to use the rest api to get the json, then go to the source sites and enter the keyword and compare fields in the json to the responses on the page.

    Then I need to validate the data displayed on the page for the user with the json data.

    I can use selenium to validate the data. Is there any way to invoke the api and get the json other than doing a 2 part test… use a rest tool, save the json and then use selenium for the browser compares.

    • That sounds like a perfect candidate for splitting up into separate tests.. One (set of) test(s) that checks whether the API response contains the correct data, one (set of) test(s) that checks whether data that’s being returned is shown on the screen correctly. You don’t have to do all of that in a single test.

Leave a Reply

Your email address will not be published. Required fields are marked *