使用chromedriver

时间:2017-07-02 14:41:16

标签: selenium selenium-webdriver laravel-5 phpunit ubuntu-16.04

我在Ubuntu 16.04上安装了selenium 3.01,chromedriver 2.27和chrome 59.

一切都在我的本地机器上运行(运行gnome桌面),但不适用于测试机器(没有任何GUI)。

我使用下一个命令启动了Selenium(我在这台机器上没有GUI):

xvfb-run java -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone-3.0.1.jar -debug

我用telnet检查它是从端口4444开始的:

root@xxx:~# telnet localhost 4444
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

f
HTTP/1.1 400 No URI
Content-Length: 0
Connection: close
Server: Jetty(9.2.15.v20160210)

Connection closed by foreign host.

但是当用phpunit运行测试时,它们会失败:

root@xxx:/var/www/dev.xxx.tt-laravel/site# "vendor/phpunit/phpunit/phpunit" tests/General/NotAuth.php --verbose
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.18-0ubuntu0.16.04.1
Configuration: /var/www/dev.domain-laravel/site/phpunit.xml

SS                                                                  2 / 2 (100%)

Time: 2 minutes, Memory: 12.00MB

There were 2 skipped tests:

1) NotAuth::test_shops_index_load
The Selenium Server is not active on host localhost at port 4444.

/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:299
/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:337
/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:314

2) NotAuth::test_shops_categories_load
The Selenium Server is not active on host localhost at port 4444.

/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:299
/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:337
/var/www/dev.domain-laravel/site/vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:314

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Skipped: 2.

和selenium选项卡的输出:

2017-07-02 17:18:00.738:INFO::main: Logging initialized @497ms
2017-07-02 17:18:00.870:INFO:osjs.Server:main: jetty-9.2.15.v20160210
2017-07-02 17:18:00.917:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@5158b42f{/,null,AVAILABLE}
2017-07-02 17:18:00.938:INFO:osjs.ServerConnector:main: Started ServerConnector@6771beb3{HTTP/1.1}{0.0.0.0:4444}
2017-07-02 17:18:00.939:INFO:osjs.Server:main: Started @698ms
Starting ChromeDriver 2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320) on port 6589
Only local connections are allowed.
Starting ChromeDriver 2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320) on port 16531
Only local connections are allowed.

感谢任何帮助。谢谢!

PS:这是其中一项测试:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Modelizer\Selenium\SeleniumTestCase;

// https://github.com/Modelizer/Laravel-Selenium
// https://github.com/Modelizer/Laravel-Selenium/wiki/APIs

require_once(__DIR__.'/../../vendor/hacks/load_env.php');

class NotAuth extends SeleniumTestCase
{

    /**
     * SHOPS MODULES START
     */

    public function test_shops_index_load()
    {
        $url = trim(route('shops_index', [], FALSE));
        $this->visit($url)
            ->see('Top Shops')
            ->see('Help Center3')
            ->seePageIs($url)
        ;
    }

    public function test_shops_categories_load()
    {
        $url = trim(route('shops_categories', [], FALSE));
        $this->visit($url)
            ->see('Automotive')
            ->see('Baby')
            ->seePageIs($url)
        ;
    }

    /**
     * SHOPS MODULES END
     */
}

1 个答案:

答案 0 :(得分:1)

在过去的两个奇数月里,我一直有同样的问题,并且已就此进行了大量的调查。

我注意到在我的环境中本地整个过程都有效,但构建服务器上完全相同的设置失败了......

我最终注意到在本地,我正在运行并安装一切作为流浪者用户。然而,在构建服务器上(Ubuntu 16.04.2 LTS),我以root身份完成所有操作,并且由于系统由Puppet维护,因此无法手动向PATH添加项目。

注意到,我将所有必需的权限更改为Jenkins(ci最终将运行测试)。

我还全球安装了phpunit&amp; phpunit-selenium以及所有必需工具的特定版本。

  • 的Xvfb
  • Google-Chrome - V59.0.3071.115
  • FireFox - V54.0
  • Chromedriver - V2.30
  • GeckoDriver - V0.16.1
  • Selenium Standalone Server - V3.4.0
  • PHPUnit - V5.7.21
  • PHPUit-Selenium - V3.0.3

我遵循的安装: 爪哇:

apt-get -qq install default-jre

谷歌铬:

apt-get -qq install libxss1 libappindicator1 libindicator7
wget http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_59.0.3071.115-1_amd64.deb
dpkg -i google-chrome*.deb
apt-get -qq install -f

火狐:

apt-get -qq update
apt-get -qq install firefox

的Xvfb:

apt-get -qq install xvfb

Google Chrome驱动程序。 (V2.30):

cd ~/
mkdir browser-drivers
cd browser-drivers
wget https://chromedriver.storage.googleapis.com/2.30/chromedriver_linux64.zip
unzip chromedriver*.zip
chmod a+x chromedriver
chmod 775 chromedriver
chown jenkins:jenkins chromedriver

Firefox Gecko Driver。 (V0.16.1):

cd ~/browser-drivers
wget -N https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-linux64.tar.gz
tar -xvzf geckodriver*
chmod a+x geckodriver
chmod 775 geckodriver
chown jenkins:jenkins geckodriver

Selenium Standalone:

cd ~/
mkdir selenium
cd selenium
wget -N http://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
chmod a+x selenium*
chown jenkins:jenkins selenium*

PhpUnit(V5.7.21):

curl -Lo /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-5.7.21.phar
chmod +x /usr/local/bin/phpunit
chown -R jenkins:jenkins /usr/local/bin/phpunit
# Run from "/var/lib/jenkins" as jenkins user
cd /var/lib/jenkins
su jenkins
composer global require 'phpunit/phpunit=5.*'
composer global require 'phpunit/phpunit-selenium=3.0.3'

然后我还创建了两个脚本来启动xvfb和amp;每次测试后都要硒。

seleniumxvfbup.sh

#!/bin/bash

Xvfb :8 -screen 8 1920x1080x24 -ac +extension GLX +render -noreset > /dev/null 2>&1 &

export DISPLAY=:8.8

java -Dwebdriver.gecko.driver=browser-drivers/geckodriver -Dchrome.binary=/opt/google/chrome/chrome -Dwebdriver.chrome.driver=browser-drivers/chromedriver -jar selenium/selenium-server-standalone-3.4.0.jar -port 4444 > /dev/null 2>&1 &

以这种方式启动selenium服务器允许我对chrome或firefox进行单独测试,而不必每次都停止/启动selenium。

杀死服务就像

一样简单
pkill -f selenium & pkill -f Xvfb &

最后

chmod +x ~/seleniumxvfbup.sh
chown jenkins:jenkins seleniumxvfbup.sh

确保所有文件都不在/ root / dir中,因为这是系统失败的主要问题。

希望这可以帮助你...