Skip to main content
Skip to content

Selenium and API Testing: What Selenium Can and Cannot Do

“Selenium API testing” is a popular search, and it hides a common misunderstanding. Selenium is built to automate web browsers. It is not an API testing tool. It does not send HTTP requests, check status codes or compare JSON responses. What you can do is combine Selenium with a proper API testing library in one framework, and use Selenium’s own features to observe the network traffic the browser makes. This article clears up the confusion and shows the practical setup. For the basics of the tool itself, start with what is Selenium.

Selenium and API testing

What Selenium is for

Selenium is a suite of tools for browser automation. Selenium WebDriver, the part most people use, lets your code control a real browser: open pages, click buttons, fill in forms and read what is on screen. Selenium Grid runs those tests in parallel across machines, and Selenium IDE records simple flows. All of it works at the user interface level. You can read more in our cross browser testing in Selenium guide and in Selenium pros and cons.

What API testing needs

API testing checks the service layer directly, without a browser. A good API test:

  • sends a request (GET, POST, PUT, DELETE) with headers and a body,
  • checks the status code, response time, headers and the content of the response,
  • covers error cases such as invalid input, missing authentication and rate limits.

Selenium has no built in way to do this. Tools designed for it do.

Better tools for API tests

  • REST Assured (Java): a fluent library for writing API tests in code. It pairs naturally with JUnit or TestNG, which are the same frameworks many Selenium suites use.
  • Postman and its command line runner: visual, quick to start, good for exploration and collection based checks. See Postman automation and the Postman Postbot overview.
  • Playwright, Karate, pytest with requests and similar tools: options if your stack is not Java.

If you are preparing for interviews on this topic, see API testing interview questions and REST Assured interview questions.

How Selenium and API tests work together

The best use of both is in one framework, each doing what it does best:

  1. Set up data through the API. Instead of clicking through ten screens to create a user or an order, create it with an API call, then use Selenium for the one journey you actually want to test. Tests become faster and less flaky.
  2. Verify results through the API. After a UI action such as placing an order, check the backend with an API call to confirm the data was saved correctly.
  3. Clean up through the API. Delete test data after the run so tests stay independent.
  4. Observe network calls from the browser. Selenium 4 supports the browser’s developer tools protocol (CDP, and the newer BiDi in supported browsers). Through it you can capture network requests, listen to responses, or mock a response. This lets a UI test confirm which calls the page made, though it is still not a replacement for dedicated API tests.

Setting up Selenium WebDriver today

You no longer need to download driver files by hand in most cases. From Selenium 4.6 onwards, Selenium Manager finds or downloads a matching browser driver automatically. The basic steps are:

  1. Install a supported browser (Chrome, Firefox or Edge).
  2. Add the Selenium client library for your language to your project (Maven or Gradle for Java).
  3. Write a test with a test framework such as JUnit or TestNG.

The full walkthrough is in how to install Selenium. A simple Java example that opens a page and checks its title:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyFirstTest {
   public static void main(String[] args) {
      // Selenium Manager locates the matching driver automatically (Selenium 4.6+)
      WebDriver driver = new ChromeDriver();
      try {
         driver.get("https://www.example.com");
         String title = driver.getTitle();
         System.out.println(title.contains("Example") ? "Title is correct" : "Title is incorrect");
      } finally {
         driver.quit();
      }
   }
}

In a real suite you would use assertions from your test framework instead of printing, so a wrong title fails the test.

Practices that keep UI and API tests healthy

  • Follow the test pyramid. Keep most checks at the API and unit level, and only a few critical journeys in the browser. See the test automation pyramid.
  • Run in CI. Trigger tests in Jenkins, GitHub Actions or similar on each change, with API tests first because they are fast.
  • Use Selenium Grid or a cloud grid for parallel browser runs.
  • Use real assertions and a reporting library. Selenium itself does not report results; the test framework or a reporting tool such as Allure or ExtentReports does. See Extent Reports in Selenium.
  • Keep code organised. Use the page object pattern for UI code and a separate client layer for API calls.

Frequently asked questions

Can Selenium test APIs?

Not directly. It automates browsers. Use REST Assured, Postman or a similar tool for API tests, and combine them with Selenium in one framework.

Why do some articles say Selenium can send HTTP requests?

Usually because Selenium code runs inside a Java or Python project that also uses an HTTP library. The requests come from that library, not from Selenium.

Can Selenium capture network calls?

Yes, to a degree. Selenium 4 can use the browser’s developer tools protocol to see and modify network traffic during a UI test.

Should I choose Selenium or Cypress or Playwright?

That depends on your languages, browsers and team. See Cypress vs Selenium for a comparison.

Conclusion

Selenium is excellent at driving browsers and poor at testing APIs, because it was never meant to. Use an API tool for service checks, use Selenium for the user journeys that need a real browser, and let the API layer speed up your setup and verification. That combination gives faster, steadier and more meaningful automation.

Want more testing guides in your Google results? Add Testmetry as a preferred source.

Share it :

Leave a Reply