Transaction start & end points

Hi There!

I’m just discovering the xk6-browser, I’ve run a few scripts with it and I’m curious if it’s possible to define our own transaction start and end points based on the property of the chosen object (as is the case with LoadRunner TruClient for example).

Here’s an example of what I mean: I’m buying an item and I want to start a transaction immediately before I click the “Submit Order” button. I check the page, and stop the transaction when the text “Your order has been submitted” appears on the screen.

Is it possible to do that?

Hi Bassem, welcome to the forum :slight_smile:

What do you mean by “transaction”? k6 (and xk6-browser) doesn’t have a concept of transactions as seen in other tools.

You could get an approximation using groups and fail(). If you consider a transaction to be a single iteration, you could use fail() to interrupt it.

Something like:

import { group, fail } from 'k6';
import launcher from 'k6/x/browser';

export default function () {
  const browser = launcher.launch('chromium', {
    headless: false,
  });
  const context = browser.newContext();
  const page = context.newPage();

  group('buy an item', function () {
    page.goto('http://ecommerce.k6.io/', { waitUntil: 'networkidle' });

    // Use an XPath selector to match on the element text contents.
    // You might also consider using page.waitForSelector() if it's a dynamic page.
    const el = page.$('//span[@class="onsale" and text()="Sale!"]');
    if (el !== null) {
      // Interrupt this iteration if the element is found.
      fail();
    }

    // Otherwise, continue with other operations...
  });

  page.close();
  browser.close();
}

Hope this helps.