在柏树规格中等待cookie

发布于 2025-02-09 12:09:31 字数 1072 浏览 1 评论 0原文

我在柏树测试中阅读cookie的读数有问题,

/// <reference types="cypress" />

describe('cookie', () => {
    beforeEach(() => {
        cy.setCookie('abc', 'def');
    });

    it('read', () => {
        let cookieValue: string;
        cy.getCookie('abc').should('exist').then(c => cookieValue = c.value);
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});

我从最后一个中出现了的错误: 预期不确定的不确定

“

编辑1

我的目标是同步具有cookie值。我设法拥有工作代码:

  let cookieValue: string;
  it('read', async () => {
    cy.wrap(cookieValue).should('equal', undefined);
    cookieValue = await new Promise(resolve => cy.getCookie('abc').should('exist').then(c => resolve(c.value)));
    cy.wrap(cookieValue).should('not.equal', undefined);
  });

但看起来并不柏树。还有其他解决方案吗?

I have problem with reading cookie in my Cypress test

/// <reference types="cypress" />

describe('cookie', () => {
    beforeEach(() => {
        cy.setCookie('abc', 'def');
    });

    it('read', () => {
        let cookieValue: string;
        cy.getCookie('abc').should('exist').then(c => cookieValue = c.value);
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});

I have error from last should:
expected undefined to not equal undefined

Cypress console

Edit 1

My aim is to have cookie value synchronically. I managed to have working code:

  let cookieValue: string;
  it('read', async () => {
    cy.wrap(cookieValue).should('equal', undefined);
    cookieValue = await new Promise(resolve => cy.getCookie('abc').should('exist').then(c => resolve(c.value)));
    cy.wrap(cookieValue).should('not.equal', undefined);
  });

but it doesn't look cypress-way. Any other solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远昼 2025-02-16 12:09:31

则可以将cookie存储在变量中,同时设置该cookie ,然后在theeach块中提取,使您具有同步值。

describe('cookie', () => {
   let cookieValue: string;

    beforeEach(() => {
        cy.setCookie('abc', 'def');
        cookieValue = 'def'
    });

    it('read', () => {
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});

如果已经将cookie设置为测试,

describe('cookie', () => {
   let cookieValue: string;

    beforeEach(() => {
        cy.getCookie('abc').should('exist')
          .then(c => cookieValue = c.value);
    });

    it('read', () => {
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});

You can store the cookie in a variable at the same time as setting it

describe('cookie', () => {
   let cookieValue: string;

    beforeEach(() => {
        cy.setCookie('abc', 'def');
        cookieValue = 'def'
    });

    it('read', () => {
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});

If the cookie is already set going in to the test, extracting in beforeEach block gives you a synchronous value.

describe('cookie', () => {
   let cookieValue: string;

    beforeEach(() => {
        cy.getCookie('abc').should('exist')
          .then(c => cookieValue = c.value);
    });

    it('read', () => {
        cy.wrap(cookieValue).should('not.equal', undefined);
    });
});
执手闯天涯 2025-02-16 12:09:31

命令是异步的,您需要在内包装和测试.。

cy.getCookie('abc').should('exist').then(c => 
  cookieValue = c.value
  cy.wrap(cookieValue).should('not.equal', undefined);
);

柏树 以cookievalue的值为例,在排队运行之前,它具有最初的空值。


您可以使用普通JavaScript同步获取与测试文档相关的所有cookie。

由于返回值是字符串,因此您可能需要解析更复杂的值,例如对象。

const document = cy.state('document')
const cookieValue = document.cookie
  .split('; ')
  .find(row => row.startsWith('abc='))
  ?.split('=')[1];

您也可以等待cy.now()

it('takes the cookie', async () => {
  const cookieObject = await cy.now('getCookie', 'abc')
  /* Properties
    domain: "localhost"
    expiry: 2287046766
    httpOnly: false
    name: "abc"
    path: "/"
    secure: false
    value: "my-cookie-value"
  */
  const cookieValue = cookieObject.value

The Cypress commands are async, you need to wrap and test inside the .then()

cy.getCookie('abc').should('exist').then(c => 
  cookieValue = c.value
  cy.wrap(cookieValue).should('not.equal', undefined);
);

More precisely, Cypress commands run in a queue, but cy.wrap(cookieValue) will take the value of cookieValue before the queue runs, i.e it has the original empty value when enqueued.


You can synchronously get all the cookies related to test document using plain javascript.

Since the return value is a string you may need to parse more complex values such as objects.

const document = cy.state('document')
const cookieValue = document.cookie
  .split('; ')
  .find(row => row.startsWith('abc='))
  ?.split('=')[1];

You can also await cy.now()

it('takes the cookie', async () => {
  const cookieObject = await cy.now('getCookie', 'abc')
  /* Properties
    domain: "localhost"
    expiry: 2287046766
    httpOnly: false
    name: "abc"
    path: "/"
    secure: false
    value: "my-cookie-value"
  */
  const cookieValue = cookieObject.value
掩于岁月 2025-02-16 12:09:31

您可以使用Alias 来保存cookie值,然后 在外面访问它,然后像这样。要记住的一件事是,这些别名将在同一测试中起作用,因为柏树清除了测试之间的别名。如果要在整个测试中访问该值,我建议您使用cypress.env(),在下面说明。

/// <reference types="cypress" />

describe('cookie', () => {
  beforeEach(() => {
    cy.setCookie('abc', 'def')
  })

  it('read', () => {
    let cookieValue: string
    cy.getCookie('abc')
      .should('exist')
      .then((c) => {
        cy.wrap(c.value).as('cookieValue')
      })
    cy.get('@cookieValue').then((cookieValue) => {
      expect(cookieValue).to.not.be.undefined
    })
  })
})

您也可以使用cypress.env在全球范围内保存cookie值并在整个项目中使用它。

/// <reference types="cypress" />

describe('cookie', () => {
  beforeEach(() => {
    cy.setCookie('abc', 'def')
  })

  it('read', () => {
    let cookieValue: string
    cy.getCookie('abc')
      .should('exist')
      .then((c) => {
        Cypress.env('cookieValue', c.value)
      })
    cy.wrap(Cypress.env('cookieValue')).should('not.equal', undefined)
  })
})

You can use an alias as to save the cookie value and then access it outside then like this. One thing to remember is the aliases will work within the same test, because cypress clears aliases between tests. If you want to access the value throughout the tests, I would suggest you to use Cypress.env(), explained below.

/// <reference types="cypress" />

describe('cookie', () => {
  beforeEach(() => {
    cy.setCookie('abc', 'def')
  })

  it('read', () => {
    let cookieValue: string
    cy.getCookie('abc')
      .should('exist')
      .then((c) => {
        cy.wrap(c.value).as('cookieValue')
      })
    cy.get('@cookieValue').then((cookieValue) => {
      expect(cookieValue).to.not.be.undefined
    })
  })
})

You can also use Cypress.env to globally save the cookie value and use it anywhere throughout the project.

/// <reference types="cypress" />

describe('cookie', () => {
  beforeEach(() => {
    cy.setCookie('abc', 'def')
  })

  it('read', () => {
    let cookieValue: string
    cy.getCookie('abc')
      .should('exist')
      .then((c) => {
        Cypress.env('cookieValue', c.value)
      })
    cy.wrap(Cypress.env('cookieValue')).should('not.equal', undefined)
  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文