Unable to call custom command in test spec file
See original GitHub issueUnable to call custom command :
i have add a new command in commands.js
and trying to call it inside spec test
but its not defined
Cypress.Commands.add('login',(username,password) => {
cy.visit('https://example.com/')
cy.get('input[name="username"]').clear().type('username');
cy.get('input[name="password"]').type('password');
cy.get('button[type="submit"]', { timeout: 20000 }).last().click();
cy.url().should('eq','https://example.com/home/');
});
then call it in beforeEach
import helper from '../../Libs/helper/helper.js';
import AccountPage from '../../Libs/accountPage.js';
import Menu from '../../Libs/menu';
import LoginPage from '../../Libs/loginPage.js';
const page = new AccountPage();
const menu = new Menu();
const login = new LoginPage();
describe('MC_TC_02 Account Overview test', function () {
beforeEach(function () {
**cy.login();**
cy.fixture('testdata').as('testData');
cy.get('@testData').then((testData) => {
helper.testData = testData;
});
});
it('2.1.Verify account is loaded properly after loging', function () {
page.navigateToAccoun()
});
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
unable to invoke Cypress custom commands in test files
I am working on managing the session cookie for each test case by setting it in beforeEach() and afterEach() block. Using the below...
Read more >Custom Commands - Cypress Documentation
Cypress comes with its own API for creating custom commands and overwriting existing commands. The built in Cypress commands use the very same...
Read more >Custom Commands in Cypress - Tools QA
The best place to define the custom commands is in the cypress/support/commands.js file, as it loads before any of the test-script files.
Read more >How to create custom Commands in Cypress? - YouTube
The place to define or overwrite commands is in your cypress/support/ commands.js file, since it is loaded before any test files are ...
Read more >Component testing scenarios - Angular
That's not a problem when you run the CLI ng test command because it compiles the application ... Here's the testing module configuration...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No worries, that’s how we learn.
Based on your earlier request, “lets say i have 5 specs i want this login method to be run once upon specific spec” it is certain that you wanted to login before starting each spec file, which means, you have to logout at the end of each test to make it work (otherwise your before() test fails on next spec file, because you already logged in and cannot fill login form).
Also, if you have some scenarios to test on the login feature itself, you can achieve that within the spec file (while logging out one last time at the end).
To put it in simple terms.
01.login.spec.js
02.nagivation.spec.js
I think, if logout is obvious in each spec file, you can put it in after() command of index.js (which, ofcourse I haven’t tried myself yet 😃 )
Issues in our GitHub repo are reserved for potential bugs or feature requests. This issue will be closed since it appears to be neither a bug nor a feature request.
We recommend questions relating to how to use Cypress be asked in our community chat. Also try searching our existing GitHub issues, reading through our documentation, or searching Stack Overflow for relevant answers.