我可以使用cy.session()而不致电cy.visit()吗?

发布于 2025-02-10 23:00:37 字数 1012 浏览 1 评论 0原文

我是赛普拉斯的新手,如果问题不足,则很抱歉。简短描述我要实现的工作流程:

    describe('user selects products and navigates away from page', () => {
      //1
      it('user logs in and selects products', () => {
})
      //2
      it('user proceeds to compare selected products'), => {
})
      //3
      it ('user navigates away from comparison page, then back to comparison'), => {
})  
    })

这是登录函数的样子:

    Cypress.Commands.add("openAndLogin", () => {
        cy.session('login', () => {
            cy.VisitHomePage()
            cy.get('#siteHeaderTopRight').find('button.signin-btn').should('contain.text', 'Sign In').click() 
            cy.userLogin()
        })
})

第一个iT-block运行良好,但是第二次会引发错误t进入用户的产品选择。我也不能在第二块中使用cy.visit()。

因此,我的问题是:如果您在上一页上取决于您的操作的页面之间移动,是否可以使用cy.session()?在这种情况下,我不能在每个IT块中称为cy.visit(),我可以吗?

我在某个地方读到,每个IT块都应该能够独立运行,但是将整个流程放在单个iT块中似乎也不正确。想法?

I'm new to Cypress, so apologies, if the question is inadequate. Short description of the workflow that I'm trying to achieve:

    describe('user selects products and navigates away from page', () => {
      //1
      it('user logs in and selects products', () => {
})
      //2
      it('user proceeds to compare selected products'), => {
})
      //3
      it ('user navigates away from comparison page, then back to comparison'), => {
})  
    })

This is what the login function looks like:

    Cypress.Commands.add("openAndLogin", () => {
        cy.session('login', () => {
            cy.VisitHomePage()
            cy.get('#siteHeaderTopRight').find('button.signin-btn').should('contain.text', 'Sign In').click() 
            cy.userLogin()
        })
})

The first it-block runs fine, but the 2nd throws an error because I get stuck at some cy.session related intermittent screen and can't get to the user's product selection. I also can't use cy.visit() in the 2nd block.

So my question here is this: is it possible to use cy.session() if you're moving between pages that are dependent on your actions on the previous page? I can't call cy.visit() in each it-block in this scenario, can I?

I read somewhere that each it-block should be able to run independently, but putting the whole flow in a single it-block somehow doesn't seem right, either. Thoughts?

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

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

发布评论

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

评论(2

野稚 2025-02-17 23:00:37

您包装在cy.session()中的任何内容都会在运行期间缓存。

首次在内部完成所有内容并缓存所得的cookie,存储等。

这意味着当您调用cy.openandlogin() 但是恢复所有缓存的值。

关键是,每个测试都应调用cy.openandlogin()

尝试在会话代码中添加console.log(),它只能显示一次。

Cypress.Commands.add("openAndLogin", () => {
  cy.session('login', () => {

    console.log('Calling sign-in')

    cy.VisitHomePage()
    cy.get('#siteHeaderTopRight').find('button.signin-btn')
      .should('contain.text', 'Sign In').click() 
    cy.userLogin()
  })
})


Anything you wrap in a cy.session() is cached for the duration of the run.

That means when you call cy.openAndLogin() the first time it does all the stuff inside and caches the resulting cookies, storage, etc.

The second time you call it, it does not repeat the code, but restores all the cached values.

The key thing is, each test should make a call to cy.openAndLogin().

Try adding a console.log() inside the session code, it should only show once.

Cypress.Commands.add("openAndLogin", () => {
  cy.session('login', () => {

    console.log('Calling sign-in')

    cy.VisitHomePage()
    cy.get('#siteHeaderTopRight').find('button.signin-btn')
      .should('contain.text', 'Sign In').click() 
    cy.userLogin()
  })
})


花开雨落又逢春i 2025-02-17 23:00:37

是的,您可以使用此功能,Aftereach测试获取您的URL,然后在每个测试中进行CY.Visit(New URL)

cy.location().then((url) => {
            url = url.href
        })

Yes, you can use this, afterEach test get your url and then in each test make cy.visit(new url)

cy.location().then((url) => {
            url = url.href
        })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文