簡體   English   中英

賽普拉斯在使用夾具和 POM 時返回“未定義”參數

[英]cypress returning "undefined" argument when using fixture and POM

在修復了我的固定裝置的一些問題后,我正在使用 POM 玩 cypress 腳本,但是每當我希望我的函數輸入我在 JSON 文件中的一些數據時,我都會收到以下消息

cy.type() 只能接受字符串或數字。 你傳入:未定義

這是我的代碼,我確保遵循有關如何使用固定裝置的正確說明

 import loginPage from '../POM/pomtest.js' const credentials = require('../../fixtures/loginTestCases.json') /// <reference types= "cypress" /> describe("test suite", function(){ let credentials; before(() => { cy.fixture('loginFixture').then((logindata) =>{ credentials = logindata; return credentials }); }) beforeEach(() =>{ loginPage.visit(); cy.wrap(credentials).as('credentials') }) it('incorrect username', function (){ loginPage.clickLoginBtn() loginPage.typeUsername(this.credentials.usernameIncorrecto); loginPage.typePassword(this.credentials.passwordCorrecto); loginPage.clickLoginBtn(); loginPage.elements.myNotes().should('have.text', this.credentials.loginFallido); }) it('incorrect password', function (){ loginPage.clickLoginBtn() loginPage.typeUsername(this.credentials.usernameCorrecto); loginPage.typePassword(this.credentials.passwordIncorrecto) loginPage.clickLoginBtn() loginPage.elements.myNotes().should('have.text', this.credentials.loginFallido) }) it('Valid Login', function (){ loginPage.clickLoginBtn() loginPage.typeUsername(this.credentials.usernameCorrecto); loginPage.typePassword(this.credentials.passwordCorrecto) loginPage.clickLoginBtn() loginPage.elements.myNotes().should('have.text', this.credentials.loginCorrecto) }) })

這是我的 POM 文件

 class loginPage{ elements = { usernameInput: () => cy.xpath("//input[@placeholder='Username']"), passwordInput: () => cy.xpath("//input[@name='login.password']"), loginBtn: () => cy.get('button').contains('Login'), errorMsg: () => cy.get('#login-error-message'), myNotes: () => cy.get('H2') } typeUsername(username){ this.elements.usernameInput().type(username); } typePassword(password){ this.elements.passwordInput().type(password); } clickLoginBtn(){ this.elements.loginBtn().click(); } visit(){ cy.visit("http://testapp.galenframework.com/") } fillemail(value){ let field = cy.xpath("//input[@placeholder='Username']") field.clear() field.type(value) } fillpassword(value){ let field = cy.xpath("//input[@name='login.password']") field.clear() field.type(value) } } //export default loginPage module.exports = new loginPage();

嘗試鍵入 JSON 文件中的內容時出現錯誤

 [ { "usernameIncorrecto": "incorrecto@gmail.com", "passwordIncorrecto": "incorrecta", "usernameCorrecto": "testuser@example.com", "passwordCorrecto": "test123", "loginFallido": "The username or password are incorrect", "loginCorrecto": "My Notes" } ]

您的問題是您的credentials變量是一個數組,但您試圖直接訪問該變量沒有的屬性。 (此外,您調用的屬性在對象上不存在,即使它們被正確引用。您應該切換到通過索引查找數據,或者在對象上使用名稱屬性。

// accessing data via an index
    it('incorrect username', function (){
        loginPage.clickLoginBtn()
        loginPage.typeUsername(this.credentials[0]. usernameIncorrecto);
        loginPage.typePassword(this.credentials[0]. passwordIncorrecto);
        loginPage.clickLoginBtn();
        loginPage.elements.myNotes().should('have.text', this.credentials[0]. loginFallido);
    })

請注意上面我如何使用this.credentials[0]訪問 JSON 中的第一個對象。 使用此方法,您還可以在it塊的開頭聲明一個變量以簡化此操作。

it('incorrect username', function() {
  const creds = this.credentials[0];
  ...
  loginPage.typeUsername(creds.usernameIncorrecto);
  ...
})

暫無
暫無

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

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