1

I have trouble setting up PHPUnit and Selenium testing environment on Windows 7 x64 machine. Apache/2.4.20 (Win64) OpenSSL/1.0.2h PHP/7.0.11.

My composer.json files looks like this (snipet):

"require": {
    "php": ">=5.6.0",
    "psr/http-message": "^1.0",
    "psr/cache": "^1.0",
    "zendframework/zend-diactoros": "^1.3",
    "phpunit/phpunit": "^5.6",
    "phpunit/phpunit-selenium": "^3.0"
},

I have the following components downloaded already:

all the files are stored in the folder D:\dev\selenium\.

Selenium server is started by issuin the following command C:\ProgramData\Oracle\Java\javapath\java.exe -Dwebdriver.chrome.driver="D:\dev\selenium\chromedriver.exe" -jar "D:\dev\selenium\selenium-server-standalone-3.0.1.jar" in command prompt.

I was searchring all day long on Google but found nothing; this is why I am asking you guys. When I'am trying to run tests by executing the command phpunit in project folder I get no result. Chrome opens up with the URL data:; and closes immadiately. Firefox is worst, it doesn't even opens. Standard PHPUnit tests are executin properly and I can see the results of those.

-- Edit --

Forgot to mention I am using Chrome version 55.0.2883.87 m x64. If somebody could suggest version combination all of these which actually works I would be really happy.

-- Edit 2 --

Java version: 8 update 111

Console output of Selenium server:

09:50:06.651 INFO - Selenium build info: version: '3.0.1', revision: '1969d75'
09:50:06.651 INFO - Launching a standalone Selenium Server
2017-01-09 09:50:06.682:INFO::main: Logging initialized @414ms
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
09:50:06.760 INFO - Driver provider org.openqa.selenium.safari.SafariDriver regi
stration is skipped: registration capabilities Capabilities [{browserName=safari, version=, platform
=MAC}] does not match the current platform VISTA
2017-01-09 09:50:06.807:INFO:osjs.Server:main: jetty-9.2.15.v20160210
2017-01-09 09:50:06.823:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@f5e5e3{/,null,AVAILABLE}
2017-01-09 09:50:06.994:INFO:osjs.ServerConnector:main: Started ServerConnector@c4039c{HTTP/1.1}{0.0.0.0:4444}
2017-01-09 09:50:06.994:INFO:osjs.Server:main: Started @739ms
09:50:06.994 INFO - Selenium Server is up and running

-- Edit 3 --

PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The best matching driver provider org.openqa.selenium.edge.EdgeDriver can't create a new driver instance for Capabilities [{browserName=*firefox}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: '****', ip: '10.10.146.251', os.name: 'Windows 7', os.arch: 'x86 (????)', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: unknown

Can not believe that nobody responded yet. Almost two days and I could not get closer to the solution. Anyhow I am currently considering Codeception as an alternative, it is a shame the Selenium does not have better support!

1 Answer 1

1

This is a known issue: https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666

Based on your current code, this is how your web test file should look. This works for Chromedriver 2.25, phpunit 5.6.3, phpunit-selenium 3.0.2 and the standalone selenium server 3.0.1 and assumes you are running tests locally (E.g NOT across selenium hubs on remote machines through jenkins etc)

I have added a couple of method overrides from Selenium2TestCase, one of which will fix the data:// error you get in your original question when running code coverage (prepareSession) and a useful helper function which captures screenshots on test failures (onNotSuccessfulTest)

Try using the following as your test class:

namespace Acceptance\Tests;

use PHPUnit_Extensions_Selenium2TestCase;

class BaseWebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    // Remove: not needed
    /*public static $browsers = [
        [
            'browserName' => 'chrome',
            'host'        => '127.0.0.1',
            'port'        => 4444,
            'sessionStrategy' => 'shared'
        ]
    ];*/

    /**
     * Make sure parent setUp is called
     */
    public function setUp()
    {
        parent::setUp(); 

        $this->setHost($host); // localhost
        $this->setPort((int)$port); // 4444
        $this->setBrowser($browser); // chrome
        $this->setBrowserUrl($application); // localhost/your_app (NOT just localhost)
        $this->prepareSession()->currentWindow()->size(array('width' => 1920, 'height' => 1080)); // maximise window area
    }

    /**
     * Ensure the session begins with a url that cookies can be set against.
     * Without this calling the tester with code coverage breaks.
     *
     * @see https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666
     *
     * @return object
     */
    public function prepareSession()
    {
        $session = parent::prepareSession();
        $this->url('/');

        return $session;
    }

    /**
     * Override this method from \PHPUnit_Framework_TestCase so we can capture a screenshot.
     *
     * @return void
     */
    public function onNotSuccessfulTest($exception)
    {
        $filedata   = $this->currentScreenshot();
        $file       = 'YOUR\SCREENSHOT\DIR\HERE\\' . basename(get_class($this)) . '.png';
        file_put_contents($file, $filedata);

        parent::onNotSuccessfulTest($exception);
    }

    public function testTitle()
    {
        $this->url('http://localhost/');
        $this->assertEquals('Virtual host localhost configured', $this->title());
    }
}

Make sure selenium is actually running by calling this in a separate window:

START java -Dwebdriver.chrome.driver=E:\path\to\chromedriver.exe -jar E:\path\to\selenium-server-standalone-3.0.1.jar
Sign up to request clarification or add additional context in comments.

16 Comments

I have something strange going on. Today I tried npmjs.com/package/webdriver-manager together with codeception.com/docs/modules/WebDriver and tried the PHP code you wrote, but still doesn't work. Anyhow I appreciate your effort, thank you @john-joseph
The code above is for phpunit-selenium, so it would only work when using that library.
Yes, you are right, but as of today neither seems to work. I am thinking setting up a virtual machine installing fresh Windows 7, Selenium, Composer and see if it works. I am not mixing the Codeception test files with the previous setup just to mention. /smile
OK, so now you have a different selenium error to the one specified in the question in that pastebin ... I am using Chromedriver 2.25 with selenium 3.0.1, and the latest phpunit-selenium. The code applied in my answer above works for those combinations, so try those (downgrade to chromedriver 2.25)
Answer updated again - I'd missed out some settings. Use that full test case in my answer, it should work now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.