当前位置:网站首页>Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you

Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you

2022-07-07 08:51:00 Software testing

problem

When we describe a “ Good automated test cases ” when , The standard that often appears is :

accurate

Automated test cases should test one thing , There is 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

It doesn't need much explanation . The faster the automated test case suite runs , The more useful it is

These standards are in Selenium The test suite can be particularly challenging . The reason lies in ,Selenium Of UI Automated testing usually involves registration , Sign in , Some navigation , Settings for form submission or other interaction with the website . Only after doing these things , You can make assertions in some aspects of the website . But this will introduce some errors that are not related to the original test case, resulting in the failure of the automation script .

02、 resolvent

Now many new websites use REST API. If you can access in a script REST API To complete some basic necessary operations , Not in UI On , This will improve the execution efficiency of our tests .

REST API be based on HTTP agreement , This is basically the whole Internet protocol . Almost all modern programming languages can be used to emit HTTP Requested Library , therefore , We can call REST API Add support 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 it can be used REST API To create new users , It can greatly save the time of filling in the form of new account .

Let's assume a virtual REST API

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

adopt POST Method to create a new user .

Body It needs filling JSON The data are as follows :

{ 

 'username':   'example-username', 

 'password':   'abcd1234', 

 'email':      '[email protected]', 

 'first_name': 'bob', 

 'last_name':  'example'

}

The following is the code implementation :

require '
rest-cli
ent'
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 example of how to put API Access and UI Idea of combining automation . Under the guidance of this idea , It can be done to UI Automated testing makes many extensions , For example, verify the data sum of interface data elements API Consistency of data in .

The house needs to be built layer by layer , Knowledge needs to be learned at one point one . We should lay a good foundation in the process of learning , More hands-on practice , Don't talk much , The last dry goods here ! I stayed up late to sort out the stages ( function 、 Interface 、 automation 、 performance 、 Test open ) Skills learning materials + Practical explanation , Very suitable for studying in private , It's much more efficient than self-study , Share with you .

Get off w/x/g/z/h: Software testing tips dao

Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .

原网站

版权声明
本文为[Software testing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070611469153.html