我有一个需求,我想在一个循环中读取一个特定的值x(每次都是自动生成的),比如说n次。现在,我想存储这些自动生成的x值,所以,我以后可以使用它们,并迭代它来执行我的测试(protractor)。
我想做的方法是通过创建一个数组,使用 let list: string[] = [];
. 现在,我正在使用推送值到我定义的列表。list.push[x];
在每次迭代中。循环结束后,希望得到我的x(string)的n个值的数组。list
数组。为了验证,我做了 console.log(list);
中,我可以看到这些值被推送到定义的 list
.
后来,在我的代码中,如果我试图使用 let item = list[0];
我得到的是 undefined
值。
我想我需要初始化数组到一些特定大小的默认值,然后在循环中修改它们。但是,作为新的TypeScript,我无法找到一个解决方案,如何做到这一点。请帮助,TIA!
这里,是下面的片段。
const tests = [
{type: 'admin', id='', uname='foo', pass='bar'},
{type: 'super', id='', uname='foo1', pass='bar'},
{type: 'normal', id='customId', uname='foo', pass='bar'}
];
let list: string[] = [];
// let list = [ //this is the final list that i got from the console.log(list);
// 'QR417msytVrq',
// 'V0fxayA3FOBD',
// 'QnaiegiVoYhs'];
describe(`Open Page `, () => {
//Code to get to the page
beforeAll(async () => {
//initialize page objects
});
describe(`Login User `, async () => {
tests.forEach(test => {
it(` should login user with `+test.type, async () => {
//....
//....
// On Success
const myId = userPage.getUID().getText();
list.push(myId);
console.log(list);
console.log(list.length);
});
});
});
describe(`Delete User`, async () => {
// describe(`Confirmation `, async () => {
console.log(list);
// list.forEach(item => { //this code doesn't gets executed and wasn't giving any error, so, commented out and tried to access the first element which is undefined.
let item = list[0];
console.log(item); //getting undefined value here.
it(` should select and Delete the User having id as ` + item, async () => {
//code to remove the user having id as item.
});
// });
});
});
最终,它是... 挂羊头卖狗肉.
说到这里,有两个或可能有三个选项应该是可行的。
describe(`Delete User`, async () => {
describe(`Confirmation `, () => {
it(`Log all users out who previously logged in`, async () => {
list.forEach((item) => {
console.log(item);
});
});
});
});
由于 list
数组是由前一个测试填充的,在下一个测试中插入依赖于它的代码将确保它有值可以使用。
describe(`Login and delete user `, async () => {
tests.forEach(test => {
it(` should login and delete user with ` + test.type, async () => {
const myId = userPage.getUID().getText();
// Select and delete myId here
});
});
});
您可能可以删除 list
完全是通过将整个用户流放到一个大型集成测试中。
describe(`Delete User`, async () => {
const list = ["QR417msytVrq", "V0fxayA3FOBD", "QnaiegiVoYhs"];
describe(`Confirmation `, () => {
list.forEach((item) => {
it(
` should select and Delete the User having id as ` + item,
async () => {}
);
});
});
});
如果你提前知道要删除的值是什么,你可以手动添加进去。如果数值是随机生成的,这就不行了。
你所使用的动态数组语法看起来是正确的,但是你的测试中似乎有一个执行顺序的问题。
代码中的 describe
功能的规格之外的功能(该产品的 it
块)在任何规范内的代码之前被执行。测试框架将遍历 describe
块,执行它发现的任何代码,但只注意到了 it
规格。当它完成了这些,它就会执行 it
它发现的规格按顺序排列。
当你试图保存 list[0]
జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ 'Login User'
规格尚未执行。更具体地说。
describe(`Login User `, async () => {
tests.forEach(test => {
it(` should login user with ` + test.type, async () => {
// This code is executed AFTER the code in the 'Delete User'
// block but BEFORE the 'Delete User' spec
const myId = userPage.getUID().getText();
list.push(myId);
});
});
});
describe(`Delete User`, async () => {
// This code is executed before any specs are run
let item = list[0];
// List is [] when item is initialized
// The following spec will therefore not work as item is undefined
it(` should select and Delete the User having id as ` + item, async () => {
});
});
一个可能的解决方法是改变这个问题的字符串 'Delete User'
规格为类似 ' should select and Delete first User'
以及将所有规范外的代码移到规范内。
您的代码示例有 describe
块 'Login User'
, 'Delete User'
和 'Confirmation'
),它们返回Promises。你应该删除 async
前的函数声明。规格可以也应该保持不变。例如
describe(`Login User `, () => {
你的示例开头的测试对象没有使用JSTS对象语法。每个键的后面都应该是冒号,而不是等号。你的意思可能是写。
const tests = [{
type: 'admin',
id: '',
uname: 'foo',
pass: 'bar'
},
{
type: 'super',
id: '',
uname: 'foo1',
pass: 'bar'
},
{
type: 'normal',
id: 'customId',
uname: 'foo',
pass: 'bar'
}
];
Sources: