我在cypressintegrationtodo-actions.spec.js中有以下测试类。
/// <reference types="cypress" />
import { TodoPage } from "../page-object/TodoPage"
describe('todo actions test', () => {
const todoPage = new TodoPage
const todoText = 'hello'
beforeEach(() => {
todoPage.navigateDelay()
todoPage.addToDo(todoText)
})
it('add to do', () => {
todoPage.verifyTodoText(todoText)
todoPage.verifyTodoIsNotSelected(1)
})
})
它引用的是'todoPage.verifyTodoIsNotSelected'这个方法在cypresspage-object.js类中。该类有一个断言,它是引用同一类中的另一个方法。
export class TodoPage{
navigate() {
cy.visit('http://todomvc-app-for-testing.surge.sh/')
}
navigateDelay() {
cy.visit('http://todomvc-app-for-testing.surge.sh/?delay-new-todo=4000')
}
addToDo(todoText){
cy.get('.new-todo', {timeout:6000}).type(todoText + '{enter}')
}
getTodo(index){
cy.get('li:nth-child('+index+') > .view > .toggle')
}
selectToDo(index){
this.getTodo(index).click()
}
clearToDo(){
cy.contains('Clear completed').click()
}
filterAll(){
cy.get(':nth-child(1) > a').click()
}
filterActive(){
cy.get(':nth-child(2) > a').click()
}
filterCompleted(){
cy.get(':nth-child(3) > a').click()
}
//assertions start
verifyTodoIsNotSelected(index){
this.getTodo(index).should('not.be.checked')
}
verfyTodoIsSelected(index){
this.getTodo(index).should('be.checked')
}
verifyTodoText(todoText){
cy.get('label').should('have.text',todoText)
}
verifyTodoMarkCompleted(){
cy.get('label').should('have.css','text-decoration-line','line-through')
}
verifyClearTodo(){
cy.get('.todo-list').should('not.have.descendants','li')
}
verifyTodoCount(count){
cy.get('.todo-list li').should('have.length', count)
}
}
但当我运行测试时,它以下列错误失败。
TypeError: Cannot read property 'should' of undefined
我尝试了下面的测试,结果通过了,但是我想知道为什么上面的测试失败了?
verifyTodoIsNotSelected(index){
cy.get('li:nth-child('+index+') > .view > .toggle').should('not.be.checked')
}
你必须在这里返回cy.get方法。
getTodo(index){
return cy.get('li:nth-child('+index+') > .view > .toggle')
}