TypeScript Example

Hi all, here’s an example of a performance framework written in TypeScript:

I’m also learning, so please let me know if you notice anything that can be improved :slight_smile:

(I’d love to know how to transpile it down to a version of JS that can run with ‘base’ compatibility mode also! I’ve had no luck there…)

6 Likes

Good job!

I’ll have a look and might provide a PR in case I find something that could be of use for you. :+1:

2 Likes

Lately, we have fixed some issues on the k6 TypeScript definitions.

Inspired by @sstratton’s project. We have ended creating a GitHub Template to generate a k6 project using TypeScript easily.

We hope it can also be useful for the community.

5 Likes

Hi, @ppcano ! I was following your approach to using it in my project.

I copied the webpack.config.js, tsconfig.json, .babelrc files and installed the devDependencies in my project that uses the POM “pattern” (I just only did a minor change in the GlobEntries changing the path to my performance tests).

When I tried to use the chromium browser (import { chromium } from 'k6/experimental/browser';, I got this error: Module '"k6/experimental/browser"' has no exported member 'chromium'.

Sample.-

import { sleep, check } from "k6";
import { chromium } from 'k6/experimental/browser';

import { SLLoginPage } from '../pages/SLLoginPage';
import { SLProductPage } from '../pages/SLProductsPage';

let slLoginPage: SLLoginPage;
let slProductPage: SLProductPage;

export const options = {
    scenarios: {
        login: {
            executor: 'constant-vus',
            exec: 'login',
            vus: 1
        }
    },
};

export async function login() {
    const browser = chromium.launch({
        headless: true,
        slowMo: '500ms'
    });
    const context = browser.newContext();
    const page = context.newPage();

    try { 
        slLoginPage = new SLLoginPage(page);
        slProductPage = new SLProductPage(page);

        await slLoginPage.goTo();
        await slLoginPage.fillLoginForm(process.env.USERNAME as string, process.env.PASSWORD as string);
        await page.waitForURL('**/dashboard', { timeout: 3000 });
} finally {
    page.close();
    browser.close();
}

Thanks in advance for your help. Have a great day!