[英]How to return a value based on fixture value in Cypress
我正在嘗試根據夾具值返回一個值,當我從另一個 class 調用該方法時,我得到的結果是未定義的。
Class A:
getTestEnv() {
let test;
cy.fixture("Properties/env.json").then((profile) => {
var df = profile.test_env;
if (df.match(/d\d+/)) {
test = `random.${df}.test.com`;
} else {
test = `random.${df}.autotest.com`;
}
return test;
});
}
Class B:
getDetails() {
let env = this.getTestEnv();
cy.log("env: " + env); // Undefined
}
我認為您錯過了一些返回值。
第一個方法返回 fixture,然后第二個方法將以與調用cy.fixture()
本身相同的方式調用第一個方法,即它將使用 .then( .then()
從返回值中獲取 fixture 值
Class A:
getTestEnv() {
let test;
return cy.fixture("Properties/env.json").then((profile) => {
var df = profile.test_env;
if (df.match(/d\d+/)) {
test = `random.${df}.test.com`;
} else {
test = `random.${df}.autotest.com`;
}
return test;
});
}
getDetails() {
this.getTestEnv().then(env => {
cy.log("env: " + env);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.