Question

After writing some web tests with Selenium, I need now to test REST APIs. I would like to drive my browser with Selenium and to verify the results displayed in my web browser with the results generated when I launch my API urls. My API URLs are in HTTP mode and the answer will be in JSON format.

I have found rest-assured : https://code.google.com/p/rest-assured/ But before jumping and use it I would like to know if it's easy to integrate it with my selenium tests and if there's a better alternative.

Thanks

Was it helpful?

Solution

You can force Selenium to wait for results to become available in the DOM. So yes, Selenium is a good tool to use to test your REST calls especially if your results update the HTML displayed on the page.

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

See the following page for more examples.

OTHER TIPS

It is ofcourse possible to integrate REST API to WebDriver. I have worked on this and it works like a charm.

Service Layer Function:

/**
 * This function will retrieve the child nodes under the current TG element
 * @param webDriver The WebDriver reference
 * @param element The Web Element whose child nodes are to be retrieved
 * @return List<WebElement> The list of Child Elements for element. 
 * @throws Exception 
 */ 
public static List<WebElement> getChildElements(WebElement element,WebDriver webDriver) throws Exception {          
    try{
        List<WebElement> elementChilds = element.findElements(By.xpath(GlobalTreeGridValues.TreeGrid.TG_Shared_NextLevelXPath)); 
        System.out.println("Avaialble child nodes for the current node are "+ element.findElements(By.xpath(GlobalTreeGridValues.TreeGrid.TG_Shared_NextLevelXPath)).size());
        return elementChilds;
        }catch(ElementNotFoundException ex){
        System.out.println("Error in getting the XPath "+  ex.getMessage());
        return null;
    }       
}

In order to read this data into a java object, we just need json formatters and libraries like httpclient etc.

Read http://code.google.com/p/json-io/ for more info!

I think Selenium is more for UI Testing. For testing REST APIs there are better tools for this in my opinion. You can use tools for REST API Testing like RestCase, Runscope or vRest. There are also many rest clients available like Postman and etc..

As discussed by Krishnam, this can absolutely be done by using httpclient or similar. I have found it very useful to utilise the combination of Selenium and httpclient API code rather than separate API testing tools, for the following reasons:

  1. Ability to run website UI tests that get verified by calling an API and then reading the content returned
  2. Ability to run API calls that push data to the server, and running Selenium UI code to verify the results on the website
  3. The ease of using a json serialiser to look at objects returned from the API easily
  4. The ability to do anything custom in the verification. In my case, I've written a database access layer to my selenium suite to easily test values have been saved correctly in the database
  5. In my case, running APIs sometimes requires a huge amount of setup (or potential mocking). On the system I test, this setup can be done without too much difficulty using Selenium webdriver commands on the website prior to running the API command. (For example on my system, the creation of users and activating their account, which isn't currently available via our API, and would be a nightmare to mock or setup in the database).

Obviously every system under test is different, but these are some of the benefits that may help you ascertain whether this is the right approach for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top