Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will we call a function written in a separate file from a Cypress test?

In Cypress.io test, I am calling a subtract function and tests written in the 'example-spec' as below. This is working fine. But how will we call the same subtract function written in a different js file, for example '/basetest.js' from the Cypress test?

describe ('Calling a function', function(){
it('Call the Subtract function and asert the calculation', function(){
    cy
    .wrap({sub: subValues})
    .invoke('sub', 15, 8)
    .should('eq', 7) // true        
    })

})

// Subtract function:

const subValues = (a, b) => {
  return a - b 
}
like image 238
soccerway Avatar asked Aug 13 '18 11:08

soccerway


People also ask

How do you call a JavaScript function in Cypress?

//Method 2: Cypress way of executing javascript click using window object cy. window(). then((win) => { win. eval('document.

How do I run a specific file in Cypress?

cypress run --spec <spec> Run tests specifying a single test file to run instead of all tests. The spec path should be an absolute path or can relative to the current working directory.

How do I run a specific test case in Cypress?

Cypress provides two ways to test cases. Either using the Cypress UI Test Runner or from the CLI using the "cypress run" command. A specific test case can be executed on the CLI using the "--spec" option. We can change the browser for a specific test run on CLI using the "--browser" option.

How do you fetch text in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.


2 Answers

From https://docs.cypress.io/api/cypress-api/custom-commands.html

Put this in your support/commands.js file:

Cypress.Commands.add('subValues', (a, b) => { return a - b });

Put this in your support/index.js file, if it isn't already there (it should be):

import "./commands";

Call it in your test like so:

describe ('Calling a function', function(){
  it('Call the Subtract function and asert the calculation', function(){
    cy
      .subValues(15, 8)
      .should('eq', 7) // true        
    });
});
like image 83
Brendan Avatar answered Sep 19 '22 14:09

Brendan


Add a new file to cypress/support/, e.g. cypress/support/functions.js

cy.myproject = {
    makeUniqueUsername: () => {
        return 'cypress-test-' + Cypress.moment().format("YYMMDD-HHmmss");
    }
}

Include it by adding a reference to it in cypress/support/index.js

import './functions'

Then call your function from any test

describe('registration', function() {
    it('can register', function() {
        let username = cy.myproject.makeUniqueUsername();
        cy.visit('/register');
        cy.get('#username').type(username);
        // etc...
    })
});

When considering plain functions versus cypress "custom commands" note that the cypress documentation encourage plain functions where custom commands aren't necessary in single spec files, though I don't agree with them that you shouldn't DRY test code.


Thanks to the other answers for getting me to this. The main addition is making it step by step, and keeping custom functions out of the global cy.* namespace.


Note: By doing it this way as opposed to registering a command via Cypress.Commands.add you lose logging and time-travel in the Cypress UI. (Thanks @conny). It is up to you and your team which you value more for each case, the explicit steps or the IDE/linter/etc support from using plain js. Registering a command makes it an explicit step in the test rather than just a supporting function.

like image 23
Tim Abell Avatar answered Sep 17 '22 14:09

Tim Abell