Multiple test in same file with same browser

In this example, I want to test visiting the login page, logging in and logging out and in the results, I want to see

Authentication
   ✓ Visit login page (1s)
   ✓ Login (2s)
   ✓ Logout (2s)

From what I can see in the docs, I have to run them as three separate tests, but that also means having to run all the code for logging in a second time in order to test logging out because each test creates a new browser object.

Is there a way to use the same browser object to run all three tests?

This is what I currently have, you can see there is a lot of duplicate code.

'use strict'

const { test, trait } = use('Test/Suite')('Authentication')

trait('Test/Browser')

test('Visit login page', async ({ browser }) => {
  const page = await browser.visit('/')
  await page.assertHas('Login')
  await page.click('a[href="/user/login"]').waitForNavigation()
  await page.assertHas('Email')
  await page.assertHas('Password')
})

test('Login', async ({ browser }) => {
  const page = await browser.visit('/')
  await page.assertHas('Login')
  await page.click('a[href="/user/login"]').waitForNavigation()
  await page.assertHas('Email')
  await page.assertHas('Password')
  await page.type('[name="email"]', 'tester@example.com')
  await page.type('[name="password"]', 'password')
  await page.submitForm('form')
  await page.waitForElement('div.bg-green')
  await page.assertHasIn('div.bg-green', 'SUCCESS')
})

test('Logout', async ({ browser }) => {
  const page = await browser.visit('/')
  await page.assertHas('Login')
  await page.click('a[href="/user/login"]').waitForNavigation()
  await page.assertHas('Email')
  await page.assertHas('Password')
  await page.type('[name="email"]', 'tester@example.com')
  await page.type('[name="password"]', 'password')
  await page.submitForm('form')
  await page.waitForElement('div.bg-green')
  await page.assertHasIn('div.bg-green', 'SUCCESS')
  await page.click('a[href="/user/logout"]').waitForNavigation()
  await page.assertHas('Email')
  await page.assertHas('Password')
})

1 Like

What’s the use case for using the same browser instance?

@virk If I want to test the user carrying out 10 successive operations in the same way they would in real life but want a report on the success of each operation.

Without the same browser instance I will have to sign the user in for each test.

That is a lot of duplicate code, but it’s also not testing the site in the way the user would carry out the operations in real life. No one would do operation 1 then sign out, sign in again and then do operation 2 …

I know that in theory the correct way of testing is to test each operation separate but that has the chance to miss some bugs that the user might experience when actually using the application.

Also if I have to sign in the user for every test and I want to update something relating to signing in, I have to update every test.

Hope that makes sense.