Background
Hi, I am new to cypress and I have the following code which allows a specified user type (out of 4 total) to login.
As this is one of the main tests, instead of having to copy and paste this same code into each of my new tests, I would like to make them re-usuable so that in future tests, they can be used or "called" again.
It will be easier to keep these tests in a separate folder and separate files, so that they be maintained on their own but have the ability to call them when necessary in any future test files that I created.
For example, if I have a future test relating to a teacher, I can call the function that does the teacher login and then start writing the rest of the script that actually deals with the test without needing to mess about copying and pasting it each time.
The block of code that does the login part is below (and is pretty much the same for all other types of users)
I have been trying to research how to create methods and functions, but am not too sure about the structure I should use, and have looked at the documentation but I am a bit puzzled.
describe('Create Homework', function() {
it('Create New Assignment', function() {
cy.visit('http://www.demoapp.com')
cy.contains('Log in')
// Check that the user has indeed landed on the login page
cy.url().should('include','/login')
// Make a school selection
cy.get('#school-selector-search-box')
.type('Bristol Free School')
// Click the suggestion
.get('.suggestions > .ember-view.suggestion:nth-of-type(1) > .suggested-school-name').click()
// Enter a username at this step
cy.get('#identification')
.type('gdawson_4319c')
.should ('have.value','gdawson_4319c')
// Enter a password at this step
cy.get('#password')
.type('demo')
.should ('have.value','demo')
// Proceed to login to account
cy.get('.actions > .btn').click()
// Assert that Dashboard is visible
cy.get('h1.main-header-title')
cy.contains ('Dashboard')
})
})
Outcome
Be able to wrap the above code into something (like a function) that would make it reusable so that I can use these steps in another test without needing to manually copy and paste it. This means I could move these into a seperate file, and only call them when I need to use these steps which will save me time.