In my current project, I have a specific requirement that involves reading an auto-generated value "x" in a loop for a certain number of times (let's say "n"). I need to store these auto-generated values of "x" so that I can later use them for performing tests, particularly with Protractor.
To achieve this, I decided to create an Array in TypeScript by initializing it as let list: string[] = [];
. During each iteration, I am pushing the values of "x" to this list using list.push[x];
. By the end of the loop, I expect to have an Array with "n" values of "x" stored in my list
. To validate this, I added console.log(list);
in each iteration and confirmed that the values are being successfully pushed to the list
.
However, when I try to access these elements using let item = list[0];
, I am encountering an issue where the value returned is undefined
.
I believe that I may need to initialize the Array with a specific size and default values initially, and then modify them during the loop. As a newcomer to TypeScript, I am struggling to find a solution for this problem. Any assistance would be greatly appreciated. Thank you in advance!
Below is a snippet of the code:
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 get 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.
});
// });
});
});