在 Typescript 中定义动态数组?

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.
      });
    // });
  });
});

测试删除用户的选项:

最终,它是 bad practice to make tests dependent on other tests

也就是说,两个或可能三个选项应该有效:

A:在一次测试中遍历用户列表

describe(`Delete User`, async () => {
    describe(`Confirmation `, () => {
        it(`Log all users out who previously logged in`, async () => {
            list.forEach((item) => {
                console.log(item);
            });
        });
    });
});

由于 list 数组由先前的测试填充,因此在下一个测试中插入依赖于它的代码将确保它具有可使用的值。

B: 在一次测试中登录和删除用户

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 数组。

C:使用模拟数据(如果数据是随机的可能不适用)

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' 之类的内容,并将规范外的所有代码移至规范内。

描述块不应该return承诺

您的代码示例有 describe 个块(特别是 'Login User''Delete User''Confirmation')return Promise。您应该删除函数声明前面的 async 。规格可以而且应该保持不变。例如:

describe(`Login User `, () => {

对象语法

样本开头的测试对象未使用 JS/TS 对象语法。每个键都应该在值之前跟一个冒号,而不是等号。您可能打算写:

const tests = [{
        type: 'admin',
        id: '',
        uname: 'foo',
        pass: 'bar'
    },
    {
        type: 'super',
        id: '',
        uname: 'foo1',
        pass: 'bar'
    },
    {
        type: 'normal',
        id: 'customId',
        uname: 'foo',
        pass: 'bar'
    }
];

来源: