You're right. selenium automation integrates REST api practice

Posted by greenba on Thu, 24 Feb 2022 08:22:29 +0100

problem

When we describe a "good automated test case", the common criteria are:

  • accurate

    Automated test cases should test one thing, only one thing. An error in a part of an application that is not related to a 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 does not require much explanation. The faster the automated test case suite runs, the more useful it becomes

These standards can be particularly challenging in Selenium test suites. The reason is that Selenium's UI automation test usually involves settings that may include registration, login, some navigation, form submission or other interaction with the website. Only after doing these things can you make assertions in some aspects of the website. However, this will introduce some errors unrelated to the original test cases, resulting in the failure of the automatic script.

02. Solutions

Now many new websites use rest APIs at the back end. If we can access the REST API in the script to complete some basic necessary operations, rather than on the UI, it will improve the execution efficiency of our tests.

REST API is based on HTTP protocol, which basically supports the whole Internet. Almost all modern programming languages have libraries that can be used to issue HTTP requests, so we can add support for calling rest APIs to almost any test suite.

03. Example

Many tests will encounter the problem of creating new accounts. You need to test the behavior of the new account after logging in. If you can use the REST API to create new users, you can greatly save the time of filling in the form of new account.

Assume a virtual REST API

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

You can create a new user through the POST method.

The JSON data to be filled in the Body is as follows:

{ 

 'username':   'example-username', 

 'password':   'abcd1234', 

 'email':      'bob@example.com', 

 '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 

 end

end

$driver = Selenium::WebDriver.for :firefox

user = 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

04. Summary

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

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times!

Finally, learning resources such as basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, advanced test Python programming, Web automation test, APP automation test, interface automation test, test advanced continuous integration, test architecture development test framework, performance test, security test, etc.

Topics: software testing