question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to call custom command in test spec file

See original GitHub issue

Unable 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:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pravynandascommented, Jan 9, 2020

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

  1. You are already logged in before()
  2. logout
  3. login with invalid credentails (using your cy.login(‘’,‘’) command)
  4. login with invalid password only
  5. login successfully & logout
  6. reset password
  7. login one last time
  8. logout (<— important)

02.nagivation.spec.js

  1. You are already logged in before()
  2. navigate to page 1
  3. navigate to page 2
  4. logout (<-- important)

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 😃 )

0reactions
jennifer-shehanecommented, Jan 13, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found