簡體   English   中英

無法使用靜態方法返回的類實例訪問類方法

[英]not able to access class methods using the class instance returned by a static method

我已經創建了一個訂戶類來存儲訂戶詳細信息,並使用靜態方法返回該類的實例,但是我無法使用該實例設置值

這是訂戶類:

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;
    }
}

我在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());
});

這是賽普拉斯控制台上的輸出

柏

問題:

  1. 為什么即使我在before塊中設置了SubscriberID也為null
  2. 如果我打印用戶對象,為什么我看不到用戶ID

這是訂戶對象的輸出

訂戶對象

usernamepassword屬性是在before()同步定義的,因此在測試時會出現在對象上。

但是subscriberId是異步獲取的,因此您需要等待測試內的完成,例如

cy.wrap(Subscriber.getSubscriber()).should(function(subscriber){
  expect(subscriber.getSubsciberId()).not.to.be.null
})

請參考wrap-Objects,以了解如何使用Cypress命令處理對象。

應該-差異

另一方面,當將回調函數與.should()或.and()一起使用時,有特殊的邏輯來重新運行該回調函數,直到其中沒有任何斷言。

換句話說, should重試(最多5秒鍾),直到回調內部的expect不會失敗(即,在您的情況下,異步調用已完成)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM