Cypress会话测试第二次不起作用。

huangapple go评论67阅读模式
英文:

Cypress session tests not working for the second time

问题

I am using the following code in cypress version 12 on macOS.

The issue with my test is that it is only running for the first time when the session is running the setup() function, but when I re-run the same test and the session is restored the test does not get executed. (Cypress is unable to find the element).

Cypress error:-
> Timed out retrying after 6000ms: Expected to find element: :nth-child(1) > .link-button, but never found it.

What I have tried so far:-
Custom command for login:-

Cypress.Commands.add('login', (login_email, login_password)=>{
    cy.session('loginSession', ()=>{
        cy.visit('https://www.telerik.com/')
        cy.get('#onetrust-accept-btn-handler').click()
        cy.get('#js-tlrk-nav-drawer-button').click()
        cy.get('#js-tlrk-nav-not-auth-container > .TK-Aside-Menu-Button').click()
        cy.get('#email').type(login_email)
        cy.get('.btn').click()
        cy.get('#password').type(login_password)
        cy.get('.btn').click()
    })
})

spec file code:-

describe('Account page', ()=>{
    beforeEach(()=>{
        cy.login('youremail@gmail.com', 'somepassword')
    })
    it('update profile', ()=>{
        cy.get(':nth-child(1) > .link-button').click()
    })
})

I have also used { testIsolation: false } in my cypress.config.js file.

英文:

I am using the following code in cypress version 12 on mac OS.

The issue with my test is that it is only running for the first time when the session is running the setup() function, but when I re-run the same test and the session is restored the test does not got executed. (cypress is unable to find the element).

Cypress error:-
> Timed out retrying after 6000ms: Expected to find element: :nth-child(1) > .link-button, but never found it.

What I have tried so far:-
Custom command for login:-

Cypress.Commands.add('login', (login_email, login_password)=>{
    cy.session('loginSession', ()=>{
        cy.visit('https://www.telerik.com/')
        cy.get('#onetrust-accept-btn-handler').click()
        cy.get('#js-tlrk-nav-drawer-button').click()
        cy.get('#js-tlrk-nav-not-auth-container > .TK-Aside-Menu-Button').click()
        cy.get('#email').type(login_email)
        cy.get('.btn').click()
        cy.get('#password').type(login_password)
        cy.get('.btn').click()
    })
})

spec file code:-

describe('Account page', ()=>{
    beforeEach(()=>{
        cy.login('youremail@gmail.com', 'somepassword')
    })
    it('update profile', ()=>{
        cy.get(':nth-child(1) > .link-button').click()
    })
})

I have also used { testIsolation: false } in my cypress.config.js file

答案1

得分: 2

以下是您要的翻译内容:

"It's hard to be definitive about this case, but I would try adding an explicit visit inside the test itself."

"这个情况很难确定,但我建议在测试内部添加一个明确的访问。"

"When the session command runs for the first time, you are hitting the login sequence which results in navigation after login to the home page (the one containing the element that fails on the second try)."

"当第一次运行会话命令时,您会触发登录序列,登录后导航到主页(包含第二次尝试失败的元素)。"

"But AFIK on the 2nd call cy.session() only restores the login credentials, but does not perform the "manual" login steps and therefore does not invoke a redirect."

"但据我所知,在第二次调用 cy.session() 时,仅恢复登录凭据,不执行“手动”登录步骤,因此不会触发重定向。"

"You can get a feel for the process described here Where to call cy.visit()"

"您可以在这里了解到描述的过程 Where to call cy.visit()"

"Also, you can leave testIsolation:true when using cy.session(), theoretically at least."

"此外,理论上至少在使用 cy.session() 时,您可以保留 testIsolation:true。"

"Validation function"

"验证函数"

"Another tool in the toolbox is the validation function."

"工具箱中的另一个工具是验证函数。"

"This checks to see if the result of cache restoration (on the 2nd call) is valid. If not it re-runs the setup() function."

"这将检查缓存恢复的结果(在第二次调用时)是否有效。如果无效,它将重新运行 setup() 函数。"

"In your case, that failing element query could be used."

"在您的情况下,可以使用失败的元素查询。"

以上是您所需的翻译。

英文:

It's hard to be definitive about this case, but I would try adding an explicit visit inside the test itself.

When the session command runs for the first time, you are hitting the login sequence which results in navigation after login to the home page (the one containing the element that fails on the second try).

But AFIK on the 2nd call cy.session() only restores the login credentials, but does not perform the "manual" login steps and therefore does not invoke a redirect.

You can get a feel for the process described here Where to call cy.visit()

beforeEach(() => {
  cy.login('email@gmail.com', 'password')
})

it('update profile', () => {
  cy.visit('https://www.somewebsite.com/')       // presume this is the target page
  cy.get(':nth-child(1) > .link-button').click()
})

Also, you can leave testIsolation:true when using cy.session(), theoretically at least.


Validation function

Another tool in the toolbox is the validation function.

This checks to see if the result of cache restoration (on the 2nd call) is valid. If not it re-runs the setup() function.

In your case, that failing element query could be used.

Cypress.Commands.add('login', (login_email, login_password) => {
  cy.session('loginSession', () => {
    cy.visit('https://www.somewebsite.com/')
    cy.get('#js-tlrk-nav-not-auth-container > .TK-Aside-Menu-Button')
      .click()
    cy.get('#email').type(login_email)
    cy.get('.btn').click()
    cy.get('#password').type(login_password)
    cy.get('.btn').click()
  }, 
  {
    validate() {
      cy.get(':nth-child(1) > .link-button').should('exist')
    }
  })
})

huangapple
  • 本文由 发表于 2023年6月13日 14:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定