![](/img/trans.png)
[英]How to correctly export and import functions for cypress plugin index.ts?
[英]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.