Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on page.query when no matches with given selector #1101

Merged
merged 3 commits into from
Nov 16, 2023

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Nov 15, 2023

What?

This fixes the issue where Page.Query (page.$) returned an error when no elements match the given selector.

Why?

The actual expectation in this scenario, is for the API to return nil.

This was tested with the following script:

Click me
import { browser } from 'k6/experimental/browser';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      iterations: 1,
      options: {
        browser: {
          type: 'chromium'
        },
      },
    },
  },
}

export default async function () {
  const page = browser.newPage();

  await page.goto("https://test.k6.io");

  const result = page.$('input[name="login"]');
  if (!result) {
    console.log("It's null, hooray!");
  }

  page.close();
}

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates: #982

common/element_handle.go Outdated Show resolved Hide resolved
ka3de
ka3de previously approved these changes Nov 16, 2023
Copy link
Collaborator

@ka3de ka3de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

browser/mapping.go Show resolved Hide resolved
Copy link
Collaborator

@ka3de ka3de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

When page.query (page.$ in the js test script) is used, if no element
matches the selector then it used to return an error. The expected
behaviour is that it returns nil in this case.
If the result of query is nil, we shouldn't map this to an
elementHandle, since the expectation on a nil result in query is null
in the ks test script. If we map the nil result, the result is no
longer null in the js test script.
@ankur22 ankur22 merged commit e8b0269 into main Nov 16, 2023
17 checks passed
@ankur22 ankur22 deleted the fix/query-nil-result branch November 16, 2023 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants