No response from page.goto('myurl.com')

export default async function (data) {

    const browser = chromium.launch();
    const page = browser.newPage();

    const response = await page.goto('myurl.com);

    console.log(page.response);
    
    check(response.ok, {
        "response is 200": (r) => r.ok === 200,
    });

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

    sleep(1);
}

My console.log is Undefined - I expect the whole response from the call to be there.

INFO[0002] undefined source=console
INFO[0002] undefined source=console

Hi @harveysmith,

Thanks for your question.

It looks like you’re trying to access page.response which will be undefined since the page object doesn’t define a response member variable. If you print the response which is returned from page.goto you should see that it prints {}.

check’s first input variable is the value you want to check against. response.ok will return null since that is not defined, although ok() is defined. What I think you want is to pass in response, and in the check’s test body you want to call status() to retrieve the http status code:

  check(response, {
      "response is 200": (r) => r.status() === 200,
  });
}

Let me know if that helps.

Cheers,
Ankur

1 Like

Hi Ankur,

Thanks for the response. Here is my problem.

If I use a http.get(‘mypage’) to load my page, the response back from this is half a webpage because the sire requires Javascript and a browser to load the body.

If I use page.goto(‘mypage’), then I cannot use the response object as it is not defined.

How do I get past my login page and onbto the page under test (in this case a dashboard) using K6?

ps. Status is not available.

Thanks in advance

Hi @harveysmith,

I’m a little confused as to what you are trying to do with the response, can you elaborate a bit more why you need the response when you navigate to the website under test using page.goto?

Which part of the response object is undefined when working with page.goto?

I’ve reworked your example and this works for me:

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

export default async function() {

  const browser = chromium.launch();
  const page = browser.newPage();

  const response = await page.goto('https://test.k6.io');

  console.log(response.ok);
  
  check(response, {
      "response is 200": (r) => r.status() === 200,
  });

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

  sleep(1);
}

Cheers,
Ankur

I should say I am using goto to get to a login page forst, then the app auto redirects to a dashboard.

await page.goto(baseURL, { waitUntil: 'load' });

    page.locator('input[id="username"]').type(user);
    page.locator('input[id="password"]').type(password);
    const submitButton = page.locator('#loginBtn');
    const response = await Promise.all([page.waitForNavigation(), submitButton.click()]);

    check(page, { 'Verify user is logged In': page.locator("//p[starts-with(text(),'Logged in as')]") });
    check(page, { 'Verify user has welcome message': page.locator("//h6[starts-with(text(),'Welcome')]") });

    check(response, {
        "response is 200": (r) => r.status() === 200,
    });

My response object does not have status as a member

Hi @harveysmith,

Thanks for the example code which outlines your exact requirements and problem you are facing. page.waitForNavigation returns the response for the page.click action, and when working with Promise.All when all promises are fulfilled an array with the fulfilled values is returned.

What this means is that the response const variable in your example needs to be iterated over to retrieve the object which contains the response and work with that object.

Hope that helps.

Cheers,
Ankur

Thanks for the answer…

So can I goto the ‘logged in page’ and get that response then?

[quote=“harveysmith, post:7, topic:100623”]

check(response, {
    "response is 200": (r) => r.status() === 200,
});

I tried this and got no resposne…

Did you iterate over the response object? You can’t work with it directly since it’s an array that is returned when working with Promise.All.