[英]not able to access class methods using the class instance returned by a static method
I have created a subscriber class to store subscriber details and use a static method to return the instance of the class, but I am not able to set the values using the instance 我已经创建了一个订户类来存储订户详细信息,并使用静态方法返回该类的实例,但是我无法使用该实例设置值
Here is the subscriber class: 这是订户类:
let _instance;
export class Subscriber {
constructor(username, password) {
this._username = username;
this._password = password;
}
setSubscriberId(subscriberId) {
cy.log(subscriberId);
this._subscriberId = subscriberId;
}
setSessionId(sessionId) {
this.sessionId = sessionId;
}
getUserName = () => {
return this._username;
}
getPassword = () => {
return this._password;
}
getSubsciberId() {
return this._subscriberId;
}
getSessionId() {
return this.sessionId;
}
static createSubscriber(username, password) {
if (!_instance) {
_instance = new Subscriber(username, password);
}
return _intance;
}
static getSubscriber() {
return _instance;
}
}
I am creating a instance of the class in before
block and accessing the instance in Given
block 我在
before
块中创建该类的实例,并在Given
块中访问该实例
before("Create a new subscriber before the tests and set local storage", () => {
const username = `TestAutomation${Math.floor(Math.random() * 1000)}@sharklasers.com`;
const password = "test1234";
subscriberHelpers.createSubscriber(username, password, true).then((response) => {
cy.log(response);
Subscriber.createSubscriber(username, password);
Subscriber.getSubscriber().setSubscriberId(response.Subscriber.Id);
Subscriber.getSubscriber().setSessionId(response.SessionId);
}).catch((error) => {
cy.log(error);
});
});
Given(/^I launch selfcare app$/, () => {
cy.launchApp();
});
Given(/^I Set the environemnt for the test$/, () => {
cy.log(Subscriber.getSubscriber());
cy.log(Subscriber.getSubscriber().getSubsciberId());
});
here is the output on the cypress console 这是赛普拉斯控制台上的输出
Questions: 问题:
before
block before
块中设置了SubscriberID也为null Here is the output of subscriber object 这是订户对象的输出
Properties username
and password
are defined synchronously in before()
, so are present on the object when tested. username
和password
属性是在before()
同步定义的,因此在测试时会出现在对象上。
But subscriberId
is obtained asynchronously, so you will need to wait for completion inside the test, eg 但是
subscriberId
是异步获取的,因此您需要等待测试内的完成,例如
cy.wrap(Subscriber.getSubscriber()).should(function(subscriber){
expect(subscriber.getSubsciberId()).not.to.be.null
})
Refer to wrap - Objects to see how to handle an object with Cypress commands. 请参考wrap-Objects,以了解如何使用Cypress命令处理对象。
and see should - Differences 看应该-差异
When using a callback function with .should() or .and(), on the other hand, there is special logic to rerun the callback function until no assertions throw within it.
另一方面,当将回调函数与.should()或.and()一起使用时,有特殊的逻辑来重新运行该回调函数,直到其中没有任何断言。
In other words, should
will retry (up to 5 seconds) until the expect
inside the callback does not fail (ie in your case the async call has completed). 换句话说,
should
重试(最多5秒钟),直到回调内部的expect
不会失败(即,在您的情况下,异步调用已完成)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.