Buying a bicycle using Playwright


2023 - 03 - 20
Buying a bicycle using Playwright

There is a time in every cyclist's life when they decide to change their bike. This year I felt it was my turn to do so. That's why I used e2e testing, wrote Telegram bots, and set up GH Actions.

This year I decided I wanted a new bicycle. I still love my Giant Contend but decided to switch to Canyon Grizl. Sadly, buying it wasn't that easy. On the other hand, I was just after JS World and wanted to try some stuff out. Good news - I did it thanks to e2e testing, GitHub Action, and a Telegram bot.

The problem

The problem was simple - availability. There is only a limited time frame to preorder those bikes. First, I just signed up for their official newsletter - it should ping me when the bicycle is in stock. Luckily a friend at the bike store warned me that it may not work and it will better to check it manually every day at 6AM.

I hate waking at 6AM, and I love automating stuff, which leads to...

The solution

e2e testing

Checking availability was relatively easy. I had to go to a specific URL and see if one button had a particular class.

As I mentioned, I just came back from the JS World. Debbie O'Brien showed how amazing Playwright is, so I tried it. In the end, the real test looked like this:

import { test, expect } from '@playwright/test';

test('Is bike available?', async ({ page }) => {
  await page.goto( process.env.URL );

  const bike = await page.locator('[data-product-size="'+ process.env.SIZE +'"]');
  await expect(bike).toHaveClass(/js-nonSelectableVariation/);
});

The URL and SIZE were stored in the .env file. Simple, right?

Executing this every hour

I usually would use Buddy for this, but I tried GitHub Actions.

name: Playwright Tests
on: 
  schedule:
    - cron: "*/60 * * * *"
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Install dependencies
        run: npm ci
      - name: Install Playwright Browsers
        run: npx playwright install chromium
      - name: Run Playwright tests
        run: npx playwright test
      - name: send telegram message on push
        uses: appleboy/telegram-action@master
        if: failure()
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          message: |
           BUY YOUR BIKE NOW

The moment when the class was gone, I would get a notification on Telegram. And that is what happened on the 27th of February at 15:06 😄 5 minutes later, the bike was mine.

What's interesting - I never got the official notification, and all the bikes were gone later that day.

It's so beautiful

The moral

Logical thinking and problem-solving can be crucial in many situations. I used a more developer approach but could do this without code tools. There is a bigger chance that I would have to pay something for them.

Subscribe to my newsletter and stay updated.
Get an weekly email with news from around the web
Get updated about new blog posts
No spam

Share your thoughts


All Articles
Share