Skip to content

Commit

Permalink
test: additional e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roope-ankka committed May 16, 2024
1 parent 4ea1c61 commit ae1099c
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 77 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ bower_components

# tests
report/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
53 changes: 47 additions & 6 deletions e2e/tests/pages/frontpage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ test.describe('Frontpage', () => {
await page.close();
});

test('Title', async () => {
const pageTitle = await page.title();
expect(pageTitle).toContain('Kerrokantasi');
test('page title', async () => {
await expect(page).toHaveTitle(/.*Kerrokantasi/);
});

test('Map', async () => {
test('map section', async () => {
await expect(page.locator('.map')).toBeVisible();
await expect.soft(page.getByRole('heading', { name: 'Käynnissä olevat kuulemiset kartalla' })).toBeVisible();

Expand All @@ -26,7 +25,7 @@ test.describe('Frontpage', () => {
await expect.soft(page.getByLabel('Zoom out')).toBeEnabled();
});

test('Footer', async () => {
test('footer section', async () => {
await expect.soft(page.getByRole('link', { name: 'Kerro kantasi -kehitysideat' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Anna palautetta' })).toBeVisible();

Expand All @@ -36,7 +35,7 @@ test.describe('Frontpage', () => {
await expect.soft(page.getByRole('link', { name: 'Takaisin ylös' })).toBeVisible();
});

test('Header', async () => {
test('header section', async () => {
// Action bar
await expect.soft(page.getByRole('button', { name: 'Suomi', exact: true })).toBeEnabled();
await expect.soft(page.getByRole('button', { name: 'Svenska' })).toBeEnabled();
Expand All @@ -48,4 +47,46 @@ test.describe('Frontpage', () => {
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Kartta' })).toBeVisible();
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Tietoa palvelusta' })).toBeVisible();
});

test('swedish translations', async () => {
await page.goto('/?lang=sv');

// Navbar
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Höranden' })).toBeVisible();
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Karta' })).toBeVisible();
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Information om tjänsten' })).toBeVisible();

// Headings
await expect.soft(page.locator('main')).toContainText('Säg din åsikt');
await expect.soft(page.locator('main')).toContainText('Öppna höranden');
await expect.soft(page.locator('main')).toContainText('Pågående höranden på kartan');

// Footer
await expect.soft(page.getByRole('link', { name: 'Ge respons' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Tillgänglighetsutlåtande' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Dataskydd' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Cookie -inställningar' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Tillbaka till toppen' })).toBeVisible();
});

test('english translations', async () => {
await page.goto('/?lang=en');

// Navbar
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Hearings' })).toBeVisible();
await expect.soft(page.getByRole('list').getByRole('link', { name: 'Map' })).toBeVisible();
await expect.soft(page.getByRole('list').getByRole('link', { name: 'About the service' })).toBeVisible();

// Headings
await expect.soft(page.locator('main')).toContainText('Voice your opinion');
await expect.soft(page.locator('main')).toContainText('Open hearings');
await expect.soft(page.locator('main')).toContainText('Open hearings on map');

// Footer
await expect.soft(page.getByRole('link', { name: 'Give feedback' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Accessibility Statement' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Data Protection' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Data Protection' })).toBeVisible();
await expect.soft(page.getByRole('link', { name: 'Back to top' })).toBeVisible();
});
});
121 changes: 121 additions & 0 deletions e2e/tests/pages/hearing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { expect, test } from '@playwright/test';

const API_URL = process.env.API_URL || 'https://kerrokantasi.api.dev.hel.ninja';

const fetchHearing = async () => {
const res = await fetch(`${API_URL}/v1/hearing/`);
const json = await res.json();
const openHearings = json.results.filter((hearing) => !hearing.closed);
const hearingId = openHearings[0].id;
const hearing = await fetch(`${API_URL}/v1/hearing/${hearingId}/`);

return hearing.json();
};

const hearingContainsQuestions = (hearing) => Boolean(hearing.sections.some((s) => s.questions.length));

test.describe('Hearing', () => {
let page;
let hearing;

test.beforeAll(async ({ browser }) => {
hearing = await fetchHearing();
page = await browser.newPage();
await page.goto(hearing.slug);
await page.getByTestId('cookie-consent-approve-required-button').click();
});

test.beforeEach(async () => {
await page.goto(hearing.slug);
});

test('should display Finnish title', async () => {
await expect(page.locator('main')).toContainText(hearing.title.fi);
});

test('should have comment link', async () => {
const link = page.locator('.internal-link:has-text("Kirjoita kommentti")');
await expect(link).toBeVisible();
});

test('should display comment count', async () => {
await expect(page.getByTestId('comment-summary')).toContainText(`Yhteensä ${hearing.n_comments} kommentti`);
});

test('should have Swedish link if available', async () => {
const noSwedishTitle = !hearing.title.sv;
test.skip(noSwedishTitle, 'No Swedish title available');

await expect(page.getByTestId('language-select')).toContainText('Hörandet tillgängligt på svenska');
});

test('should have English link if available', async () => {
const noEnglishTitle = !hearing.title.en;
test.skip(noEnglishTitle, 'No English title available');

await expect(page.getByTestId('language-select')).toContainText('Hearing available in English');
});

test('should display label', async () => {
const firstFinnishLabel = hearing.labels[0].label.fi;

if (firstFinnishLabel) {
await expect(page.locator('[aria-label="Asiasanat"]')).toContainText(firstFinnishLabel);
}
});

test('should display contact person name', async () => {
const contactPerson = hearing.contact_persons[0];
await expect(page.locator('.hearing-contacts')).toContainText(contactPerson.name);
});

test('should have image with caption', async () => {
const img = page.locator('.section-image img');
await expect(img).toHaveAttribute('alt', hearing.main_image.caption.fi);
});

test('should have Excel link for comment summary', async () => {
const link = page.locator('a:has-text("Lataa yhteenveto kommenteista Excel-muodossa")');
await expect(link).toBeVisible();
});

test('should have comment button', async () => {
const button = page.locator('.comment-form-container button');
await expect(button).toBeVisible();
});

test('verify visibility of questions and their options', async () => {
test.skip(!hearingContainsQuestions(hearing), 'No questions found');

await page.getByRole('button', { name: 'Äänestä ja kommentoi' }).last().click();

const mainSection = hearing.sections.find((s) => s.type === 'main');
test.skip(!mainSection, 'No main section found');
const firstQuestion = mainSection.questions[0];
const questionText = firstQuestion.text.fi;
const options = firstQuestion.options.map((o) => o.text.fi);
await expect(page.locator('.comment-form')).toContainText(questionText);
await expect(page.getByTestId('question-form-group').locator('.radio')).toContainText(options);
});

test('user can successfully submit a comment', async () => {
await page.locator('.comment-form-container button').click();
await page.locator('.comment-form textarea').fill('Tämä on selaintestin kirjoittama viesti');
await page.getByPlaceholder('Anonyymi').fill('Testi Testinen');
await page.getByRole('button', { name: 'Lähetä' }).click();
await expect(page.getByText('Kommenttisi on vastaanotettu')).toBeVisible();
});

test('check Finnish title in first subsection if subsections exist', async () => {
const hasSectionCard = hearing.sections.filter((s) => s.type === 'part').length > 0;
test.skip(!hasSectionCard, 'Hearing does not contain subsections');

if (hasSectionCard) {
const button = await page.locator('.section-card button').first();
await button.click();

const partSection = hearing.sections.find((s) => s.type === 'part');
await expect(page.locator('main')).toContainText(partSection.title.fi);
}
});
});
61 changes: 61 additions & 0 deletions e2e/tests/pages/hearings-list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { expect, test } from '@playwright/test';

test.describe('Hearing list page', () => {
let page;

test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/hearings/list?lang=fi');
});

test.afterAll(async () => {
await page.close();
});

test('should have correct title', async () => {
await expect(page).toHaveTitle(/.*kuulemiset/);
});

test('should display all headings', async () => {
await expect(page.getByRole('heading', { name: 'Kaikki kuulemiset' })).toBeVisible();
});

test('should have search controls visibility', async () => {
await expect.soft(page.getByText('Etsi otsikoista')).toBeVisible();
await expect.soft(page.getByText('Hae aiheista')).toBeVisible();
await expect.soft(page.getByRole('button', { name: 'Etsi', exact: true })).toBeEnabled();
});

test('should perform a search', async () => {
const searchTextInput = page.getByRole('combobox', { name: 'Etsi otsikoista' });

// Check the default state of the select input
const input = page.locator('#formControlsSearchSelect');
await expect.soft(input).toHaveValue('');

// Perform a search and validate the input value
const SEARCH_TEXT = 'Hello';
await searchTextInput.fill(SEARCH_TEXT);
await expect(searchTextInput).toHaveValue(SEARCH_TEXT);
});

test('swedish translations', async () => {
await page.goto('/hearings/list?lang=sv');

await expect(page).toHaveTitle(/.*Alla höranden/);
await expect(page.getByRole('heading', { name: 'Alla höranden' })).toBeVisible();
await expect.soft(page.getByRole('button', { name: 'Sök', exact: true })).toBeEnabled();
await expect.soft(page.getByText('Sök bland rubrikerna')).toBeVisible();
await expect.soft(page.getByText('Sök bland ämnena')).toBeVisible();
});

test('english translations', async () => {
await page.goto('/hearings/list?lang=en');

await expect(page).toHaveTitle(/.*All hearings/);
await expect(page.getByRole('heading', { name: 'All hearings' })).toBeVisible();
await expect.soft(page.getByRole('button', { name: 'Search', exact: true })).toBeEnabled();
await expect.soft(page.getByText('Search from titles')).toBeVisible();
await expect.soft(page.getByText('Search labels')).toBeVisible();
});
});
43 changes: 0 additions & 43 deletions e2e/tests/pages/hearings.spec.js

This file was deleted.

53 changes: 41 additions & 12 deletions e2e/tests/pages/info.spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
import { expect, test } from '@playwright/test';

test.describe('Info page', () => {
let page;

test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
test.describe('Info page - Fi', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/info?lang=fi');
});

test.afterAll(async () => {
await page.close();
test('Title', async ({ page }) => {
await expect(page).toHaveTitle(/.*Tietoa palvelusta/);
});

test('Title', async () => {
const pageTitle = await page.title();
expect(pageTitle).toContain('Tietoa palvelusta');
test('Headings', async ({ page }) => {
await expect(page.getByRole('heading', { name: 'Tietoa Kerrokantasi.hel.fi' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Palvelun käyttö' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Tietosuoja' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Evästeet' })).toBeVisible();
});
});

test('Headings', async () => {
await expect(page.getByRole('heading', { name: 'Tietoa Kerrokantasi.hel.fi' })).toBeVisible();
test.describe('Info page - Sv', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/info?lang=sv');
});

test('Title', async ({ page }) => {
await expect(page).toHaveTitle(/.*Information om tjänsten/);
});

test('Headings', async ({ page }) => {
await expect(page.getByRole('heading', { name: 'Information om tjänsten Kerrokantasi' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Använda tjänsten' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Dataskyddsbeskrivning' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Om kakor' })).toBeVisible();
});
});

test.describe('Info page - En', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/info?lang=en');
});

test('Title', async ({ page }) => {
await expect(page).toHaveTitle(/.*About service/);
});

test('Headings', async ({ page }) => {
await expect(page.getByRole('heading', { name: 'Terms of use for the Kerrokantasi service' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Using the service' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Data protection notices' })).toBeVisible();
await expect(page.getByRole('heading', { name: 'About cookies' })).toBeVisible();
});
});
Loading

0 comments on commit ae1099c

Please sign in to comment.