最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extract and use a string value returned from cy.wrap() - Stack Overflow

programmeradmin2浏览0评论

Cypress sees my returned strings as objects, so I'm trying to use cy.wrap() to resolve the value as string.

I have a cypress custom mand, like so:

Cypress.Commands.add('emailAddress', () => {
    var emailAddress = 'testEmail-' + Math.random().toString(36).substr(2, 16) + '@mail';
    return cy.wrap(emailAddress);
})

That I need the return value as a sting in my test:

beforeEach(() => {
        var user = cy.emailAddress().then(value => cy.log(value)); // [email protected]
        logonView.login(user) // object{5}
      })

How do I use the string value for my login and elsewhere in my test?

Something like: logonView.login(user.value) ... but this doesn't work?

Cypress sees my returned strings as objects, so I'm trying to use cy.wrap() to resolve the value as string.

I have a cypress custom mand, like so:

Cypress.Commands.add('emailAddress', () => {
    var emailAddress = 'testEmail-' + Math.random().toString(36).substr(2, 16) + '@mail.';
    return cy.wrap(emailAddress);
})

That I need the return value as a sting in my test:

beforeEach(() => {
        var user = cy.emailAddress().then(value => cy.log(value)); // [email protected]
        logonView.login(user) // object{5}
      })

How do I use the string value for my login and elsewhere in my test?

Something like: logonView.login(user.value) ... but this doesn't work?

Share Improve this question edited Jul 28, 2020 at 6:54 Joshua 3,2063 gold badges26 silver badges41 bronze badges asked Jul 27, 2020 at 17:32 Benny MeadeBenny Meade 61212 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

In Cypress, you cannot return value like this

var user = cy.emailAddress().then(value => cy.log(value));

Instead, you get the return value in the then .then callback:

cy.emailAddress().then((value) => {
  logonView.login(user)
});

So, for you test, you can instead do the following:

describe("My test", () => {
  beforeEach(() => {
    cy.emailAddress().then((value) => {
      logonView.login(user)
    });
  });

  it("should have logged into the App", () => {
    // Write your test here
  });
});

Or use a variable in the before each block, and access it later in the test:

describe("element-one", () => {
  let user;

  beforeEach(() => {
    cy.emailAddress().then((value) => (user = value));
  });

  it("it should have user value", () => {
    expect(user).to.includes("testEmail");
  });
});
发布评论

评论列表(0)

  1. 暂无评论