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

Scroll into view before moving pointer to make Firefox behave the same as Chrome #10

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ jobs:
matrix:
php: [ '8.1', '8.2', '8.3' ]
# 4.1.2-20220227 - last known working version of 4 where the modifications to this package weren't needed
selenium: ['4.1.2', 'latest']
selenium:
- image: 'selenium/standalone-firefox:4.1.2'
browser: firefox
- image: 'selenium/standalone-firefox:latest'
browser: firefox
- image: 'selenium/standalone-chrome:latest'
browser: chrome

fail-fast: false

steps:
Expand Down Expand Up @@ -80,14 +87,16 @@ jobs:

- name: Start Selenium
run: |
docker run --net host --name selenium --volume /dev/shm:/dev/shm --volume $GITHUB_WORKSPACE:$GITHUB_WORKSPACE -e SE_NODE_OVERRIDE_MAX_SESSIONS=true -e SE_NODE_MAX_SESSIONS=5 --shm-size 2g selenium/standalone-firefox:${{ matrix.selenium }} &> ./logs/selenium.log &
docker run --net host --name selenium --volume /dev/shm:/dev/shm --volume $GITHUB_WORKSPACE:$GITHUB_WORKSPACE -e SE_NODE_OVERRIDE_MAX_SESSIONS=true -e SE_NODE_MAX_SESSIONS=5 --shm-size 2g ${{ matrix.selenium.image }} &> ./logs/selenium.log &

- name: Wait for browser & PHP to start
run: |
while ! nc -z localhost 4444 </dev/null; do echo Waiting for remote driver to start...; sleep 1; done
while ! nc -z localhost 8002 </dev/null; do echo Waiting for PHP server to start...; sleep 1; done

- name: Run tests
env:
WEB_FIXTURES_BROWSER: ${{matrix.selenium.browser}}
run: |
./vendor/bin/phpunit -v --coverage-clover=coverage.xml

Expand Down
14 changes: 14 additions & 0 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,9 +950,23 @@ public function mouseOver(string $xpath)
$this->doMouseOver($this->findElement($xpath));
}

private function scrollElementIntoView(Element $element): void {
$script = <<<JS
var e = arguments[0];
e.scrollIntoView({ behavior: 'instant', block: 'end', inline: 'nearest' });
var rect = e.getBoundingClientRect();
return {'x': rect.left, 'y': rect.top};
JS;

$this->executeJsOnElement($element, $script);
}

private function doMouseOver(Element $element): void
{
if ($this->isW3C()) {
// Firefox needs the element in view in order to move the pointer to
// it.
$this->scrollElementIntoView($element);
$actions = array(
'actions' => [
[
Expand Down
8 changes: 8 additions & 0 deletions tests/Custom/DesiredCapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

class DesiredCapabilitiesTest extends TestCase
{
protected function setUp(): void
{
$browser = getenv('WEB_FIXTURES_BROWSER') ?: 'firefox';
if ($browser !== 'firefox') {
$this->markTestSkipped('This test only works with Firefox');
}
}

public function testGetDesiredCapabilities()
{
$caps = array(
Expand Down
26 changes: 26 additions & 0 deletions tests/Custom/LargePageClickTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Behat\Mink\Tests\Driver\Custom;

use Behat\Mink\Tests\Driver\TestCase;

class LargePageClickTest extends TestCase
{
public function testLargePageClick(): void
{
$this->getSession()->visit($this->pathTo('/multi_input_form.html'));

// Add a large amount of br tags so that the button is not in view.
$large_page = str_repeat('<br />', 2000);
$script = <<<JS
const p = document.createElement("div");
p.innerHTML = "$large_page";
document.body.insertBefore(p, document.body.firstChild);
JS;
$this->getSession()->executeScript($script);

$page = $this->getSession()->getPage();
$page->pressButton('Register');
$this->assertStringContainsString('no file', $page->getContent());
}
}
Loading