Flask application consisting of login and form works well manually. However in Cypress test session is not available and test fails. Any idea how to solve or debug this behavior?
I can see create project form displayed.
I can see Cypress test click on the "Create" button.
Then manual test displays expected message that project was created.
However automatic cypress test behaves as if session doesn't exist
app.py
@app.route('/')
def homepage():
user = session.get('user')
return render_template('home.html', user=user)
@app.route('/login-test/<string:email_address>/')
def logintest(email_address):
if (app.debug):
userid = 1
user = { "email" : email_address, "uid" : userid }
session['user'] = user
else:
flash("debug is off, can't use testlogin")
return redirect('/')
@app.route('/create-project/', methods=['POST'])
def createproject():
user = session.get('user')
if (user):
flash("Project added successfully")
return redirect('/')
else:
flash("Session not found")
return redirect('/')
templates/home.html
{% if user %}
<form action="create-project" method="POST">
<input type="text" name="name" placeholder="Enter new project name">
<input type="submit" value="Create">
</form>
<a href='/logout'>destroy session</a>
{% else %}
<a href='/login'>login</a>
{% endif %}
cypress test:
describe('Basic tests', () => {
it('We will use test login to login user1', () => {
cy.visit('/login-test/[email protected]/')
cy.contains('destroy session')
}),
it('We can create project', () => {
cy.get('input[placeholder="Enter new project name"]').type('ProjectNo1')
cy.get('input[value="Create"]').click().contains('Project added successfully')
})
})
CodePudding user response:
Cypress cleans session between tests and in order to preserve session cookie I need to add
Cypress.Cookies.preserveOnce('session')
after
cy.contains('destroy session')
See: https://docs.cypress.io/api/cypress-api/cookies#Preserve-Once
Ravikiran on discord cypress channel helped me with this.