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.

45 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.

  8. Hi Bass what if your API request parameters needs to be populated firsts before you make a request to the webservice, where do I enter those parameters then?

    Can webdriver make a call to the swagger for API testing?

    Thanks in advance

    • Hey Nhlanhla,

      I think you missed the underlying message 🙂 Please never do what I did in the blog post. Instead, use a dedicated API testing tool such as REST Assured or SoapUI.

  9. HI Bas,

    This is Rakesh, working as automation test engineer for almost 7 years, Using selenium for UI automation and Rest Assured for API testing

    In our project we are asked us to converting all(possible) selenium script to API script for functional validation to reduce the execution time.

    I am not sure abt replacing selenium scripts with API scripts and how it will bring value to the project a part from execution time ….
    any suggestion from you side on the same…..

    • Hey Rakesh!

      It all depends on what you are doing with those tests. UI-level tests (using Selenium, for example) and API-level tests (using REST Assured, for example) have very different scopes and goals. Using UI tests for something that can be done with an API level test is waste and should be tackled. But API level tests cannot do everything.

      So, Selenium tests that can be replaced with API-level tests without losing coverage should definitely be replaced. Why? Because API-level tests are generally faster to run, easier to write and easier to maintain. Then you can use Selenium-like tests for everything that cannot be done through the API-level.

      I hope that makes some kind of sense. If not, please do let me know and we’ll take it from there.

  10. Got a real good idea of how two things should be segregated. Thanks.

    Also the questions from different readers in the post were useful as well.

  11. Interviewer had given the scenario asking if I have ever integrated selenium webdriver and API response? So basically the scenario was, based upon the response that we get from api, we need to perform the operations on a webpage. So how do you handle this?

    I told him that i have not faced this scenario. I am aware of api testing and selenium webdriver as independent entities.

    Can you please advice on this scenario ?

    • It’s perfectly fine to combine API calls and browser actions in a single scenario. Just make sure you use a tool like Selenium for the browser bits, and a dedicated API library for the API part.

        • An example would be a test where you would like to check if it’s possible to modify an existing account in a web application. You could then create the account through an API (if there’s an API call that supports doing so, of course), because that’s not actually part of the test but rather creating the right initial state for the test.

          Then the actual modification could be done through the user interface, because that’s what the test is about.

          Single scenario, two different approaches combined.

  12. HI Bas,
    Thanks a lot for clarifying this.Even I though what is purpose of automating REST API using REST Assured using Selenium.

  13. Hi Bas,

    I would like to know how can we do micro service testing using selenium ?Is it a correct approach to go with selenium rest assured for micro service testing ?

    • Selenium is for UI-driven testing of applications that run in a browser. If you want to communicate with a microservice directly, Selenium is not the tool for that. Now, of course, you can use Selenium to test an application based on a microservices architecture at the UI level, but I’m not sure if that’s what you’re looking for.

      If you want to test at the API level, tools like REST Assured (Java), RestSharp (C#) or requests (Python) would be much more efficient for functional testing. But there’s other types of testing as well (performance, security, contract, …)

      • I want to test at API level . So i am planning to use RESTAssured . I am in between integrated testing . how can we trace the each request response when we are psoting integrated service

Leave a Reply to Pooja Dubey Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.