Selenium Webdriver-如何避免数据重复 [英] Selenium Webdriver - How to avoid data duplication

查看:407
本文介绍了Selenium Webdriver-如何避免数据重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有要使用Java脚本自动执行的常规添加用户"模块,如何避免数据重复以避免诸如用户已存在"之类的错误消息?

Suppose I have normal "Add Users" module that I want to automate using Java scripting, how I can avoid data duplication to avoid error message such as "User already exist"?

推荐答案

可以通过多种方式使之自动化.由于您正在(可能)使用 static 运行'Add Users'测试用例,因此您收到 用户已经存在" > 变量.

There are numerous ways in which this can be automated. You are receiving 'User already exists' due to fact that you're (probably) running your 'Add Users' test cases using static variables.

注意:对于以下示例,我将考虑新用户的基本 注册 流程/场景:namepassword是必填字段. 说明002:我选择的语言是JavaScript.您应该能够轻松地用Java重现该概念.

Note: For the following examples I will consider a basic registration flow/scenario of a new user: name, email, password being the required fields. Note-002: My language of choice will be JavaScript. You should be able to reproduce the concept with Java with ease.

1.).在要提交的信息之前或之后添加 唯一 标识符(例如:Date()返回自1970年1月1日起经过的秒数=>在运行测试用例时,它始终是唯一的)

1.) Pre-pending/Post-pending a unique identifier to the information you're submitting (e.g.: Date() returns the number of seconds that have elapsed since January 1, 1970 => it will always be unique when running your test case)

var timestamp = Number(new Date());
var email = 'test.e2e' + timestamp + '@<yourMainDomainHere>'

注意:通常,name& password不必唯一,因此您实际上可以使用精雕细琢的值而不会出现任何问题.

Note: Usually, the name & password don't need to be unique, so you can actually use hardcoaded values without any issues.

2.)也可以使用Math.random()(对于JS)实现相同的操作,该返回的值在0到1(0.8018194703223693)之间,长度为18位数字.

2.) The same thing can also be achieved using the Math.random() (for JS), which returns a value between 0 and 1 (0.8018194703223693), 18 digits long.

var almostUnique = Math.random();
// You can go ahead and gen only the decimals
almostUnique = almostUnique.toString().split('.')[1];
var email = 'test.e2e' + almostUnique + '@<yourMainDomainHere>'

!!! 警告:虽然Math.random()实际上不是 ,但是在200个功能测试用例的数百次回归运行中,我没有机会看到副本.

!!! Warning: While Math.random() is not actually unique, in hundreds of regression runs of 200 functional test cases, I didn't have the chance of seeing a duplicate.

3.)(不太优雅|难以指数实现)如果您可以访问网络应用程序后端API ,并可以通过它执行不同的操作在数据库中,您实际上可以编写一些脚本,这些脚本将在 注册 测试用例之后运行,例如 cleanup 套件.

3.) (Not so elegant | Harder exponentially harder to implement) If you have access to your web-apps backend API and through it you can execute different actions in the DB, then you can actually write yourself some scripts that will run after your registration test cases, like a cleanup suite.

这些脚本必须从数据库中删除以前添加的用户.

These scripts will have to remove the previously added user from your database.

希望这会有所帮助!

这篇关于Selenium Webdriver-如何避免数据重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆