当前位置:网站首页>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 .
边栏推荐
猜你喜欢
【MySQL】数据库进阶之触发器内容详解
Novice entry SCM must understand those things
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Greenplum 6.x build_ install
ncs成都新电面试经验
Arm GIC (IV) GIC V3 register class analysis notes.
NCS Chengdu New Electric interview Experience
Introduction to data fragmentation
Greenplum6.x搭建_环境配置
随机推荐
Other 7 features of TCP [sliding window mechanism ▲]
FPGA knowledge accumulation [6]
Composer change domestic image
为什么要选择云原生数据库
联想混合云Lenovo xCloud:4大产品线+IT服务门户
leetcode134. gas station
【踩坑】nacos注册一直连接localhost:8848,no available server
let const
String operation
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Three series of BOM elements
IP地址的类别
关于基于kangle和EP面板使用CDN
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
QT charts use (rewrite qchartview to realize some custom functions)
Gson converts the entity class to JSON times declare multiple JSON fields named
leetcode135. Distribute candy
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?