Logo

dev-resources.site

for different kinds of informations.

🩰 Schedule automated tests; become premier ballet artiste

Published at
9/1/2023
Categories
playwright
yaml
tests
Author
ruthmoog
Categories
3 categories in total
playwright
open
yaml
open
tests
open
Author
8 person written this
ruthmoog
open
🩰 Schedule automated tests; become premier ballet artiste

I've been checking this page of the Royal Opera House website everyday lately:
Events page on the Royal Ballet's website

The website shows three sold-out workshops with The Royal Ballet.

There's three sold out events and I want to go to any one of them, so I am keeping an eye out for return tickets going on resale. But wait! Computers can help automate this...

1. Write a test to check the page automatically

I am not sure how the page will look when a ticket goes back on sale, but I am confident that the page will change so that the header with the words "Sold out" do not appear 3 times. (I expect this will also decrease by 1 after any of the dates have passed - but I haven't written this into my test yet).

I created an empty Vue.js project - I don't need a UI, I just want to use tests, so Vue will create a dummy project for me.

Playwright

I am going to use a browser based testing tool called Playwright (But you could use Capybara, or Selenium WebDriver etc.).

First I'm going to write a 'Hello World' style test to make sure I am on the correct page. When I navigate to the url, do I see the title I expect?

test('a hello world check that we have the right page', async ({ page }) => {
  await page.goto('https://www.roh.org.uk/tickets-and-events/dance-with-the-royal-ballet-dates');
  await expect(page).toHaveTitle(/Dance with The Royal Ballet/);
});
Enter fullscreen mode Exit fullscreen mode

Then, I'll write my useful test. I want the headless browser to go to the webpage at the given URL. I'm using JavaScript, so I am going to make my test method async and use await for actions that will create a Promise which needs to be fulfilled.

I used the Chrome DevTools to investigate what elements are used for the "Sold out" text. The Inspect tool tells me this is a h4 and therefore it has the heading Role.

Use the inspect tool to find out a page elements role

The element has a 'heading' Role.

I use Playwright's getByRole method with arguments describing that the role I want is a heading, with the exact text "Sold out".
Then I can add an assertion that I expect there to be three sold out items on the page - I made sure to break my assertion to check that the test can fail, before using the correct assertion.

test('check for sold out items', async ({ page }) => {
  await page.goto('https://www.roh.org.uk/tickets-and-events/dance-with-the-royal-ballet-dates');
  const soldOutItems = await page.getByRole('heading', { name: 'Sold out' });

  // Expects page to have three Sold out items.
  // If there are not 3 sold out items, there may be returns for sale!
  await expect(soldOutItems).toHaveCount(3);
});
Enter fullscreen mode Exit fullscreen mode

The test file on GitHub

2. Trigger the test workflows on automated schedule

I've pushed my changes to my GitHub repo, and use Actions to set up an automatic test run.

The playwright.yml file is automatically configured to run when code is pushed or a pull-request is merged, and it will run the browser tests:

name: Playwright Tests
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
    ...
    - name: Run Playwright tests
      run: npx playwright test
Enter fullscreen mode Exit fullscreen mode

To run these tests on a schedule, I need to add another item within the on section:

  schedule:
    - cron:  '1 1,10,15 * * *'
Enter fullscreen mode Exit fullscreen mode

This uses the POSIX cron syntax to add a schedule event. The example above will schedule the tests to run at 1 minute past 2am, 11am and 4pm local time (or, 01:01, 10:01, 15:01 UTC).

The GitHub Actions docs explain how to use schedule events to trigger workflows, including how the cron syntax works.

For the time being, I have guessed which intervals to use - but I have contacted the box office about when tickets are released on the website, so that I can run the tests as few times as possible.

Workflow configuration YAML on GitHub

And with that, my alerts (ballerts...) are done. GitHub will run my tests at the given times (deferring for heavy usage times) and their email notifications will let me know if there is a test failure and therefore that I should jump over to the website to try and buy a ticket (or update my test!).

*curtsy*

。・:*:・゚☆Graceful curtsy☆・゚:*:・。
tests Article's
30 articles in total
Favicon
Sufficient Software Tests Using Metrics
Favicon
Exploring the Benefits of Integration Testing
Favicon
Best Practices for Effective Automated Integration Tests
Favicon
Automated Tests instrumentation via OpenTelemetry and Aspire Dashboard
Favicon
Integrated tests... are they really important?
Favicon
Focusing on high code coverage can be a trap
Favicon
5 Mistakes to Avoid While Writing Integration Tests
Favicon
Reaching an improved realistic testing approach in the Laravel feature test
Favicon
Desafios Comuns na Escrita de Testes Automatizados: Rumo à Clareza e Padronização - Parte 1
Favicon
Artigo Software Testing: A Research Travelogue - Resumo em PT-BR
Favicon
QA - Définitions et théorie
Favicon
QA - Comment rédiger un test utile ?
Favicon
Applying integration test on NestJS with Jest and GitHub Actions
Favicon
DRY up RSpec subject defining
Favicon
On testing
Favicon
Um vídeo sobre gems e recursos interessantes que podemos integrar com nossas aplicações rails.
Favicon
🩰 Schedule automated tests; become premier ballet artiste
Favicon
Testing Timer-based Logic in Elixir with Klotho Library
Favicon
[Go] How to work with dates in tests
Favicon
Usando o chat do Bing como um aliado para escrever testes de software
Favicon
When and How to Write End-to-End Tests: A Beginner's Guide to Automated E2E Testing
Favicon
Easy Integration Tests for Event-Driven AWS Architectures with EventScout 📨🔭
Favicon
Fix Symfony tests with PHPUnit 10
Favicon
Is programming in TypeScript simply another excuse not to write test in JavaScript?
Favicon
Mocking Interface with jest-mock-extended
Favicon
Do we must implements unit test just to have coverage?
Favicon
Testing a FastAPI application using Ormar models and Alembic migrations
Favicon
How do you deal with test record leaks?
Favicon
Improve your tests with Assert Object Pattern
Favicon
Laravel how to set app environment during tests

Featured ones: