简体   繁体   English

无法使用静态方法返回的类实例访问类方法

[英]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: 问题:

  1. Why the subscriberID is null even though I am setting it in the before block 为什么即使我在before块中设置了SubscriberID也为null
  2. if I print the subscriber Object why am I not seeing subscriberID 如果我打印用户对象,为什么我看不到用户ID

Here is the output of subscriber object 这是订户对象的输出

订户对象

Properties username and password are defined synchronously in before() , so are present on the object when tested. usernamepassword属性是在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.

相关问题 为什么javascript静态变量在不创建最少一个实例的情况下不能访问(使用类名)? - Why javascript static variable not able to access ( using class name ) without creating minimum one instance? 在不使用类实例的情况下访问javascript类的静态属性 - Access the static property of a javascript class without using an instance of the class 为什么 Object class 主要使用 static 方法,而数组 ZA2F2ED4F8EBC2CBB14C21A29DZ 主要使用实例方法? - Why is the Object class using mostly static methods while the Array class is mostly having instance methods? 创建 class 的实例以调用 static 方法 - Creating an instance of a class to call static methods 如何使用静态方法从类中访问变量? - How to access variables from class using static methods? 在 typescript 中使用另一个 class 中的 static class 方法 - using static class methods in another class in typescript 在其静态方法内访问类 - Access a class inside its static methods 在返回的 class 上执行方法而不使用变量 - Execute methods on returned class without using variable 在 javascript class 上使用 static 方法 - using static method on javascript class 在JavaScript类中调用父方法,但stll可以访问对象实例内部的原型方法吗? - Call parent method in JavaScript class but stll have access to prototype methods inside object instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM