Selenium2与PhantomJS和PHPUnit - 会话/ cookie问题

时间:2015-08-31 00:52:14

标签: selenium cookies phpunit phantomjs session-cookies

我已将当前项目设置为与Selenium和PhantomJS一起运行。 (与http://www.nuanced.it/2015/05/using-phantomjs-with-phpunit.html相似的设置)

但是我继续收到以下错误

{"errorMessage":"Can only set Cookies for the current domain","request":{"headers":{"Accept":"application/json;charset=UTF-8","Content-Length":"135","Content-type":"application/json;charset=UTF-8","Host":"127.0.0.1:8080"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\":{\"name\":\"PHPUNIT_SELENIUM_TEST_ID\",\"value\":\"PepperLeaf\\\\WebBundle\\\\Tests\\\\MyProject\\\\TestFirstTest__testTitle\",\"secure\":false}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/0d022670-4f79-11e5-abbe-01f06bc40b42/cookie"}}

PhantomJS的输出

[INFO  - 2015-08-31T00:41:02.249Z] HUB Register - register - Registered with grid hub: http://127.0.0.1:4444/ (ok)
[INFO  - 2015-08-31T00:41:43.258Z] Session [0d022670-4f79-11e5-abbe-01f06bc40b42] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2015-08-31T00:41:43.258Z] Session [0d022670-4f79-11e5-abbe-01f06bc40b42] - page.customHeaders:  - {}
[INFO  - 2015-08-31T00:41:43.258Z] Session [0d022670-4f79-11e5-abbe-01f06bc40b42] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.0.0","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"mac-10.9 (Mavericks)-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2015-08-31T00:41:43.258Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 0d022670-4f79-11e5-abbe-01f06bc40b42

我在PHPUnit上运行的代码

class TestFirstTest extends \PHPUnit_Extensions_Selenium2TestCase {

    /**
     * {@inheritDoc}
     */
    protected function setUp()
    {
        $this->setBrowser('phantomjs');
        $this->setHost("127.0.0.1");
        $this->setPort(8080);
        $this->setBrowserUrl('http://www.mywebsite.dev/');
    }

    public function testTitle()
    {
        $this->url('/');
        $this->assertTitle('Example WWW Page');
    }
}

免责声明:我对前端测试很陌生,这是我第一次尝试这一点。我很困惑为什么会这样,因为我没有设置或创建任何cookie /会话。

1 个答案:

答案 0 :(得分:0)

我不确定是什么导致了这个问题。但是,这是我的symfony / phpunit.xml(这是导致问题的原因)

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
        backupGlobals               = "false"
        backupStaticAttributes      = "false"
        colors                      = "false"
        convertErrorsToExceptions   = "true"
        convertNoticesToExceptions  = "true"
        convertWarningsToExceptions = "true"
        processIsolation            = "false"
        stopOnFailure               = "false"
        syntaxCheck                 = "false"
        bootstrap                   = "../app/bootstrap.php.cache" >

    <testsuites>
        <testsuite name="PepperLeaf">
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>

    <php>
        <server name="KERNEL_DIR" value="app/" />
        <ini name="memory_limit" value="2048M" />
        <ini name="xdebug.max_nesting_level" value="500" />
    </php>

    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">../src</directory>
            <exclude>
                <directory>../src/*/*Bundle/Resources</directory>
                <directory>../src/*/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/Resources</directory>
                <directory>../src/*/Bundle/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/FrontTests</directory>
                <directory>../src/*/Bundle/*Bundle/DataFixtures</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="coverage" />
        <log type="coverage-xml" target="coverage/xml" />
        <log type="coverage-clover" target="logs/clover.xml" />
        <log type="coverage-crap4j" target="logs/crap4j.xml" />
        <log type="junit" target="logs/junit.xml" logIncompleteSkipped="false" />
    </logging>
</phpunit>

这是更新的(工作的phpunit.xml脚本)。我不确定究竟是什么导致了上述问题。

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit>

    <testsuites>
        <testsuite name="PepperLeaf_Frontend">
            <directory>../src/*/*Bundle/FrontTests</directory>
        </testsuite>
    </testsuites>

    <php>
        <server name="KERNEL_DIR" value="app/" />
        <ini name="memory_limit" value="2048M" />
        <ini name="xdebug.max_nesting_level" value="500" />
    </php>

    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">../src</directory>
            <exclude>
                <directory>../src/*/*Bundle/Resources</directory>
                <directory>../src/*/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/Resources</directory>
                <directory>../src/*/Bundle/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/DataFixtures</directory>
                <directory>../src/*/*Bundle/FrontTests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>