Skip to content

Conversation

@Ashish050488
Copy link

Implements the isInViewport method and adds a corresponding test case. Resolves #4423

What?

This PR adds the ElementHandle.isInViewport() method. This new method allows users to check if an element is currently visible within the browser's viewport by executing a script on the element.

A new test, TestFrameElementIsInViewport, has also been added to frame_test.go to validate this functionality.

Why?

This feature was requested in issue #4423 to enable more robust testing for in-page navigations, specifically to confirm that an element has been scrolled into view. This adds functionality similar to what is available in other browser automation libraries.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • [] I have run linter and tests locally (make check) and all pass.
    • Note: I was unable to run the full test suite on my Windows machine due to an existing issue (TestTmpDirCleanup failure). However, I successfully ran my new test (TestFrameElementIsInViewport) in isolation before discovering the test suite issue on Windows.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

Implements the isInViewport method and adds a corresponding test case. Resolves grafana#4423
@Ashish050488 Ashish050488 requested a review from a team as a code owner October 13, 2025 11:45
@Ashish050488 Ashish050488 requested review from codebien and oleiade and removed request for a team October 13, 2025 11:45
@CLAassistant
Copy link

CLAassistant commented Oct 13, 2025

CLA assistant check
All committers have signed the CLA.

@inancgumus inancgumus self-requested a review October 16, 2025 13:33
@Ashish050488 Ashish050488 temporarily deployed to azure-trusted-signing October 16, 2025 16:09 — with GitHub Actions Inactive
@Ashish050488 Ashish050488 temporarily deployed to azure-trusted-signing October 16, 2025 16:11 — with GitHub Actions Inactive
@Ashish050488 Ashish050488 temporarily deployed to azure-trusted-signing October 20, 2025 19:55 — with GitHub Actions Inactive
@Ashish050488 Ashish050488 temporarily deployed to azure-trusted-signing October 20, 2025 19:58 — with GitHub Actions Inactive
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, @Ashish050488 ❤️

We're trying to make it easy for users and match the Playwright API. This PR adds IsInViewPort to ElementHandle, but Playwright doesn't have a corresponding method. Fortunately, the Locator assertions do. Please move this method to the Locator type. Sorry for the inconvenience, as the issue was very old and not updated.

You should also fix the linter errors and tests. Thanks!


I was unable to run the full test suite on my Windows machine due to an existing issue (TestTmpDirCleanup failure). However, I successfully ran my new test (TestFrameElementIsInViewport) in isolation before discovering the test suite issue on Windows.

You can use the -skip flag for go test to ignore some of the flaky tests while running the whole test suite.

Comment on lines +397 to +399



Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove these extra lines.

require.NoError(t, err)
require.NotNil(t, bottomElement)

// 1. First, check that the element is NOT in the viewport
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// 1. First, check that the element is NOT in the viewport
// 1. First, check that the elements are NOT in the viewport

require.NoError(t, err)
require.False(t, inViewport, "the bottom element should not be in the viewport initially")

// Get the link at the top
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this comment, as the code is clear.

Comment on lines 435 to 436
// It can take a moment for the scroll to complete, so we wait briefly.
time.Sleep(500 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be flaky. Try using the isVisible method.

@Ashish050488 Ashish050488 force-pushed the feat/element-is-in-viewport branch from d35e805 to 4eaf78d Compare October 21, 2025 06:38
@Ashish050488 Ashish050488 force-pushed the feat/element-is-in-viewport branch from 4eaf78d to b52c4f3 Compare October 21, 2025 06:42
Moves isInViewport method to Locator, delegates implementation to Frame, updates test case per review grafana#5243.
@Ashish050488 Ashish050488 force-pushed the feat/element-is-in-viewport branch from b52c4f3 to 4c8e513 Compare October 21, 2025 06:47
Comment on lines 8 to 12
<<<<<<< HEAD
=======
"strings"
"github.com/grafana/sobek"
>>>>>>> 8ea4cb8ad (refactor(browser): address PR feedback for isInViewport)
Copy link
Contributor

Choose a reason for hiding this comment

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

Merge/Rebase artifact to cleanup 🙇🏻

@codebien codebien removed their request for review October 23, 2025 12:08
@inancgumus
Copy link
Contributor

@Ashish050488 We'll release k6 1.4.0 in two weeks. Please continue working on this PR if you want it merged into k6 1.4.0. Otherwise, we'll have to postpone merging your PR until k6 1.5.0.

@Ashish050488 Ashish050488 force-pushed the feat/element-is-in-viewport branch from c3f583b to 88dcd4c Compare October 31, 2025 05:41
@Ashish050488 Ashish050488 deleted the feat/element-is-in-viewport branch October 31, 2025 05:44
@Ashish050488 Ashish050488 restored the feat/element-is-in-viewport branch October 31, 2025 05:44
@Ashish050488 Ashish050488 reopened this Oct 31, 2025
@Ashish050488
Copy link
Author

sorry for the delay i was stuck with something now i am active i did the changes asked for do provide feedback if changes required

@Ashish050488
Copy link
Author

Ashish050488 commented Nov 3, 2025

i am trying but i dont know why those test fail can you give me help a little like where am i going wrong i would really appreaciate if you teach me

@inancgumus
Copy link
Contributor

Hi @Ashish050488, you can run goimports to fix the "imported but not used" issue: Error: internal/js/modules/k6/browser/common/locator.go:9:2: "github.com/grafana/sobek" imported and not used.

@mstoykov mstoykov added this to the v1.5.0 milestone Nov 17, 2025
@inancgumus inancgumus self-assigned this Nov 20, 2025
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.

Add ElementHandle.isInViewport()

5 participants