Tuesday, June 4, 2024
63
rated 0 times [  64] [ 1]  / answers: 1 / hits: 29245  / 7 Years ago, thu, december 7, 2017, 12:00:00

I've created the following custom command in my cypress/support/commands.js file.


Cypress.Commands.add("login", (username, password) => {
cy.request({
method: 'POST',
form: true,
url: '/test/login/',
body: {'username': username, 'password': password}
})
})

I had tests passing and login working before moving the login functionality to this custom command. I'm invoking it in my spec with cy.login(testuser, testpwd), but I'm getting the following error message: TypeError: cy.login is not a function. The docs say that /cypress/support/commands.js is loaded before any test files are evaluated, so I assumed that simply placing a custom command in there would make the command available. I'm running the tests through the local (GUI) test runner.


More From » automated-tests

 Answers
1

All the code and referenced modules in index.js are loaded before your test file. So you need to refer(require) commands.js in your index.js file.
You can however import commands.js module directly in your test file but then you need to include it every test file.
Recommended approach is to include it in index.js file and you are not worried about explicitly refer in your test files.


[#55732] Tuesday, December 5, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alexander

Total Points: 693
Total Questions: 114
Total Answers: 95

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;