How to get the current URL as a string in xk6-browser?

I tried to use page.url(), but apparently it is not working.

See my script below:

import { chromium } from ‘k6/x/browser’;
import { sleep } from ‘k6’;

var URL = ‘http://ecommerce.test.k6.io/’;

export default function () {
const browser = chromium.launch({ headless: false, slowMo: ‘500ms’ });
const context = browser.newContext();
const page = context.newPage();

page
    .goto(URL, {
        waitUntil: 'networkidle',
        waitUntil: 'load',
    }).then(() => {
        page.$('img[src="http://ecommerce.test.k6.io/wordpress/wp-content/uploads/2021/02/album-1.jpg"]').click();
        var currentURL = page.url();
        console.log(currentURL);
        sleep(1000);
        
    }).finally(() =>{
        page.close();
        browser.close();
    });

}

For this I got the error:

ERRO[0008] process with PID 19956 unexpectedly ended: exit status 1 category=browser elapsed=“0 ms” goroutine=15
ERRO[0008] cleaning up the user data directory: remove C:\Users\FD1DF~1.GAR\AppData\Local\Temp\xk6-browser-data-2390913065\Default\Affiliation Database: The process cannot access the file because it is being used by another process. category=“Browser:Close” elapsed=“1 ms” goroutine=60
ERRO[0008] Uncaught (in promise) GoError: evaluating JS: execution context changed; most likely because of a navigation
running at github.com/grafana/xk6-browser/api.Page.URL-fm (native)
default at file:///C:/Users/f.gardin/k6/har_files/xk6-browser/script.js:18:29(10) executor=per-vu-iterations scenario=default

I need to capture the URL after the action as a string, is there another way to do this???

1 Like

Hi @f.gardin welcome to the forum!

Apologies for late reply, I was playing around with your script and turns out there is a bug in the page.url() method. I’ve created an issue for it which you can track here.

Thank you for your help!

1 Like

Hi @f.gardin ,
The mentioned issue for page.url() has been fixed in the main branch for xk6-browser. There is not a specific released version that includes this yet, but you can generate a custom xk6-binary with latest changes in main building it from source.

Regards.

1 Like

Hello @Daniel.J

Thanks for the replies

Best Regards