Skip to content

Commit

Permalink
Merge pull request #10 from alexpott/FIX-firefox-click-and-move-pointer
Browse files Browse the repository at this point in the history
Scroll into view before moving pointer to make Firefox behave the same as Chrome
  • Loading branch information
longwave authored Jul 24, 2024
2 parents 9144589 + 5695798 commit 15c0c11
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
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());
}
}

0 comments on commit 15c0c11

Please sign in to comment.