[英]Defining dynamic Array in Typescript?
我有一个要求,我想在 n 次循环中读取特定值 x(每次自动生成)。 现在,我想存储这些自动生成的 x 值,以便以后可以使用它们并对其进行迭代以执行我的测试(量角器)。
我想做的方法是创建一个数组,使用let list: string[] = [];
. 现在,我正在使用list.push[x];
将值推送到我定义的列表中。 在每次迭代中。 在循环结束时,期望在我的list
数组中获得具有 n 个 x(string) 值的数组。 为了验证,我做了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
了返回 Promises 的块(特别是'Login User'
、 'Delete User'
和'Confirmation'
)。 您应该删除 function 声明前面的async
。 规格可以而且应该保持不变。 例如:
describe(`Login User `, () => {
示例开头的测试 object 未使用 JS/TS object 语法。 每个键的值前应该跟一个冒号,而不是等号。 你可能打算写:
const tests = [{
type: 'admin',
id: '',
uname: 'foo',
pass: 'bar'
},
{
type: 'super',
id: '',
uname: 'foo1',
pass: 'bar'
},
{
type: 'normal',
id: 'customId',
uname: 'foo',
pass: 'bar'
}
];
资料来源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.