【问题标题】:Cypress: Page doesn't load within the second 'it' block for the click action赛普拉斯:页面未在点击操作的第二个“it”块内加载
【发布时间】:2020-12-01 16:11:19
【问题描述】:

如果我在下面的单个“it”块中进行编码,它对于相同的点击事件可以正常工作。

工作代码:

describe('Test Suite', () => {
    it('Test case 1', () => {
        //Test 1
        //Test 2
    })
})

不工作:

describe('Test Suite', () => {
    it('Test case 1', () => {
        //Test 1
    })
    it('Test case 2', () => {
        //Test 2
    })
})

下面是我的代码sn-p,第一个'it'块在登录方法执行后工作正常。然后它会阻止仅单击正确的元素,但页面永远不会加载。

P.S.如果我在单个“it”块下编写代码,页面会加载并正常工作。

describe('Fund Manager Suite', () => {

    //Checking Fund Manager page loading 
   before(() => {

    cy.visit('xxxxxxxxxxxxx')
    cy.login('xxxxx', 'xxxxx')

   })
   
   it('fund manager navigation works', () => {
    cy.location('pathname').should('equal', '/xxxxx')
    cy.get('#appSwitcher').click()

    cy.get('#appSwitcher > .dropdown > .dropdown-menu > :nth-child(2) > a').click()
    cy.location('pathname').should('equal', '/xxxxx')
    cy.get('.k-grid-table').find('tr').should('have.length', 5)
   })

   it('fund detail works', () => {
    cy.get('.product > :nth-child(2)').click()
    cy.location('pathname').should('equal', '/xxxxx')

    // Fund Detail - Search
    cy.get('#s2id_autogen31').type('Rach')
    cy.get('#select2-result-label-32').click()
    cy.get('#searchSubmit').click()
    cy.get('#DataTables_Table_0').find('tr').should('have.length', 10)
       
   })

}) 

Execution Screen shot Code snippet screen shot

【问题讨论】:

  • 你有before()beforeEach() 块吗。
  • @AlapanDas I'm have before(() => {。因为我想保持登录状态,无需在第二次测试中重新运行登录场景。
  • @AlapanDas 尝试使用 Cypress.Cookies.preserveOnce('session_id', 'remember_token') 仍然无法加载页面。如果我用 1 'it' 块编写相同的代码,它工作正常。
  • 请为下次测试添加一些代码 - 你的问题就像一张空白表格。如果它包含一些个人数据 - 请努力编辑数据,以便人们可以帮助您
  • @RosenMihaylov 我已经更新了上面的代码 sn-p。请检查一下。

标签: javascript automation mocha.js cypress


【解决方案1】:

您必须将您的 cookie 保存在 beforeEach() 中,以确保您在所有 it() 块中保持登录状态。你可以在cypress docs阅读更多内容。

describe('Dashboard', () => {
  before(() => {
    // log in only once before any of the tests run.
    // your app will likely set some sort of session cookie.
    // you'll need to know the name of the cookie(s), which you can find
    // in your Resources -> Cookies panel in the Chrome Dev Tools.
    cy.login()
  })

  beforeEach(() => {
    // before each test, we can automatically preserve the
    // 'session_id' and 'remember_token' cookies. this means they
    // will not be cleared before the NEXT test starts.
    //
    // the name of your cookies will likely be different
    // this is an example
    Cypress.Cookies.preserveOnce('session_id', 'remember_token')
  })

  it('displays stats', () => {
    // ...
  })

  it('can do something', () => {
    // ...
  })

  it('opens a modal', () => {
    // ...
  })
})

【讨论】:

  • 尝试了上述步骤。仍然是同样的错误。等待 60000 毫秒以加载远程页面后超时。您的页面未在 60000 毫秒内触发其加载事件。
  • 您是否将session_idtoken 替换为实际值?有关如何为您的应用程序获取 cookie 的更多信息,您可以参考这篇文章 - thereluctanttester.com/2018/02/24/…
  • 我正在尝试将令牌传递给它。但是令牌会在一段时间后动态变化。会处理的,让你知道。感谢您的建议。
【解决方案2】:

我已经看到这种行为,当第一个 it case 开始并且没有完成站点中的一些 xhr 请求时 - 结果赛普拉斯在第二个案例开始期间继续该过程。对于我发现的每个案例,最好的解决方案是将每个案例分开放在不同的文件中

【讨论】:

    【解决方案3】:

    更新:

    可以通过存储 session_id 来解决这个问题。

    Cypress.Cookies.defaults({
      preserve: "session_id"
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2020-05-30
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多