簡體   English   中英

Cypress - 導入和導出功能

[英]Cypress - import and export functions

如何更好地組織我的 cypress 代碼進行測試,以便我只需要導入一些函數?

我想首先創建一個在頁面上進行身份驗證的文件,然后我想將它導入具有多個功能的測試中。

我嘗試了以下導出代碼,它似乎不正確,有錯誤:

export function login() {
    cy.visit('https://*********')
    cy.get('input[name="Parameter.UserName"]').type('*****')
    cy.get('input[name="Parameter.Password"]').type('*****')
    cy.contains('Login').click()

}

export default {login};

在測試中:

import {login} from 'elements/pages/login.js'

您的導入需要一個相對 URL

import {login} from '../elements/pages/login.js'  // relative from cypress/integration

或者如果在cypress/suport/elements

import {login} from '../support/elements/pages/login.js'  

絕對導入(其中路徑沒有前導 ./ 或 ../)被假定為node_modules中的庫包。

賽普拉斯為此提供了一些稱為自定義命令的東西,您可以在此處閱讀相關內容。

轉到cypress/support/commands.js並在此處編寫所有代碼,例如:

Cypress.Commands.add('login', () => {
  cy.visit('https://*********')
  cy.get('input[name="Parameter.UserName"]').type('*****')
  cy.get('input[name="Parameter.Password"]').type('*****')
  cy.contains('Login').click()
})

然后在您的任何測試中,您可以直接添加:

cy.login()

在測試內容中必須執行所有 cys,也許你可以這樣嘗試:

export default {

    // props
    visit: (url)=> { return cy.visit(url) }
    get: (el)=> { return cy.get(el) }

    // methods
    login(){
      cy.visit('https://*********')
      cy.get('input[name="Parameter.UserName"]').type('*****')
      cy.get('input[name="Parameter.Password"]').type('*****')
      cy.contains('Login').click()
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM