Giter Site home page Giter Site logo

symfony / panther Goto Github PK

View Code? Open in Web Editor NEW
2.9K 59.0 213.0 450 KB

A browser testing and web crawling library for PHP and Symfony

License: MIT License

PHP 100.00%
scraping e2e-testing webdriver selenium selenium-webdriver symfony php chromedriver hacktoberfest

panther's Introduction

Panther

A browser testing and web scraping library for PHP and Symfony

CI

Panther is a convenient standalone library to scrape websites and to run end-to-end tests using real browsers.

Panther is super powerful. It leverages the W3C's WebDriver protocol to drive native web browsers such as Google Chrome and Firefox.

Panther is very easy to use, because it implements Symfony's popular BrowserKit and DomCrawler APIs, and contains all the features you need to test your apps. It will sound familiar if you have ever created a functional test for a Symfony app: as the API is exactly the same! Keep in mind that Panther can be used in every PHP project, as it is a standalone library.

Panther automatically finds your local installation of Chrome or Firefox and launches them, so you don't need to install anything else on your computer, a Selenium server is not needed!

In test mode, Panther automatically starts your application using the PHP built-in web-server. You can focus on writing your tests or web-scraping scenario and Panther will take care of everything else.

Features

Unlike testing and web scraping libraries you're used to, Panther:

  • executes the JavaScript code contained in webpages
  • supports everything that Chrome (or Firefox) implements
  • allows taking screenshots
  • can wait for asynchronously loaded elements to show up
  • lets you run your own JS code or XPath queries in the context of the loaded page
  • supports custom Selenium server installations
  • supports remote browser testing services including SauceLabs and BrowserStack

Documentation

Installing Panther

Use Composer to install Panther in your project. You may want to use the --dev flag if you want to use Panther for testing only and not for web scraping in a production environment:

composer req symfony/panther

composer req --dev symfony/panther

Installing ChromeDriver and geckodriver

Panther uses the WebDriver protocol to control the browser used to crawl websites.

On all systems, you can use dbrekelmans/browser-driver-installer to install ChromeDriver and geckodriver locally:

composer require --dev dbrekelmans/bdi
vendor/bin/bdi detect drivers

Panther will detect and use automatically drivers stored in the drivers/ directory.

Alternatively, you can use the package manager of your operating system to install them.

On Ubuntu, run:

apt-get install chromium-chromedriver firefox-geckodriver

On Mac, using Homebrew:

brew install chromedriver geckodriver

On Windows, using chocolatey:

choco install chromedriver selenium-gecko-driver

Finally, you can download manually ChromeDriver (for Chromium or Chrome) and GeckoDriver (for Firefox) and put them anywhere in your PATH or in the drivers/ directory of your project.

Registering the PHPUnit Extension

If you intend to use Panther to test your application, we strongly recommend registering the Panther PHPUnit extension. While not strictly mandatory, this extension dramatically improves the testing experience by boosting the performance and allowing to use the interactive debugging mode.

When using the extension in conjunction with the PANTHER_ERROR_SCREENSHOT_DIR environment variable, tests using the Panther client that fail or error (after the client is created) will automatically get a screenshot taken to help debugging.

To register the Panther extension, add the following lines to phpunit.xml.dist:

<!-- phpunit.xml.dist -->
<extensions>
    <extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>

Without the extension, the web server used by Panther to serve the application under test is started on demand and stopped when tearDownAfterClass() is called. On the other hand, when the extension is registered, the web server will be stopped only after the very last test.

Basic Usage

<?php

use Symfony\Component\Panther\Client;

require __DIR__.'/vendor/autoload.php'; // Composer's autoloader

$client = Client::createChromeClient();
// Or, if you care about the open web and prefer to use Firefox
$client = Client::createFirefoxClient();

$client->request('GET', 'https://api-platform.com'); // Yes, this website is 100% written in JavaScript
$client->clickLink('Getting started');

// Wait for an element to be present in the DOM (even if hidden)
$crawler = $client->waitFor('#installing-the-framework');
// Alternatively, wait for an element to be visible
$crawler = $client->waitForVisibility('#installing-the-framework');

echo $crawler->filter('#installing-the-framework')->text();
$client->takeScreenshot('screen.png'); // Yeah, screenshot!

Testing Usage

The PantherTestCase class allows you to easily write E2E tests. It automatically starts your app using the built-in PHP web server and let you crawl it using Panther. To provide all the testing tools you're used to, it extends PHPUnit's TestCase.

If you are testing a Symfony application, PantherTestCase automatically extends the WebTestCase class. It means you can easily create functional tests, which can directly execute the kernel of your application and access all your existing services. In this case, you can use all crawler test assertions provided by Symfony with Panther.

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $client = static::createPantherClient(); // Your app is automatically started using the built-in web server
        $client->request('GET', '/mypage');

        // Use any PHPUnit assertion, including the ones provided by Symfony
        $this->assertPageTitleContains('My Title');
        $this->assertSelectorTextContains('#main', 'My body');
        
        // Or the one provided by Panther
        $this->assertSelectorIsEnabled('.search');
        $this->assertSelectorIsDisabled('[type="submit"]');
        $this->assertSelectorIsVisible('.errors');
        $this->assertSelectorIsNotVisible('.loading');
        $this->assertSelectorAttributeContains('.price', 'data-old-price', '42');
        $this->assertSelectorAttributeNotContains('.price', 'data-old-price', '36');

        // Use waitForX methods to wait until some asynchronous process finish
        $client->waitFor('.popin'); // wait for element to be attached to the DOM
        $client->waitForStaleness('.popin'); // wait for element to be removed from the DOM
        $client->waitForVisibility('.loader'); // wait for element of the DOM to become visible
        $client->waitForInvisibility('.loader'); // wait for element of the DOM to become hidden
        $client->waitForElementToContain('.total', '25 €'); // wait for text to be inserted in the element content
        $client->waitForElementToNotContain('.promotion', '5%'); // wait for text to be removed from the element content
        $client->waitForEnabled('[type="submit"]'); // wait for the button to become enabled 
        $client->waitForDisabled('[type="submit"]'); // wait for  the button to become disabled 
        $client->waitForAttributeToContain('.price', 'data-old-price', '25 €'); // wait for the attribute to contain content
        $client->waitForAttributeToNotContain('.price', 'data-old-price', '25 €'); // wait for the attribute to not contain content
        
        // Let's predict the future
        $this->assertSelectorWillExist('.popin'); // element will be attached to the DOM
        $this->assertSelectorWillNotExist('.popin'); // element will be removed from the DOM
        $this->assertSelectorWillBeVisible('.loader'); // element will be visible
        $this->assertSelectorWillNotBeVisible('.loader'); // element will not be visible
        $this->assertSelectorWillContain('.total', '€25'); // text will be inserted in the element content
        $this->assertSelectorWillNotContain('.promotion', '5%'); // text will be removed from the element content
        $this->assertSelectorWillBeEnabled('[type="submit"]'); // button will be enabled 
        $this->assertSelectorWillBeDisabled('[type="submit"]'); // button will be disabled 
        $this->assertSelectorAttributeWillContain('.price', 'data-old-price', '€25'); // attribute will contain content
        $this->assertSelectorAttributeWillNotContain('.price', 'data-old-price', '€25'); // attribute will not contain content
    }
}

To run this test:

bin/phpunit tests/E2eTest.php

A Polymorphic Feline

Panther also gives you instant access to other BrowserKit-based implementations of Client and Crawler. Unlike Panther's native client, these alternative clients don't support JavaScript, CSS and screenshot capturing, but they are super-fast!

Two alternative clients are available:

  • The first directly manipulates the Symfony kernel provided by WebTestCase. It is the fastest client available, but it is only available for Symfony apps.
  • The second leverages Symfony's HttpBrowser. It is an intermediate between Symfony's kernel and Panther's test clients. HttpBrowser sends real HTTP requests using Symfony's HttpClient component. It is fast and is able to browse any webpage, not only the ones of the application under test. However, HttpBrowser doesn't support JavaScript and other advanced features because it is entirely written in PHP. This one is available even for non-Symfony apps!

The fun part is that the 3 clients implement the exact same API, so you can switch from one to another just by calling the appropriate factory method, resulting in a good trade-off for every single test case (Do I need JavaScript? Do I need to authenticate with an external SSO server? Do I want to access the kernel of the current request? ... etc).

Here is how to retrieve instances of these clients:

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $symfonyClient = static::createClient(); // A cute kitty: Symfony's functional test tool
        $httpBrowserClient = static::createHttpBrowserClient(); // An agile lynx: HttpBrowser
        $pantherClient = static::createPantherClient(); // A majestic Panther
        $firefoxClient = static::createPantherClient(['browser' => static::FIREFOX]); // A splendid Firefox
        // Both HttpBrowser and Panther benefits from the built-in HTTP server

        $customChromeClient = Client::createChromeClient(null, null, [], 'https://example.com'); // Create a custom Chrome client
        $customFirefoxClient = Client::createFirefoxClient(null, null, [], 'https://example.com'); // Create a custom Firefox client
        $customSeleniumClient = Client::createSeleniumClient('http://127.0.0.1:4444/wd/hub', null, 'https://example.com'); // Create a custom Selenium client
        // When initializing a custom client, the integrated web server IS NOT started automatically.
        // Use PantherTestCase::startWebServer() or WebServerManager if you want to start it manually.

        // enjoy the same API for the 3 felines
        // $*client->request('GET', '...')

        $kernel = static::createKernel(); // If you are testing a Symfony app, you also have access to the kernel

        // ...
    }
}

Creating Isolated Browsers to Test Apps Using Mercure or WebSocket

Panther provides a convenient way to test applications with real-time capabilities which use Mercure, WebSocket and similar technologies.

PantherTestCase::createAdditionalPantherClient() creates additional, isolated browsers which can interact with each other. For instance, this can be useful to test a chat application having several users connected simultaneously:

<?php

use Symfony\Component\Panther\PantherTestCase;

class ChatTest extends PantherTestCase
{
    public function testChat(): void
    {
        $client1 = self::createPantherClient();
        $client1->request('GET', '/chat'); 
 
        // Connect a 2nd user using an isolated browser and say hi!
        $client2 = self::createAdditionalPantherClient();
        $client2->request('GET', '/chat');
        $client2->submitForm('Post message', ['message' => 'Hi folks 👋😻']);

        // Wait for the message to be received by the first client
        $client1->waitFor('.message');

        // Symfony Assertions are always executed in the **primary** browser
        $this->assertSelectorTextContains('.message', 'Hi folks 👋😻');
    }
}

Accessing Browser Console Logs

If needed, you can use Panther to access the content of the console:

<?php

use Symfony\Component\Panther\PantherTestCase;

class ConsoleTest extends PantherTestCase
{
    public function testConsole(): void
    {
        $client = self::createPantherClient(
            [],
            [],
            [
                'capabilities' => [
                    'goog:loggingPrefs' => [
                        'browser' => 'ALL', // calls to console.* methods
                        'performance' => 'ALL', // performance data
                    ],
                ],
            ]
        );

        $client->request('GET', '/');
        $consoleLogs = $client->getWebDriver()->manage()->getLog('browser'); // console logs 
        $performanceLogs = $client->getWebDriver()->manage()->getLog('performance'); // performance logs
    }
}

Passing Arguments to ChromeDriver

If needed, you can configure the arguments to pass to the chromedriver binary:

<?php

use Symfony\Component\Panther\PantherTestCase;

class MyTest extends PantherTestCase
{
    public function testLogging(): void
    {
        $client = self::createPantherClient(
            [],
            [],
            [
                'chromedriver_arguments' => [
                    '--log-path=myfile.log',
                    '--log-level=DEBUG'
                ],
            ]
        );

        $client->request('GET', '/');
    }
}

Checking the State of the WebDriver Connection

Use the Client::ping() method to check if the WebDriver connection is still active (useful for long-running tasks).

Additional Documentation

Since Panther implements the API of popular libraries, it already has an extensive documentation:

Environment Variables

The following environment variables can be set to change some Panther's behaviour:

  • PANTHER_NO_HEADLESS: to disable the browser's headless mode (will display the testing window, useful to debug)
  • PANTHER_WEB_SERVER_DIR: to change the project's document root (default to ./public/, relative paths must start by ./)
  • PANTHER_WEB_SERVER_PORT: to change the web server's port (default to 9080)
  • PANTHER_WEB_SERVER_ROUTER: to use a web server router script which is run at the start of each HTTP request
  • PANTHER_EXTERNAL_BASE_URI: to use an external web server (the PHP built-in web server will not be started)
  • PANTHER_APP_ENV: to override the APP_ENV variable passed to the web server running the PHP app
  • PANTHER_ERROR_SCREENSHOT_DIR: to set a base directory for your failure/error screenshots (e.g. ./var/error-screenshots)
  • PANTHER_DEVTOOLS: to toggle the browser's dev tools (default enabled, useful to debug)
  • PANTHER_ERROR_SCREENSHOT_ATTACH: to add screenshots mentioned above to test output in junit attachment format

Changing the Hostname and Port of the Built-in Web Server

If you want to change the host and/or the port used by the built-in web server, pass the hostname and port to the $options parameter of the createPantherClient() method:

// ...

$client = self::createPantherClient([
    'hostname' => 'example.com', // Defaults to 127.0.0.1
    'port' => 8080, // Defaults to 9080
]);

Chrome-specific Environment Variables

  • PANTHER_NO_SANDBOX: to disable Chrome's sandboxing (unsafe, but allows to use Panther in containers)
  • PANTHER_CHROME_ARGUMENTS: to customize Chrome arguments. You need to set PANTHER_NO_HEADLESS to fully customize.
  • PANTHER_CHROME_BINARY: to use another google-chrome binary

Firefox-specific Environment Variables

  • PANTHER_FIREFOX_ARGUMENTS: to customize Firefox arguments. You need to set PANTHER_NO_HEADLESS to fully customize.
  • PANTHER_FIREFOX_BINARY: to use another firefox binary

Accessing To Hidden Text

According to the spec, WebDriver implementations return only the displayed text by default. When you filter on a head tag (like title), the method text() returns an empty string. Use the method html() to get the complete contents of the tag, including the tag itself.

Interactive Mode

Panther can make a pause in your tests suites after a failure. It is a break time really appreciated for investigating the problem through the web browser. For enabling this mode, you need the --debug PHPUnit option without the headless mode:

$ PANTHER_NO_HEADLESS=1 bin/phpunit --debug

Test 'App\AdminTest::testLogin' started
Error: something is wrong.

Press enter to continue...

To use the interactive mode, the PHPUnit extension must be registered.

Using an External Web Server

Sometimes, it's convenient to reuse an existing web server configuration instead of starting the built-in PHP one. To do so, set the external_base_uri option:

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $pantherClient = static::createPantherClient(['external_base_uri' => 'https://localhost']);
        // the PHP integrated web server will not be started
    }
}

Having a Multi-domain Application

It happens that your PHP/Symfony application might serve several different domain names.

As Panther saves the Client in memory between tests to improve performances, you will have to run your tests in separate processes if you write several tests using Panther for different domain names.

To do so, you can use the native @runInSeparateProcess PHPUnit annotation.

ℹ Note: it is really convenient to use the external_base_uri option and start your own webserver in the background, because Panther will not have to start and stop your server on each test. Symfony CLI can be a quick and easy way to do so.

Here is an example using the external_base_uri option to determine the domain name used by the Client:

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class FirstDomainTest extends PantherTestCase
{
    /**
     * @runInSeparateProcess
     */
    public function testMyApp(): void
    {
        $pantherClient = static::createPantherClient([
            'external_base_uri' => 'http://mydomain.localhost:8000',
        ]);
        
        // Your tests
    }
}
<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class SecondDomainTest extends PantherTestCase
{
    /**
     * @runInSeparateProcess
     */
    public function testMyApp(): void
    {
        $pantherClient = static::createPantherClient([
            'external_base_uri' => 'http://anotherdomain.localhost:8000',
        ]);
        
        // Your tests
    }
}

Using a Proxy

To use a proxy server, set the following environment variable: PANTHER_CHROME_ARGUMENTS='--proxy-server=socks://127.0.0.1:9050'

Accepting Self-signed SSL Certificates

To force Chrome to accept invalid and self-signed certificates, set the following environment variable: PANTHER_CHROME_ARGUMENTS='--ignore-certificate-errors' This option is insecure, use it only for testing in development environments, never in production (e.g. for web crawlers).

For Firefox, instantiate the client like this:

$client = Client::createFirefoxClient(null, null, ['capabilities' => ['acceptInsecureCerts' => true]]);

Docker Integration

Here is a minimal Docker image that can run Panther with both Chrome and Firefox:

FROM php:alpine

# Chromium and ChromeDriver
ENV PANTHER_NO_SANDBOX 1
# Not mandatory, but recommended
ENV PANTHER_CHROME_ARGUMENTS='--disable-dev-shm-usage'
RUN apk add --no-cache chromium chromium-chromedriver

# Firefox and GeckoDriver (optional)
ARG GECKODRIVER_VERSION=0.28.0
RUN apk add --no-cache firefox libzip-dev; \
    docker-php-ext-install zip
RUN wget -q https://github.com/mozilla/geckodriver/releases/download/v$GECKODRIVER_VERSION/geckodriver-v$GECKODRIVER_VERSION-linux64.tar.gz; \
    tar -zxf geckodriver-v$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/bin; \
    rm geckodriver-v$GECKODRIVER_VERSION-linux64.tar.gz

Build it with docker build . -t myproject Run it with docker run -it -v "$PWD":/srv/myproject -w /srv/myproject myproject bin/phpunit

GitHub Actions Integration

Panther works out of the box with GitHub Actions. Here is a minimal .github/workflows/panther.yml file to run Panther tests:

name: Run Panther tests

on: [ push, pull_request ]

jobs:
  tests:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Install dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

      - name: Run test suite
        run: bin/phpunit

Travis CI Integration

Panther will work out of the box with Travis CI if you add the Chrome addon. Here is a minimal .travis.yml file to run Panther tests:

language: php
addons:
  # If you don't use Chrome, or Firefox, remove the corresponding line
  chrome: stable
  firefox: latest

php:
  - 8.0

script:
  - bin/phpunit

Gitlab CI Integration

Here is a minimal .gitlab-ci.yml file to run Panther tests with Gitlab CI:

image: ubuntu

before_script:
  - apt-get update
  - apt-get install software-properties-common -y
  - ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
  - apt-get install curl wget php php-cli php7.4 php7.4-common php7.4-curl php7.4-intl php7.4-xml php7.4-opcache php7.4-mbstring php7.4-zip libfontconfig1 fontconfig libxrender-dev libfreetype6 libxrender1 zlib1g-dev xvfb chromium-chromedriver firefox-geckodriver -y -qq
  - export PANTHER_NO_SANDBOX=1
  - export PANTHER_WEB_SERVER_PORT=9080
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php composer-setup.php --install-dir=/usr/local/bin --filename=composer
  - php -r "unlink('composer-setup.php');"
  - composer install

test:
  script:
    - bin/phpunit

AppVeyor Integration

Panther will work out of the box with AppVeyor as long as Google Chrome is installed. Here is a minimal appveyor.yml file to run Panther tests:

build: false
platform: x86
clone_folder: c:\projects\myproject

cache:
  - '%LOCALAPPDATA%\Composer\files'

install:
  - ps: Set-Service wuauserv -StartupType Manual
  - cinst -y php composer googlechrome chromedriver firfox selenium-gecko-driver
  - refreshenv
  - cd c:\tools\php80
  - copy php.ini-production php.ini /Y
  - echo date.timezone="UTC" >> php.ini
  - echo extension_dir=ext >> php.ini
  - echo extension=php_openssl.dll >> php.ini
  - echo extension=php_mbstring.dll >> php.ini
  - echo extension=php_curl.dll >> php.ini
  - echo memory_limit=3G >> php.ini
  - cd %APPVEYOR_BUILD_FOLDER%
  - composer install --no-interaction --no-progress

test_script:
  - cd %APPVEYOR_BUILD_FOLDER%
  - php bin\phpunit

Usage with Other Testing Tools

If you want to use Panther with other testing tools like LiipFunctionalTestBundle or if you just need to use a different base class, Panther has got you covered. It provides you with the Symfony\Component\Panther\PantherTestCaseTrait and you can use it to enhance your existing test-infrastructure with some Panther awesomeness:

<?php

namespace App\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;
use Symfony\Component\Panther\PantherTestCaseTrait;

class DefaultControllerTest extends WebTestCase
{
    use PantherTestCaseTrait; // this is the magic. Panther is now available.

    public function testWithFixtures(): void
    {
        $this->loadFixtures([]); // load your fixtures
        $client = self::createPantherClient(); // create your panther client

        $client->request('GET', '/');
    }
}

Limitations

The following features are not currently supported:

  • Crawling XML documents (only HTML is supported)
  • Updating existing documents (browsers are mostly used to consume data, not to create webpages)
  • Setting form values using the multidimensional PHP array syntax
  • Methods returning an instance of \DOMElement (because this library uses WebDriverElement internally)
  • Selecting invalid choices in select

Pull Requests are welcome to fill the remaining gaps!

Troubleshooting

Run with Bootstrap 5

If you are using Bootstrap 5, then you may have a problem with testing. Bootstrap 5 implements a scrolling effect, which tends to mislead Panther.

To fix this, we advise you to deactivate this effect by setting the Bootstrap 5 $enable-smooth-scroll variable to false in your style file.

$enable-smooth-scroll: false;

Save the Panthers

Many of the wild cat species are highly threatened. If you like this software, help save the (real) panthers by donating to the Panthera organization.

Credits

Created by Kévin Dunglas. Sponsored by Les-Tilleuls.coop.

Panther is built on top of PHP WebDriver and several other FOSS libraries. It has been inspired by Nightwatch.js, a WebDriver-based testing tool for JavaScript.

panther's People

Contributors

andreybolonin avatar arderyp avatar chalasr avatar changeplaces avatar coraxster avatar derrabus avatar dunglas avatar fabpot avatar gregcop1 avatar gseidel avatar hypemc avatar kbond avatar kissifrot avatar lyrixx avatar maidmaid avatar matthieumota avatar metaer avatar nicolas-grekas avatar nusje2000 avatar nyholm avatar oskarstark avatar owlycode avatar pborreli avatar pierstoval avatar pkruithof avatar robertfausk avatar rocramer avatar samnela avatar thomaslandauer avatar trakos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

panther's Issues

Is Josh K's seal of approval still necessary?

if ($_SERVER['PANTHER_NO_SANDBOX'] ?? $_SERVER['HAS_JOSH_K_SEAL_OF_APPROVAL'] ?? false)

I don't know a JoshK to ask for his seal of approval :( Should this be removed?

(Sorry for all these issues - I'm working on writing lots of e2e tests at the moment!)

[QUESTION] Chrome Driver - Docker image

Hi everyone,

Small question about the usage of Panther into a PHP Alpine image, I've tried to install Chromedriver and chromium but here's the error that I get:

#!/usr/bin/env php
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

Error:         No code coverage driver is available

Testing E2E
E                                                                   1 / 1 (100%)

Time: 8.08 seconds, Memory: 40.25MB

There was 1 error:

1) E2E\Core\HomeE2ETest::testStatusCode
RuntimeException: sh: exec: line 1: /var/www/marketReminder/vendor/symfony/panther/src/ProcessManager/../../chromedriver-bin/chromedriver_linux64: not found


/var/www/marketReminder/vendor/symfony/panther/src/ProcessManager/WebServerReadinessProbeTrait.php:50
/var/www/marketReminder/vendor/symfony/panther/src/ProcessManager/ChromeManager.php:49
/var/www/marketReminder/vendor/symfony/panther/src/Client.php:80
/var/www/marketReminder/vendor/symfony/panther/src/Client.php:272
/var/www/marketReminder/vendor/symfony/panther/src/Client.php:186
/var/www/marketReminder/E2E/Core/HomeE2ETest.php:26

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Remaining deprecation notices (1)

  1x: Doctrine\Common\ClassLoader is deprecated.
    1x in HomeE2ETest::testStatusCode from E2E\Core

make: *** [panther] Error 2

For information, here's the Dockerfile:

# Development build
FROM php:fpm-alpine as base

ARG WORKFOLDER

ENV COMPOSER_ALLOW_SUPERUSER 1
ENV PANTHER_NO_SANDBOX 1
ENV PANTHER_WEB_SERVER_PORT 9800
ENV WORKPATH ${WORKFOLDER}

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS icu-dev postgresql-dev gnupg graphviz make autoconf git zlib-dev curl chromium chromium-chromedriver go rabbitmq-c rabbitmq-c-dev \
    && docker-php-ext-configure pgsql --with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install zip intl pdo_pgsql pdo_mysql opcache json pdo_pgsql pgsql mysqli \
    && pecl install apcu redis grpc protobuf amqp \
    && docker-php-ext-enable apcu mysqli redis grpc protobuf amqp

COPY docker/php/conf/php.ini /usr/local/etc/php/php.ini

# Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Blackfire (Docker approach) & Blackfire Player
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
    && curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/alpine/amd64/$version \
    && tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp \
    && mv /tmp/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
    && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
    && mkdir -p /tmp/blackfire \
    && curl -A "Docker" -L https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \
    && mv /tmp/blackfire/blackfire /usr/bin/blackfire \
    && rm -Rf /tmp/blackfire \
    && curl -OLsS http://get.blackfire.io/blackfire-player.phar \
    && chmod +x blackfire-player.phar \
    && mv blackfire-player.phar /usr/local/bin/blackfire-player

# PHP-CS-FIXER & Deptrac
RUN wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer \
    && chmod a+x php-cs-fixer \
    && mv php-cs-fixer /usr/local/bin/php-cs-fixer \
    && curl -LS http://get.sensiolabs.de/deptrac.phar -o deptrac.phar \
    && chmod +x deptrac.phar \
    && mv deptrac.phar /usr/local/bin/deptrac

RUN mkdir -p ${WORKPATH} \
    && chown -R www-data /tmp/ \
    && mkdir -p \
       ${WORKPATH}/var/cache \
       ${WORKPATH}/var/logs \
       ${WORKPATH}/var/sessions \
    && chown -R www-data ${WORKPATH}/var

WORKDIR ${WORKPATH}

COPY --chown=www-data:www-data . ./

# Production build
FROM base as production

COPY --from=gcr.io/marketreminder-206206/node:latest ${WORKPATH}/public/build ./public/build

COPY docker/php/conf/production/php.ini /usr/local/etc/php/php.ini

RUN rm -rf /usr/local/bin/deptrac \
    && rm -rf /usr/local/bin/php-cs-fixer

I don't really see where the problem is coming from and i've search to see if someone has the same problem but nothing about this type of error, does anyone has an idea ?

PS: I've tried the installation recommanded by the documentation but the driver doesn't seem to be found

Thanks for the help :)

Display the testing window if run in docker

is it possible to run tests with ENV PANTHER_NO_HEADLESS 1 in docker ?

now i get error


Fatal error: Uncaught Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--no-sandbox"]}}}

Operation timed out after 30001 milliseconds with 0 bytes received in /app/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:292
Stack trace:
#0 /app/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php(126): Facebook\WebDriver\Remote\HttpCommandExecutor->execute(Object(Facebook\WebDriver\Remote\WebDriverCommand))
#1 /app/vendor/symfony/panther/src/ProcessManager/ChromeManager.php(59): Facebook\WebDriver\Remote\RemoteWebDriver::create('http://127.0.0....', Object(Facebook\WebDriver\Remote\DesiredCapabilities))
#2 /app/vendor/symfony/panther/src/Client.php(74): Symfony\Component\Panther\ProcessManager\ChromeManager->start()
#3 /app/vendor/symfony/panther/src/Client.php(245): Symfony\Component\Panther\Client->start()
#4 /app/vendor/symfony/panther/src/Client.ph in /app/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php on line 292

i tried to start chromedriver on host machine, and share 9515 port - but same error

Symfony 3.4 support

Hi,

is there a specific reason why Panther requires symfony/browser-kit and symfony/console in ^4.0 version?

Since Symfony 4.0 does not yet have an LTS version, and since it is fully compatible with 3.4, it would be nice to allow installing Panther with Symfony 3.4 present.

Thanks!

$client->request('GET', $nonExistantUrl) returns status 200

I've just spent the last few hours trying to figure out why a url that should be 404'ing is returning 200. I assumed it was a problem with the cache, but after dd'ing the response, clearly it isn't. The response contains the normal 404 response text from symfony, but with a 200 status.

reproduce: use a panther client to make a request to a non-existing url, and see the status is 200, instead of 404.

should getCurrentUrl return the full url, or the relative url?

It's a bit strange when making a $client->request('GET', '/foos/') for $client->getCurrentURL() to return the full url including the host. Shouldn't it just return the relative url?

e.g. getCurrentURL returns: http://127.0.0.1:9000/foos/, and I think it should return /foos/.

Symfony syntax not working ?

Hello, this code is working fine with Symfony crawler, but it's not working anymore with panthere

$crawler->filter('div:contains("This value should not be blank")')

Here is the error:
WebDriver\Exception\InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

How to change the environment the website runs in?

When working with panther tests, the website seems to be running with dev mode config, even though if I dd(getenv('APP_ENV')) it reports it's in 'test' environment. This causes lots of problems when testing because of the web profiler toolbar overlay preventing buttons from being clicked.

The toolbar is disabled for the test env per the config, and the only way to prevent the toolbar from showing is by editing the config/dev/web_profiler and set toolbar:false. Something I shouldn't have to do as I'm in the test environment

Why is this crazy behaviour happening?

Impossible to get the HttpFoundation objects from the Client

Q A
Bug report? yes
Feature request? no
BC Break report? yes

When using PanthereTestCase, and when making a request, we can't get access to the Symfony\Component\HttpFoundation\Response object that should normally be sent to the Kernel. Instead, we only get access to the BrowserKit one.

Steps to reproduce:

<?php

use Panthere\PanthereTestCase;

class MyPanthereTest extends PanthereTestCase
{
    public function testMyApp()
    {
        $client = static::createPanthereClient();
        $crawler = $client->request('GET', static::$baseUri.'/any-page');

        static::assertSame(200, $client->getResponse()->getStatusCode());
    }
}

PHPUnit's output:

Error : Call to undefined method Symfony\Component\BrowserKit\Response::getStatusCode()

I consider this as a BC break, because the $client->request() method SHOULD return an instance of the Symfony\Component\BrowserKit\Client class, for which getResponse() returns an instance of Symfony\Component\HttpFoundation\Response.

This is the same for the Request via $client->getRequest() by the way.

The issue comes from these lines:
https://github.com/dunglas/panthere/blob/b3f0601e105010d365360f238ca1b572a4e5ec82/src/Client.php#L238-L241

IMO, the Request and Response object should be either blocked from the PanthereClient via an exception (easy pick), or created & adapted based on the WebDriver results (harder).

WDYT?

Web server hanging on port 9000

My test isn't running at all, it appears to be pausing until stopped. I've tracked it down to: PantherTestCaseTrait, specifically StartWebServer, and after the lines:

        self::$webServerManager = new WebServerManager($webServerDir, $hostname, $port);
        self::$webServerManager->start();

It would appear the server starts, but then blocks.
My test is directly from the examples

        $client = static::createPantherClient(); // Your app is automatically started using the built-in web server
        $crawler = $client->request('GET', '/');
        $this->assertContains('My Title', $crawler->filter('title')->text()); // You can use any PHPUnit assertion

After a bit more digging, it would seem although port 9000 is free, it really will not run on 9000 and hangs. Changing to port 9001 doesn't present any such problem. I'd like to suggest a quick note in the readme as being able to pass in the port isn't immediately obvious.

As seeing nothing during a hung test run, is there some way the server can be tested on the port before starting it without having to do an nmap?

Missing execution bit from chromedriver preventing its execution

Hi,
I'm trying to use Panthère in Symfony on Debian, but I have this error when launching my tests :
RuntimeException: sh: 1: exec: /srv/app/vendor/symfony/panthere/src/ProcessManager/../../chromedriver-bin/chromedriver_linux64: Permission denied

Indeed, here's the permission status :
image

Is there a way to install chromedriver without setting permissions by hand ?

feature request: some shorthand way to retrieve input values?

Getting the value of inputs is fairly common: $crawler->filter('#course_title')->attr('value') is perhaps a little long-winded. Could a helper function like $crawler->filter('#foo')->value() be more appropriate? Or even something shorter like $crawler->input('#foo')->val()?

client->wait is really client->maxtimeout

I've been using $client->wait(10) as a sleep, thinking, it'll wait for x seconds, right? Except it doesn't. Digging through the code, it's a max time out for an operation rather than a wait:

     *   $driver->wait(20, 1000)->until(
     *     WebDriverExpectedCondition::titleIs('WebDriver Page')
     *   );

Can this be renamed, or perhaps made a little clearer somehow? Maybe passing the until condition as a closure to the wait method?

Clean dir structure by adding a src/PHPUnit folder

src
├── ...
└── PHPUnit
    ├── PantherTestCase.php
    ├── PantherTestCaseTrait.php
    ├── ServerExtension.php
    ├── ServerListener.php
    └── ServerTrait.php

It would be better like that, no ?

Make sexy the full mouse API

The current mouse API of the Panther client is still little bit messy and confusing, despite #128. Have a look:

click(Symfony\Component\DomCrawler\Link $link)
getMouse()->click(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where)
getMouse()->clickTo($cssSelector)
getMouse()->contextClick(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where) 
getMouse()->contextClickTo($cssSelector) 
getMouse()->doubleClick(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where)
getMouse()->doubleClickTo($cssSelector)
getMouse()->mouseDown(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where)
getMouse()->mouseDownTo($cssSelector)
getMouse()->mouseMove(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where, $xOffset = null, $yOffset = null)
getMouse()->mouseMoveTo($cssSelector, $xOffset = null, $yOffset = null)
getMouse()->mouseUp(Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates $where)
getMouse()->mouseUpTo($cssSelector)

I suggest:

getMouse() // is marked as internal
click($cssSelector) // is able to deal with Crawler Link, Panther Link and getMouse()->clickTo($cssSelector)
contextClick($cssSelector) // calls getMouse()->contextClickTo($cssSelector) 
doubleClick($cssSelector) // calls getMouse()->doubleClickTo($cssSelector)
mouseDown($cssSelector) // calls getMouse()->mouseDownTo($cssSelector)
mouseMove($cssSelector, $xOffset = null, $yOffset = null) // calls getMouse()->mouseMoveTo($cssSelector, $xOffset = null, $yOffset = null)
mouseUp($cssSelector) // calls getMouse()->mouseUpTo($cssSelector)

InvalidArgumentException: The "Symfony\Component\Panther\DomCrawler\Crawler::clear" method is not supported when using WebDriver.

I use the filter method to target elements, and I want to clear out the product title, and type "test title2":

$crawler->filter('#product_title')->clear()->sendKeys('test title2');

But this gives the error: InvalidArgumentException: The "Symfony\Component\Panther\DomCrawler\Crawler::clear" method is not supported when using WebDriver.

I instead have to use:
$crawler->findElement(WebDriverBy::id('product_title'))->clear()->sendKeys('test title2');

I think this is an inconsistency which need addressing?

Example code throws Exception

When using the example code, i.e

$client = \Panthere\Client::createChromeClient();
        $crawler = $client->request('GET', 'http://api-platform.com'); // Yes, this website is 100% in JavaScript

        $link = $crawler->selectLink('Support')->link();
        $crawler = $client->click($link);

// Wait for an element to be rendered
        $client->waitFor('.support');

        echo $crawler->filter('.support')->text();

An Exception is thrown:

Return value of Panthere\Client::waitFor() must be an instance of Panthere\object, instance of Facebook\WebDriver\Remote\RemoteWebElement returned

TypeError
in vendor/symfony/panthere/src/Client.php (line 230)
Client->waitFor('.support')
in src/Controller/StocksController.php (line 97)
        $link = $crawler->selectLink('Support')->link();        $crawler = $client->click($link);// Wait for an element to be rendered        $client->waitFor('.support');        echo $crawler->filter('.support')->text();        // $client->takeScreenshot('screen.png'); // Yeah, screenshot!    }
StocksController->seekingalphaDividendScraper('R')
in src/Controller/StocksController.php (line 75)
in vendor/symfony/http-kernel/HttpKernel.php->build (line 149)
in vendor/symfony/http-kernel/HttpKernel.php->handleRaw (line 66)
in vendor/symfony/http-kernel/Kernel.php->handle (line 188)
Kernel->handle(object(Request))
in public/index.php (line 37)

PHP Version

$ php -v
PHP 7.2.7 (cli) (built: Jun 22 2018 06:27:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies

Composer.json

{
  "type": "project",
  "license": "proprietary",
  "require": {
    "php": "^7.1.3",
    "ext-iconv": "*",
    "eleirbag89/telegrambotphp": "dev-master",
    "mgp25/instagram-php": "^4.1",
    "paquettg/php-html-parser": "^1.7",
    "sensio/framework-extra-bundle": "^5.1",
    "shaked/time": "dev-master",
    "symfony/asset": "^4.1",
    "symfony/console": "^4.1",
    "symfony/expression-language": "^4.1",
    "symfony/flex": "^1.0",
    "symfony/form": "^4.1",
    "symfony/framework-bundle": "^4.1",
    "symfony/lts": "^4@dev",
    "symfony/monolog-bundle": "^3.1",
    "symfony/orm-pack": "*",
    "symfony/panthere": "dev-master",
    "symfony/process": "^4.1",
    "symfony/security-bundle": "^4.1",
    "symfony/serializer-pack": "*",
    "symfony/swiftmailer-bundle": "^3.1",
    "symfony/twig-bundle": "^4.1",
    "symfony/validator": "^4.1",
    "symfony/web-link": "^4.1",
    "symfony/webpack-encore-pack": "*",
    "symfony/yaml": "^4.1"
  },
  "require-dev": {
    "symfony/debug-pack": "*",
    "symfony/dotenv": "^4.1",
    "symfony/maker-bundle": "^1.0",
    "symfony/profiler-pack": "*",
    "symfony/test-pack": "^1.0",
    "symfony/web-server-bundle": "^4.1"
  },
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/Shaked/TelegramBotPHP"
    }
  ],
  "config": {
    "preferred-install": {
      "*": "dist"
    },
    "sort-packages": true
  },
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "App\\Tests\\": "tests/"
    }
  },
  "replace": {
    "symfony/polyfill-iconv": "*",
    "symfony/polyfill-php71": "*",
    "symfony/polyfill-php70": "*",
    "symfony/polyfill-php56": "*"
  },
  "scripts": {
    "auto-scripts": {
      "cache:clear": "symfony-cmd",
      "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
      "@auto-scripts"
    ],
    "post-update-cmd": [
      "@auto-scripts"
    ]
  },
  "conflict": {
    "symfony/symfony": "*"
  },
  "extra": {
    "symfony": {
      "allow-contrib": false
    }
  }
}

Update example code with a simple css select and input text entry

As a complete newbie, I think a quick addition to the example code will be useful to help populate inputs, so something like:

        // populate the search box with some text - this will trigger a search box to appear with results
        $client->findElement(WebDriverBy::cssSelector('.search__input.ds-input'))
            ->sendKeys('how');

        // Wait for the dynamic searchbox to appear
        $client->waitFor('#algolia-autocomplete-listbox-0');

How to use with kahlan

I could like to use panther in my symfony project, I am not using PhPUnit but kahlan which is using natural langage & describe syntax, It do not use any classes, how should I use panther in this kind of framework ?

For sure I can use namespace but what about the class to extend ? 🧐

error while loading shared libraries: libnss3.so

So, wanted to give a go to this project. When running example in readme, this happens:

Fatal error: Uncaught RuntimeException: /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/ProcessManager/../../chromedriver-bin/chromedriver_linux64: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
 in /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/ProcessManager/WebServerReadinessProbeTrait.php:62
Stack trace:
#0 /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/ProcessManager/ChromeManager.php(48): Panthere\ProcessManager\ChromeManager->waitUntilReady(Object(Symfony\Component\Process\Process), 'http://127.0.0....')
#1 /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/Client.php(65): Panthere\ProcessManager\ChromeManager->start()
#2 /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/Client.php(236): Panthere\Client->start()
#3 /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/Client.php(156): Panthere\Client->get('http://api-plat...')
#4 /root/.composer/vendor-bin/dunglas/test.php(6) in /root/.composer/vendor-bin/dunglas/vendor/dunglas/panthere/src/ProcessManager/WebServerReadinessProbeTrait.php on line 62

Port 9000 is already in use

Hello,

On my mac the port 9000 is already in use.

capture d ecran 2018-04-05 a 15 18 00

and this is my code

capture d ecran 2018-04-05 a 15 21 09

Have you ever had this problem? is there a way to change it?

Missing ServerExtension.php in last release

The class Symfony\Component\Panther\ServerExtension described in the section "Improve Performances by Having a Persistent Web Server Running" of the readme is missing in the last release.

Make server creation more flexible

According to this code:

https://github.com/dunglas/panthere/blob/b3f0601e105010d365360f238ca1b572a4e5ec82/src/PanthereTestCase.php#L85-L100

I would suggest a few things (before making a PR for it):

  • Resolve public dir from the composer.json file if possible (maybe create a getWebServerDir() protected method)
  • Make the server IP and Port customizable via the startWebServer() arguments (especially IP, because it's mandatory to test multi-domains applications)

WDYT?

What about the Chrome DevTools Protocol ?

The Chrome DevTools Protocol allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers.

https://chromedevtools.github.io/devtools-protocol/

Basically, Chrome allows to be started with a --remote-debugging-port agument which gives access to the DevTools. That could significantly expand what we can do by manipulating for instance the Network panel in the DevTools. But maybe, integrating it in this project is off topic ?

Impossible to start the server on Windows

Here's my test case:

namespace Tests\EsterenMaps\Controller\PanthereTests;

use Panthere\PanthereTestCase;
use Tests\WebTestCase as PiersTestCase;

class JSMapsControllerTest extends PanthereTestCase
{
    use PiersTestCase;

    public function testMapIndex()
    {
        static::startWebServer(null);
        $client = static::createPanthereClient();

        $crawler = $client->request('GET', 'http://maps.esteren.piers:9000/fr/map/map-tri-kazel');

        static::assertSame(200, $client->getInternalResponse()->getStatus());
        static::assertCount(1, $crawler->filter('#map_wrapper'), 'Map view seems broken');
    }
}

The maps.esteren.piers is resolved to 127.0.0.1 in my hosts file so it's not an issue with hosts.

I'm using the #31 fix

For debug purposes, I updated WebServerReadinessProbeTrait::waitUntilReady() a little to check why it is throwing an exception:

+dump($process->getStatus());

while (Process::STATUS_STARTED !== ($status = $process->getStatus()) || false === @\file_get_contents($url, false, $context)) {
+	dump($process->getStatus());
	if (Process::STATUS_TERMINATED === $status) {
+		dump($process);
		throw new \RuntimeException($process->getErrorOutput(), $process->getExitCode());
	}

	// block until the web server is ready
	\usleep(1000);
}

Here's my output:

Testing Tests\EsterenMaps\Controller\PanthereTests\JSMapsControllerTest
"started"
"started"
"terminated"
"terminated"
Symfony\Component\Process\Process {#38242
  -callback: null
  -hasCallback: false
  -commandline: array:6 [
    0 => "E:\dev\php72-nts\php.exe"
    1 => "-dvariables_order=EGPCS"
    2 => "-S"
    3 => "127.0.0.1:9000"
    4 => "-t"
    5 => "E:\dev\www\corahn_rin\vendor\dunglas\panthere\src/../../../../public"
  ]
  -cwd: "E:\dev\www\corahn_rin\vendor\dunglas\panthere\src/../../../../public"
  -env: null
  -input: null
  -starttime: 1528881806.93
  -lastOutputTime: 1528881806.93
  -timeout: null
  -idleTimeout: null
  -exitcode: -1073740791
  -fallbackStatus: []
  -processInformation: array:8 [
    "command" => "cmd /V:ON /E:ON /D /C (E:\dev\php72-nts\php.exe -dvariables_order=EGPCS -S 127.0.0.1:9000 -t "E:\dev\www\corahn_rin\vendor\dunglas\panthere\src/../../../../public") 1>"E:\dev\www\tmp\php72\sf_proc_00.out" 2>"E:\dev\www\tmp\php72\sf_proc_00.err""
    "pid" => 9492
    "running" => false
    "signaled" => false
    "stopped" => false
    "exitcode" => -1073740791
    "termsig" => 0
    "stopsig" => 0
  ]
  -outputDisabled: false
  -stdout: stream resource {@5903
    wrapper_type: "PHP"
    stream_type: "TEMP"
    mode: "w+b"
    unread_bytes: 0
    seekable: true
    uri: "php://temp/maxmemory:1048576"
    options: []
  }
  -stderr: stream resource {@5905
    wrapper_type: "PHP"
    stream_type: "TEMP"
    mode: "w+b"
    unread_bytes: 0
    seekable: true
    uri: "php://temp/maxmemory:1048576"
    options: []
  }
  -process: Closed resource @5917
  -status: "terminated"
  -incrementalOutputOffset: 0
  -incrementalErrorOutputOffset: 0
  -tty: null
  -pty: false
  -useFileHandles: true
  -processPipes: Symfony\Component\Process\Pipes\WindowsPipes {#36889
    -files: array:2 [
      1 => "E:\dev\www\tmp\php72\sf_proc_00.out"
      2 => "E:\dev\www\tmp\php72\sf_proc_00.err"
    ]
    -fileHandles: []
    -readBytes: array:2 [
      1 => 0
      2 => 0
    ]
    -haveReadSupport: true
    +pipes: []
    -inputBuffer: ""
    -input: null
    -blocked: false
    -lastError: null
  }
  -latestSignal: null
}

 E:\dev\www\corahn_rin\vendor\dunglas\panthere\src\ProcessManager\WebServerReadinessProbeTrait.php:54
 E:\dev\www\corahn_rin\vendor\dunglas\panthere\src\ProcessManager\WebServerManager.php:69
 E:\dev\www\corahn_rin\vendor\dunglas\panthere\src\PanthereTestCase.php:99
 E:\dev\www\corahn_rin\tests\EsterenMaps\Controller\PanthereTests\JSMapsControllerTest.php:23

Something very strange is that if I run the provided command-line instruction:

cmd /V:ON /E:ON /D /C (E:\dev\php72-nts\php.exe -dvariables_order=EGPCS -S 127.0.0.1:9000 -t "E:\dev\www\corahn_rin\vendor\dunglas\panthere\src/../../../../public") 1>"E:\dev\www\tmp\php72\sf_proc_00.out" 2>"E:\dev\www\tmp\php72\sf_proc_00.err"

The server runs properly and I can make HTTP calls to it (even if I retrieve HTTP errors because of env vars), but at least it keeps being started and doesn't quit anytime.

Could it be that file_get_contents() that makes the server fail? Or anything else? Am I missing something?

I'm using Windows 10 and PHP 7.2.2 NTS.

Cannot navigate to invalid URL

This works for me:

$client = \Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient();
$crawler = $client->request('GET', '/login');

For this:

$client = \Symfony\Component\Panther\Client::createChromeClient();
$crawler = $client->request('GET', '/login'); 

I get the error:

Facebook\WebDriver\Exception\UnknownServerException : unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot navigate to invalid URL"}
  (Session info: headless chrome=68.0.3440.106)
  (Driver info: chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85),platform=Mac OS X 10.13.6 x86_64)
 /private/var/www/app/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:114
 /private/var/www/app/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:326
 /private/var/www/app/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:547
 /private/var/www/app/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:226
 /private/var/www/app/vendor/symfony/panther/src/Client.php:253
 /private/var/www/app/vendor/symfony/panther/src/Client.php:165
 /private/var/www/app/tests/Controller/ControllerTest.php:193

What is there wrong?

example for clicking on an alert box

I think it'd be useful to have an alert box triggered in an example with the brief code of how to accept / dismiss it:

$client->switchTo()->alert()->accept();

Use symfony/web-server-bundle instead of the WebServerManager homemade

By using symfony/web-server-bundle :

  • a part of #106 becomes obsolete because the router script support already exists in this package,
  • the same about #105 because this package auto discovers a correct port.

Ok, it is a bundle at the moment but not a component, but it is from the same Symfony family. Very probably, there will be new others needs which will be requested and already covered by this package. Why not use it now ?

Can't install with symfony 4.1

Your requirements could not be resolved to an installable set of packages.

Problem 1
- dunglas/panthere dev-master requires symfony/browser-kit ^4.0 -> satisfiable by symfony/browser-kit[v4.1.0].
- dunglas/panthere dev-master conflicts with symfony/browser-kit[v4.1.0].
- dunglas/panthere dev-master conflicts with symfony/browser-kit[v4.1.0].
- dunglas/panthere dev-master conflicts with symfony/browser-kit[v4.1.0].
- Installation request for dunglas/panthere dev-master -> satisfiable by dunglas/panthere[dev-master].

Why exactly was the conflict with browser-kit 4.1 added?

text() return an empty string

Hi,

when I use the crawler to filter the DOM and test the content of the title tag, I receive an empty string.

Tested with:

  • Debian 9.4 64bits
  • PHP 7.2.3
  • PHP standalone server
  • Symfony 4.0 basic skeleton
  • Chrome Version 65.0.3325.181 (Build officiel) (64 bits)
<?php
// tests/PanthereTest.php
declare(strict_types=1);

namespace App\Tests;

use Panthere\PanthereTestCase;

/**
 * Class PanthereTest
 * @package App\Tests
 */
class PanthereTest extends PanthereTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient();
        $crawler = $client->request('GET', static::$baseUri.'/');
        $this->assertEquals('Welcome!', $crawler->filterXPath('//title')->html());
        $this->assertEquals('Welcome!', $crawler->filterXPath('//title')->text());

        $client = static::createPanthereClient();
        $crawler = $client->request('GET', static::$baseUri.'/');
        $this->assertEquals('<title>Welcome!</title>', $crawler->filterXPath('//title')->html());
        $this->assertEquals('Welcome!', $crawler->filterXPath('//title')->text());
    }
}
// composer.json
{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "symfony/console": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/yaml": "^4.0"
    },
    "require-dev": {
        "dunglas/panthere": "^1.0@dev",
        "symfony/dotenv": "^4.0",
        "symfony/phpunit-bridge": "^4.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "id": "01C9W9DMK0BWP564TKPSF2P5F0",
            "allow-contrib": false
        }
    }
}

Result:

PHPUnit 6.5.7 by Seb
astian Bergmann and contributors.

Testing Project Test Suite
F                                                                   1 / 1 (100%)

Time: 4.76 seconds, Memory: 8.00MB

There was 1 failure:

1) App\Tests\PanthereTest::testSomething
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Welcome!'
+''

tests/PanthereTest.php:24

FAILURES!
Tests: 1, Assertions: 4, Failures: 1.

Use test_case

Is it possible to get the webserver with a test_case started? The page I want to test uses vue.js and the controller normaly requests an external API, which I would like to mock.

Mock client for unit testing

I'm using Panther to crawl and analyse other's websites. Is there an easy way to mock the Client and Crawler class for unit testing?

Thanks!

Rename PANTHER_NO_HEADLESS to PANTHER_HEADLESS

PANTHER_NO_HEADLESS env var gave me many headaches...

headless means "hidden", so "no headless" means "no hidden" , so "show". If in the code we use "! no headless", that means "no no hidden", so "yes hidden", so "show". Why not get rid of this double negation by simply renaming it PANTHER_HEADLESS ?

waitFor never find element

I'm using waitFor('.wai_hidden'), the page fail to load after a while
If I use wait(5) then $crawler->html(), there's an element with class=wai_hidden

Add the support of "router" script to WebServerManager

If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request.

http://php.net/manual/en/features.commandline.webserver.php

Basically, we could add a router param to createPantherClient() method.

protected static function createPantherClient(
    string $hostname = '127.0.0.1',
    int $port = 9000,
    string $router = null
): PantherClient

If you are interested, I can suggest a PR.

From Basic usage, not properly issue...

Hello,
I should open a webpage, wait for all the js to load (eg. in puppeteer 'waitUntil' => 'networkidle0') and then store the entire page source into a variable....

<?php

require __DIR__.'/vendor/autoload.php'; // Composer's autoloader

$client = \Symfony\Component\Panther\Client::createChromeClient();
$crawler = $client->request('GET', 'https://myurl.com/page.html'); 

// Wait for an element to be rendered
$client->waitFor('.support'); //indeed, I should wait for all the js executed on this page...to store the page source somewhere else....
....

how to do it here, please? Thank you

cannot find Chrome binary

Hy,

on a new installation, I'll testing the example request code on a laravel installation and will become, with the default configuration the following error:

unknown error: cannot find Chrome binary (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.4.0-131-generic x86_64)

The binaries are avalaible and executable. I've tested the chromium X64 binary for linux on the command line interface and can resolve the status from the server.

For the completenes here the code to test symfony/panther:

Route::get('test', function(){
    $client = \Symfony\Component\Panther\Client::createChromeClient();
    $crawler = $client->request('GET', 'http://api-platform.com'); // Yes, this website is 100% in JavaScript

    $link = $crawler->selectLink('Support')->link();
    $crawler = $client->click($link);

    // Wait for an element to be rendered
    $client->waitFor('.support');

    echo $crawler->filter('.support')->text();
    $client->takeScreenshot('screen.png');
});

Code coverage compliance?

Your project is very interesting. 👍

What about code coverage? Does it work like for a regular Symfony WebTestCase? If not, is that a panned thing or is it not feasible?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.