How to add REST API operations to UI automation testing

1. Question

When we describe a "good automated test case", the criteria that often come up are:

accurate

Automated test cases should test one thing and one thing only. A bug in a part of the application that is not relevant to the test case should not cause the test case to fail.

independent

Automated test cases should not be affected by any other test cases in the test suite.

fast

This doesn't require much explanation. The faster an automated test case suite runs, the more useful it is.

These criteria can be particularly challenging in Selenium test suites. The reason is that UI automation testing for Selenium typically involves setup that may include registration, login, some navigation, form submission, or other interactions with the website. Only after doing these things can you make assertions about certain aspects of your website. However, this will introduce some errors unrelated to the original test cases, causing the automation script to fail.

2. Solution

Many new websites now use REST API in their backends. If we can complete some basic necessary operations by accessing the REST API in the script instead of on the UI, this will improve the execution efficiency of our tests.

The REST API is based on the HTTP protocol, which is the protocol that basically powers the entire Internet. Almost all modern programming languages ​​have libraries available for making HTTP requests, so we can add support for calling REST APIs to almost any test suite.

3. Examples

Many tests will encounter the problem of creating new accounts. It is necessary to test the behavior of the new account after logging in. If you can use the REST API to create a new user, you can greatly save the time of filling out the form for creating a new account.

This assumes a virtual REST API

POST http://api.myfakeapp.com/v1/create-user

Users can be created through the POST method. The JSON data that needs to be filled in the Body is as follows:

{

  'username':   'example-username',
  'password':   'abcd1234',
  'email':      '[email protected]',
  'first_name': 'bob',
  'last_name':  'example'
}

The following is the code implementation

require 'rest-client'require 'json'require 'securerandom'class RestApiInterface

  @headers = {
    'content-type' => 'application/json',
    'user-agent' => 'Rest Api Helper',
  }

  def post_to_api url, post_body_obj
    json_body = JSON.generate(post_body_obj)
    response = RestClient.post url, json_body, @headers
  end

  def create_test_user
    # Step 1: Build the user parameters randomly
    random_test_user = {
      'username'   => random_string,
      'password'   => random_string,
      'email'      => "#{random_string}@testing.com",
      'first_name' => 'test',
      'last_name'  => 'user',
    }

    # Step 2: Execute the API call
    response = post_to_api "http://api.myfakeapp.com/v1/create-user", random_test_user    # Step 3: Ensure the api call returned a success code
    if response.code != '200'
      raise 'User creation failed'
    end

    # Final Step: Return the user object so we can use it
    response.body['user']['data']
  end

  def random_string
    # This is an easy way to get a good randomized string
    SecureRandom.hex  endend$driver = Selenium::WebDriver.for :firefoxuser = RestApiInterface.new.create_test_user$driver.get 'http://myfakeapp.com'$driver.find_element(:css, 'input[name="username"]').send_keys @user['username']$driver.find_element(:css, 'input[name="password"]').send_keys @user['password']$driver.find_element(:css, 'button[name="login"]').click
puts $driver.find_element(:css, '#user_id').text

4. Summary

Here is just an idea on how to combine API access with UI automation. Under the guidance of this idea, many extensions can be made to UI automated testing, such as verifying the consistency of data in interface data elements and data in APIs, etc.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you! 

Guess you like

Origin blog.csdn.net/lzz718719/article/details/132716642