当前位置:网站首页>selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
2022-07-07 06:12:00 【软件测试呀】
问题
当我们描述一个“好的自动化测试用例”时,经常出现标准是:
精确
自动化测试用例应该测试一件事,只有一件事。与测试用例无关的应用程序的某个部分中的错误不应导致测试用例失败
独立
自动化测试用例不应该受测试套件中任何其他测试用例影响
快速
这不需要太多的解释。自动化测试用例套件运行得越快,它就越有用
这些标准在Selenium测试套件中可能特别具有挑战性。原因在于,Selenium的UI自动化测试通常涉及可能包括注册,登录,某些导航,表单提交或与网站的其他交互的设置。只有在做完这些事情之后,你才可以在网站的某些方面做出断言。但是这样就会引入一些和原本的测试用例无关的错误导致自动化脚本运行失败。
02、解决方法
现在很多新的网站的后端都采用REST API。如果能够在脚本中通过访问REST API来完成一些基础的必要操作,而不是在UI上,这样将提高我们测试的执行效率。
REST API基于HTTP协议,这是基本上支持整个互联网的协议。几乎所有现代编程语言都有可用于发出HTTP请求的库,因此,我们可以将调用REST API的支持添加到几乎任何测试套件中。
03、实例
很多测试中都会遇到新建账号的问题。需要测试新的账号在登陆之后的行为。如果能够使用REST API来新建用户的话,就可以大大的节省填写新建账号的表单的时间。
这里假设一个虚拟的REST API
POST http://api.myfakeapp.com/v1/create-user
通过POST方法可以新建用户。
Body里面需要填充的JSON数据如下:
{
'username': 'example-username',
'password': 'abcd1234',
'email': '[email protected]',
'first_name': 'bob',
'last_name': 'example'
}
下面是代码实现:
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、总结
这里只是给出了一个如何把API访问和UI自动化结合的思路。在这个思路的引导下,可以对UI自动化测试进行很多扩展,例如校验界面数据元素的数据和API中数据的一致性等等。
房子要一层一层盖,知识要一点一点学。大家在学习过程中要好基础,多上手实操,话不多说,这里狠狠上一次干货!我熬夜整理好的各阶段(功能、接口、自动化、性能、测开)技能学习资料+实操讲解,非常适合私下里学习,比找资料自学高效多了,分享给你们。
领取关 w/x/g/z/h:软件测试小dao
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
边栏推荐
- Greenplum6.x搭建_环境配置
- IP地址的类别
- 使用AGC重签名服务前后渠道号信息异常分析
- Category of IP address
- Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
- leetcode135. Distribute candy
- Three usage scenarios of annotation @configurationproperties
- [Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
- MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
- Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
猜你喜欢

详解华为应用市场2022年逐步减少32位包体上架应用和策略

Three series of BOM elements

Virtual address space

Introduction to data fragmentation

How to realize the high temperature alarm of the machine room in the moving ring monitoring system
![[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources](/img/e3/3703bdace2d0ca47c1a585562dc15e.jpg)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources

In go language, function is a type

如何在HarmonyOS应用中集成App Linking服务

Implementation of navigation bar at the bottom of applet

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
随机推荐
2 - 3 arbre de recherche
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Input of mathematical formula of obsidan
SSM integration
opencv之图像分割
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
mysql分区讲解及操作语句
National SMS center number inquiry
Novice entry SCM must understand those things
Greenplum 6.x common statements
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
指针进阶,字符串函数
leetcode134. gas station
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
POJ - 3616 Milking Time(DP+LIS)
go写一个在一定时间内运行的程序
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
Qt Charts使用(重写QChartView,实现一些自定义功能)
Composer change domestic image
注解@ConfigurationProperties的三种使用场景