grafana / xk6-browser

k6 module that adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol
https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/
GNU Affero General Public License v3.0
341 stars 42 forks source link

Error when parsing `null` remote object #793

Closed ka3de closed 1 year ago

ka3de commented 1 year ago

Brief summary

There is a mishandling of RemoteObject parsing when this one is of subtype null. This mishandling ends up on our implementation trying to unmarshal a nil value which ultimately results in a panic with error:

panic: GoError: parsing remote object value: unexpected end of JSON input

The concrete flow is the following:

Solution We should add an explicit case inside TypeObject case in order to handle nullsubtype.

See the CDP specs for remote object and possible values for type and subtype.

xk6-browser version

v0.8.1

OS

Ubuntu 20.04.5 LTS

Chrome version

109.0.5414.119 (Official Build) (64-bit)

Docker version and image (if applicable)

No response

Steps to reproduce the problem

The problem can be reproduced with this script:

import { chromium } from 'k6/x/browser';

export default function () {
  const browser = chromium.launch({ headless: false/*, debug: true*/ });
  const page = browser.newPage();

  page
    .goto('https://apply.mykaleidoscope.com/')
    .then(() => {
        console.log("navigated");
    })
    .finally(() => {
      page.close();
      browser.close();
    });
}

Expected behaviour

The xk6-browser execution finishes correctly, navigating to the website.

Actual behaviour

The execution crashes with the following error:

scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

WARN[0001] "Google Maps JavaScript API has been loaded directly without a callback. This is not supported and can lead to race conditions and suboptimal performance. For supported loading patterns please see https://goo.gle/js-api-loading"  source=browser-console-api
WARN[0001] url:https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html method:GET err:fetching response body: No resource with given identifier found (-32000)  category="Response:bodySize:fetchBody" elapsed="0 ms" goroutine=386
WARN[0001] url:https://js.stripe.com/v3/fingerprinted/js/m-outer-8cb24ab2d649fd36a488d04d8c457933.js method:GET err:fetching response body: No resource with given identifier found (-32000)  category="Response:bodySize:fetchBody" elapsed="0 ms" goroutine=386
WARN[0001] url:https://m.stripe.network/inner.html method:GET err:fetching response body: No resource with given identifier found (-32000)  category="Response:bodySize:fetchBody" elapsed="0 ms" goroutine=315
WARN[0001] url:https://m.stripe.network/out-4.5.42.js method:GET err:fetching response body: No resource with given identifier found (-32000)  category="Response:bodySize:fetchBody" elapsed="0 ms" goroutine=315
WARN[0002] url:https://m.stripe.com/6 method:POST err:fetching response body: No resource with given identifier found (-32000)  category="Response:bodySize:fetchBody" elapsed="0 ms" goroutine=315
ERRO[0005] Failed to load resource: the server responded with a status of 429 ()  browser_source=network line_number=0 source=browser stacktrace="<nil>" url="https://o1012071.ingest.sentry.io/api/5977259/envelope/?sentry_key=e1363397e5574a048c87ce93858e83bf&sentry_version=7&sentry_client=sentry.javascript.nextjs%2F7.26.0"
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
WARN[0006] "/!\\ You are using legacy implementaion. Please update your code: use createWrapper() and wrapper.withRedux()."  source=browser-console-api
panic: GoError: parsing remote object value: unexpected end of JSON input
running (00m06.1s), 1/1 VUs, 0 complete and 0 interrupted iterations
goroutine 101 [running]:-------------------------] 1 VUs  00m06.1s/10m0s  0/1 iters, 1 per VU
go.k6.io/k6/js/common.Throw(0x1407000?, {0x194cf40?, 0xc001f101a0?})
    go.k6.io/k6@v0.42.1-0.20230130080633-582ec4d3940c/js/common/util.go:20 +0x4d
github.com/grafana/xk6-browser/k6ext.Panic({0x195c988, 0xc00276f1d0}, {0x167e2a5, 0x1f}, {0xc00061f9d0?, 0x1, 0x1})
    github.com/grafana/xk6-browser@v0.0.0-00010101000000-000000000000/k6ext/panic.go:51 +0x2c6
github.com/grafana/xk6-browser/common.handleParseRemoteObjectErr({0x195c988, 0xc00276f1d0}, {0x194ce40?, 0xc0016a4468}, 0x0?)
    github.com/grafana/xk6-browser@v0.0.0-00010101000000-000000000000/common/remote_object.go:195 +0x125
github.com/grafana/xk6-browser/common.(*FrameSession).onConsoleAPICalled(0xc00132c0b0, 0xc00088e000)
    github.com/grafana/xk6-browser@v0.0.0-00010101000000-000000000000/common/frame_session.go:530 +0x1e5
github.com/grafana/xk6-browser/common.(*FrameSession).initEvents.func1()
    github.com/grafana/xk6-browser@v0.0.0-00010101000000-000000000000/common/frame_session.go:249 +0x565
created by github.com/grafana/xk6-browser/common.(*FrameSession).initEvents
    github.com/grafana/xk6-browser@v0.0.0-00010101000000-000000000000/common/frame_session.go:208 +0x1ab

See also the comparison between the console log messages in the k6 output vs the console.log message shown when navigating to the website through the browser: Screenshot from 2023-02-24 09-21-27

Notice the null messages output in the browser console right at the moment that xk6-browser crashes.

Related: https://github.com/grafana/xk6-browser/issues/777

ArielKo1248 commented 1 year ago

Hi,

First of all, great working narrowing down the cause.

Second, I see there is an orphaned PR for this, any idea if this fix can make it into the next version as it is blocking our large scale performance testing.

Thanks so much!

ankur22 commented 1 year ago

@ArielKo1248,

We're going to add this to our current milestone.

ka3de commented 1 year ago

Hi @ArielKo1248 , the fix for this issue has just been merged into main, but won't be included in k6 until the next release. If you want you can use xk6 to build a custom version which includes k6 browser main version.