简体   繁体   English

等待页面加载 - 赛普拉斯

[英]Waiting for a page to load - Cypress

I'm having an issue as the page is not fully loaded.我遇到了一个问题,因为页面没有完全加载。 I tried the cy.wait(1000) method, which I do believe is not a good solution, but still not working, the page is not fully loaded.我尝试了cy.wait(1000)方法,我认为这不是一个好的解决方案,但仍然无法正常工作,页面未完全加载。

Here is the website that I'm trying to test https://www.24mx.ie/ .这是我正在尝试测试https://www.24mx.ie/的网站。 The code is in the file homePage.js .代码在文件homePage.js中。

class HomePage {
  static loadHomePage() {
    cy.visit(Cypress.env('url') + '.ie/');
    cy.wait(1000)      
  }

  static acceptCookies() {
    cy.get('div.m-button.m-button--navigation.m-button--xs.qa-consent-agree-btn.ng-tns-c95-8').click();
  }
}

export default HomePage

code in the file homePage.spec.js文件 homePage.spec.js 中的代码

import HomePage from '../pageObjects/homePage'

describe('Home Page Test', function () {
    it('Home Page TC', function () {
        HomePage.loadHomePage();
    })

    it('Accepting Cookies TC', function () {
        HomePage.acceptCookies();
    })

})

Here is a print screen from the test:这是测试的打印屏幕:

在此处输入图像描述

To make sure the page has loaded successfully, you can assert any element to be visible.为确保页面已成功加载,您可以断言任何元素可见。 Since after page load you're clicking the accept cookie button I would suggest to assert the same as visible, something like:由于在页面加载后您单击了接受 cookie 按钮,我建议将其声明为可见,例如:

class HomePage {
  static loadHomePage() {
    cy.visit(Cypress.env('url') + '.ie/');
    cy.get('[class*=qa-consent-agree-btn]', {
      timeout: 5000
    }).should('be.visible') //Make sure Cookie Accept button is visible with timeout of 5 seconds. You can increase timeout as required.     
    }

    static acceptCookies() {
      cy.get('[class*=qa-consent-agree-btn]').click();
    }
  }

  export default HomePage

You should just do a cy.get() and .should() to make Cypress wait for something to appear on the page.您应该只执行cy.get().should()以使 Cypress 等待页面上出现某些内容。

If the page is busy doing API requests, you can do a cy.intercept() on one or more of the API calls, and wait for them.如果页面忙于执行 API 请求,您可以对一个或多个 API 调用执行cy.intercept()并等待它们。 This will buy you some time before you wait for an element to appear.这将在您等待元素出现之前为您争取一些时间。 You can look at the network tab in the Chrome debug tools to see what API requests the page is making.您可以查看 Chrome 调试工具中的网络选项卡,以查看 API 请求页面正在发出的内容。 I would pick one that takes some time to load, and wait for that.我会选择一个需要一些时间来加载的,然后等待。

This is a much better way of "waiting" for the page to load than doing a cy.wait()这是一种“等待”页面加载比执行cy.wait()更好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM