LoginSignup
1

More than 5 years have passed since last update.

E2E testをheroku ciで実施する

Last updated at Posted at 2018-04-02

heroku ciでe2eテストを実施

構成

  • heroku ci
  • php7
  • phpunit
  • facebook/webdriver
  • chromedriver

Setting

  • heroku のbuildpackで提供されるgoogle chromeはbinaryの場所が環境変数GOOGLE_CHROME_SHIMで定義されているディレクトリにあるのでwebdriverにsetする必要がある
  • seleniumで入力などのオペレーションさせたい場合はheroku-buildpack-xvfb-google-chromeを使う
  • heroku-buildpack-xvfb-google-chromeを使う場合はheroku-16のstackでは動かないcedar-14を使う

Heroku settings

composer.json
{
  "name": "e2e",
  "description": "This app does one little thing",
  "stack": "cedar-14",
  "env": {
    "GOOGLE_CHROME_CHANNEL":"stable"
  },
  "environments": {
    "stack": "cedar-14",
    "test": {
      "buildpacks": [
        { "url": "heroku/php" },
        { "url": "https://github.com/heroku/heroku-buildpack-chromedriver" },
        { "url": "https://github.com/heroku/heroku-buildpack-xvfb-google-chrome" }
      ],
      "scripts": {
        "test": "phpunit tests"
      }
    }
  }
}

Test example

E2eTest.php
<?php
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Chrome\ChromeDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
class E2eTest extends TestCase
{

    protected $driver;

    protected function setUp()
    {
        putenv('webdriver.chrome.driver=/app/.chromedriver/bin/chromedriver');

        $options = new ChromeOptions();

        $chromeBinary = getenv("GOOGLE_CHROME_SHIM");

        $options->setBinary($chromeBinary);

        $capabilities = DesiredCapabilities::chrome();

        $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

        $this->driver = ChromeDriver::start($capabilities);
    }

    /**
     * @large
     */
    public function testTitle()
    {
        $this->driver->get('https://www.google.co.jp/');
        $title = $this->driver->getTitle();
        $this->assertEquals($title, 'Google');
    }
}
?>
composer.json
{
  "require": {
    "php": ">=7.1.0",
    "facebook/webdriver": "^1.5"
  },
  "require-dev": {
      "phpunit/phpunit": ">=7.0"
  }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1