在赛普拉斯中创建一个随机字符串并将其传递给 cy 命令
Creating a random string in Cypress and passing this to a cy command
我是 Cypress 的新手,有一个小问题需要帮助。
我的应用程序中有一个输入字段,可让我输入姓名。此名称必须是唯一的,并且不能与系统中已有的名称相同。
我目前正在通过以下方式点击此输入字段:
cy.get('input[type="text"].form-control')
如果我使用 cy.type()
命令,这将始终键入提供的相同值,但每次测试运行时,我都想分配一个不同的值。
// Fill in some details of a new class to proceed with creation
cy.get('.pull-left > h4').contains('Add a new class')
cy.get('input[type="text"].form-control') // Clicks on the field
// Some code to go here to create a random string and use what was created and
type this into the field above
预计
创建一个允许生成随机字符串的函数,然后通过普通的 cypress 命令将其输入到输入字段中。
我在这里做了一些假设。我假设您调用某种 API 来检查是否存在重名。我会 stub/mock 绕过它。我在这里猜测,但你传递了这个名字,你得到的东西说真或假,你存根总是 return 假,所以你可以做你的副本。
我创建了一个生成随机字符串的函数,然后创建了一个变量来存储这个值,然后在其余测试的逻辑中使用该值。
function generate_random_string(string_length) {
let random_string = '';
let random_ascii;
for(let i = 0; i < string_length; i++) {
random_ascii = Math.floor((Math.random() * 25) + 97);
random_string += String.fromCharCode(random_ascii)
}
return random_string
}
然后我将其分配给下面的变量:
var random_string = generate_random_string(8)
然后从中提取输出并通过在 Cypress 中使用简单的 get
和 type
命令将其放入字段中:
cy.get('input[type="text"].form-control').type(random_string)
这会获取值并将其键入到我想要的字段中。我也可以在任何测试中再次 "random_string",例如,如果我希望稍后在测试中做一些断言。
试试这个code.Hope这会起作用。
cy.get(':nth-child(2) > :nth-child(2) > input').type(userID_Alpha())
function userID_Alpha() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
或使用以下代码
cy.get(':nth-child(2) > :nth-child(2) > input').type(userID_Alpha_Numeric())
function userID_Alpha_Numeric() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
example.spec.js
it('timestamp', function() {
cy.task('datenow').then((random) => { cy.log('test' + random)})
})
plugins/index.js
on('task', {
datenow () {
return Date.now()
}
})
以上代码会生成随机字符串
刚刚在博客中找到了另一种方法,在此添加以供参考。
const uuid = () => Cypress._.random(0, 1e6)
const id = uuid()
const testname = `testname${id}`
cy.get('input').type(testname);
对我来说效果很好:)
鉴于您每毫秒需要少于 1 个 id,在并行环境中不需要唯一值,并且您不是时间旅行者,您可以使用 Date.now()
。
如果您每毫秒需要超过 1 个 id,您可以使用 Date.now()
作为 Cypress._.uniqueId()
的种子:
const uniqueSeed = Date.now().toString();
const getUniqueId = () => Cypress._.uniqueId(uniqueSeed);
it('uses a unique id', () => {
const uniqueId = getUniqueId();
});
有更好的方法。检查此 link 以获得完整的包详细信息。 https://www.npmjs.com/package/faker
首先通过运行以下npm命令添加Faker库。
npm install faker
接下来在你的Cypress自动化测试输入数据到系统的任何地方调用相关的faker数据生成命令。
const faker = require("faker");
cy.get('input').type(faker.random.alphaNumeric());
我是 Cypress 的新手,有一个小问题需要帮助。
我的应用程序中有一个输入字段,可让我输入姓名。此名称必须是唯一的,并且不能与系统中已有的名称相同。
我目前正在通过以下方式点击此输入字段:
cy.get('input[type="text"].form-control')
如果我使用 cy.type()
命令,这将始终键入提供的相同值,但每次测试运行时,我都想分配一个不同的值。
// Fill in some details of a new class to proceed with creation
cy.get('.pull-left > h4').contains('Add a new class')
cy.get('input[type="text"].form-control') // Clicks on the field
// Some code to go here to create a random string and use what was created and
type this into the field above
预计
创建一个允许生成随机字符串的函数,然后通过普通的 cypress 命令将其输入到输入字段中。
我在这里做了一些假设。我假设您调用某种 API 来检查是否存在重名。我会 stub/mock 绕过它。我在这里猜测,但你传递了这个名字,你得到的东西说真或假,你存根总是 return 假,所以你可以做你的副本。
我创建了一个生成随机字符串的函数,然后创建了一个变量来存储这个值,然后在其余测试的逻辑中使用该值。
function generate_random_string(string_length) {
let random_string = '';
let random_ascii;
for(let i = 0; i < string_length; i++) {
random_ascii = Math.floor((Math.random() * 25) + 97);
random_string += String.fromCharCode(random_ascii)
}
return random_string
}
然后我将其分配给下面的变量:
var random_string = generate_random_string(8)
然后从中提取输出并通过在 Cypress 中使用简单的 get
和 type
命令将其放入字段中:
cy.get('input[type="text"].form-control').type(random_string)
这会获取值并将其键入到我想要的字段中。我也可以在任何测试中再次 "random_string",例如,如果我希望稍后在测试中做一些断言。
试试这个code.Hope这会起作用。
cy.get(':nth-child(2) > :nth-child(2) > input').type(userID_Alpha())
function userID_Alpha() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
或使用以下代码
cy.get(':nth-child(2) > :nth-child(2) > input').type(userID_Alpha_Numeric())
function userID_Alpha_Numeric() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
example.spec.js
it('timestamp', function() {
cy.task('datenow').then((random) => { cy.log('test' + random)})
})
plugins/index.js
on('task', {
datenow () {
return Date.now()
}
})
以上代码会生成随机字符串
刚刚在博客中找到了另一种方法,在此添加以供参考。
const uuid = () => Cypress._.random(0, 1e6)
const id = uuid()
const testname = `testname${id}`
cy.get('input').type(testname);
对我来说效果很好:)
鉴于您每毫秒需要少于 1 个 id,在并行环境中不需要唯一值,并且您不是时间旅行者,您可以使用 Date.now()
。
如果您每毫秒需要超过 1 个 id,您可以使用 Date.now()
作为 Cypress._.uniqueId()
的种子:
const uniqueSeed = Date.now().toString();
const getUniqueId = () => Cypress._.uniqueId(uniqueSeed);
it('uses a unique id', () => {
const uniqueId = getUniqueId();
});
有更好的方法。检查此 link 以获得完整的包详细信息。 https://www.npmjs.com/package/faker
首先通过运行以下npm命令添加Faker库。
npm install faker
接下来在你的Cypress自动化测试输入数据到系统的任何地方调用相关的faker数据生成命令。
const faker = require("faker");
cy.get('input').type(faker.random.alphaNumeric());