Can we call one test case from the another test case in Cypress?

220 views Asked by At

I am doing automation testing using Cypress with JavaScript.

I have to manage a test case for add, search and edit user. I have created a different test case for each. After adding user, I verified recent created user by searching it. I also search same user before to edit it.

In Cypress, test cases run in a sequence. My first case for 'add user', second for 'Search user' and last one for 'Edit user'. After adding it automatically perform search actions. But before edit, I have to call all functions which are used in a 'Search' test case. I am using the POM framework.

Here is the code I have written:

describe('User management', function(){
    it('Add user', function(){
        obj.clickOnAddUserButton()
        obj.fillDetails()
        obj.clickOnSaveButton()
    })
    it('Search user', function(){
        obj.searchName()
        obj.searchRole()
        obj.verifyName()
        obj.verifyRole()
    })
    it('Edit user', function(){
        obj.searchName()
        obj.searchRole()
        obj.verifyName()
        obj.verifyRole()
        obj.editUser()
    })
})

Is it possible to call it() from the other it(), like a function we call?

I tried the following solution to understand but I was fail: Cypress - have 1 test call another test and run it

I also go through the following link which doesn't help me: How to call the common test case in other test case in protractor

Let give me an example what I want exactly.

describe('User management', function(){
    it('Add user', function(){
        obj.clickOnAddUserButton()
        obj.fillDetails()
        obj.clickOnSaveButton()
    })
    it('Search user', function(){
        obj.searchName()
        obj.searchRole()
        obj.verifyName()
        obj.verifyRole()
    })
    it('Edit user', function(){
        //Here I want to call it() instead of calling same functions here
        //i.e. it('Search user')
        obj.editUser()
    })
})
1

There are 1 answers

0
Lola Ichingbola On

You can just rearrange the code you already have a little bit.

A test pattern in general is it('test-name', callbackFunction).

You can't nest it() because the Cypress runner needs to manage those functions for things like retries and reporting results.

But you can move callbackFunction to make it reuseable.

describe('User management', function() {
  it('Add user', function() {
    obj.clickOnAddUserButton()
    obj.fillDetails()
    obj.clickOnSaveButton()
  })

  function searchUser() {
    obj.searchName()
    obj.searchRole()
    obj.verifyName()
    obj.verifyRole()
  }

  it('Search user', searchUser)   // NOTE no calling brackets on searchUser

  it('Edit user', function() {
    searchUser()                  // NOTE using calling brackets here
    obj.editUser()
  })
})

Composing inside the Page Object

It's going to be tidier to compose that searchUser() function inside the page object itself

class User {

  searchName() {...}
  searchRole() {...}
  verifyName() {...}
  verifyRole() {...}

  searchUser() {
    this.searchName()
    this.searchRole()
    this.verifyName()
    this.verifyRole()
  }
}
describe('User management', function(){
  it('Add user', function(){
    obj.clickOnAddUserButton()
    obj.fillDetails()
    obj.clickOnSaveButton()
  })

  it('Search user', () => {
    obj.searchUser()
  })

  it('Edit user', function(){
    obj.searchUser()
    obj.editUser()
  })
})

Separating steps from workflows

Or one more abstraction, compose a UserWorkflow page object

class User {
  searchName() {...}
  searchRole() {...}
  verifyName() {...}
  verifyRole() {...}
}

class UserWorkflow {

  // Various User workflows to use in tests

  constructor() {
    this.user = new User()
  }

  searchUser() {
    this.user.searchName()
    this.user.searchRole()
    this.user.verifyName()
    this.user.verifyRole()
  }
}