I have a TypeScript class where I declared a global variable and used it in a method of the same class. Here is an example:
First TypeScript Class:
export class FirstTestDetails {
baseProductDetails = {
baseQty: null,
basePrice: null,
};
public async calculateTotalPrice() {
await this.txtBaseQty.getAttribute('value').then((baseQty) => {
this.baseProductDetails.baseQty = baseQty;
console.log('Base Qty ==> ' + this.baseProductDetails.baseQty);
});
}
}
In the above code snippet, the correct value is assigned to the global variable (this.baseProductDetails.baseQty). However, when trying to access the same variable in another class, it returns a 'null' value. Could you please provide guidance?
Second TypeScript Class:
Import {FirstTestDetails} from '../pages/firsttest.page';
const firstTest = new FirstTestDetails();
export class SecondTestDetails {
async validateQtyFromFirstTest() {
await this.txtBaseQty.getAttribute('value').then((baseQty) => {
if (firstTest.baseProductDetails.baseQty === baseQty) {
console.log('Quantities match between First and Second classes');
}
});
}
}
However, I am getting a 'null' value for 'firstTest.baseProductDetails.baseQty' instead of the actual value printed in the FirstTestClass function. Is there a way to read the global variable of the first class in the second class? Your assistance is greatly appreciated!
Additionally, my test scenario looks like this:
import { SecondTestClass } from '../pages/xxxxxxx';
const firstTest = new FirstTestClass();
const secondTest = new SecondTestClass();
describe('Qty check', () => {
describe('Check qty in first test', () => {
it('As User1 - Check qty in first test', async () => {
await firstTest.calculateTotalPrice();
expect(xxxxx).toBeTruthy();
});
it('As User1 - Compare qty in second test with first test', async () => {
await secondTest.validateQtyFromFirstTest();
expect(xxxxx).toBeTruthy();
});
});
});