Skip to content

Commit

Permalink
add headless-browser testing of UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmail committed Jun 16, 2024
1 parent 9e42c47 commit 03d91e7
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Web-based OpenStreetMap Editor",
"type": "project",
"require": {
"jbelien/oauth2-openstreetmap": "^0.1.2"
"jbelien/oauth2-openstreetmap": "^0.1.2",
"php-webdriver/webdriver": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^11.2"
Expand Down
209 changes: 208 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions test/PageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
require 'vendor/autoload.php';

// https://php-webdriver.github.io/php-webdriver/latest/Facebook/WebDriver.html
// https://gist.github.com/aczietlow/7c4834f79a7afd920d8f

use Facebook\WebDriver\Chrome\ChromeDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverKeys;
use Facebook\WebDriver\WebDriverExpectedCondition;
use PHPUnit\Framework\TestCase;

require_once 'WebServerHelper.php';


class WebPageTest extends TestCase
{
protected $webDriver;

// Runs once
public static function setUpBeforeClass(): void
{
WebServerHelper::start();
}

public static function tearDownAfterClass(): void
{
WebServerHelper::stop();
}

// Runs before each test
public function setUp(): void
{
// https://github.com/php-webdriver/php-webdriver/wiki/Chrome#start-chromedriver
// putenv('WEBDRIVER_CHROME_DRIVER=/path/to/chromedriver');

// https://github.com/php-webdriver/php-webdriver/wiki/Chrome#general-usage
$chromeOptions = new ChromeOptions();
// $chromeOptions->setBinary('/home/user/Downloads/my_chrome_binary');
$chromeOptions->addArguments(['--headless']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);

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

public function tearDown(): void
{
$this->webDriver->quit();
}

private function elementByCSS($query)
{
return $this->webDriver->findElement(WebDriverBy::cssSelector($query));
}

public function testPage()
{
$this->webDriver->get(WebServerHelper::url());

sleep(2);
// Wait for map (Javacript) to load
$this->webDriver->wait()->until(
WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector('.leaflet-map-pane'))
);


$h2 = $this->elementByCSS('h2');
$this->assertEquals($h2->getText(), 'Level0 OpenStreetMap Editor');


$input = $this->elementByCSS('input[name="url"]');
$input->sendKeys(array('w105957600', WebDriverKeys::ENTER));

sleep(2);

$textarea = $this->elementByCSS('textarea');
$this->assertStringContainsString("way 105957600\n", $textarea->getText());
$this->assertStringContainsString(" addr:street = Downing Street\n", $textarea->getText());


// Button "Add to Editor"
$input = $this->elementByCSS('input[name="url"]');
$input->sendKeys('n3815077900');
$this->elementByCSS('input[type="submit"][name="add"]')->click();
sleep(2);
$textarea = $this->elementByCSS('textarea');
$this->assertStringContainsString("way 105957600\n", $textarea->getText());
$this->assertStringContainsString("node 3815077900: ", $textarea->getText());


// Button "Check for conflicts"
$this->elementByCSS('input[name="check"]')->click();
$this->assertStringContainsString('Nothing is modified', $this->webDriver->getPageSource());


// Button "Show osmChange"
$this->elementByCSS('input[name="showosc"]')->click();
$this->assertStringContainsString('this will be uploaded to the server', $this->webDriver->getPageSource());
$this->assertMatchesRegularExpression('/&lt;osmChange [^&]+&gt;\s*&lt;\/osmChange&gt;/', $this->webDriver->getPageSource());
}
}
40 changes: 40 additions & 0 deletions test/WebServerHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

class WebServerHelper
{
private static $pid;
private static $host = 'localhost';
private static $port = 8000;
private static $docRoot = __DIR__ . '/../www';

public static function url()
{
return 'http://' . self::$host . ':' . self::$port;
}

public static function start()
{
$command = sprintf(
'php -S %s:%d -t %s > /dev/null 2>&1 & echo $!',
self::$host,
self::$port,
escapeshellarg(self::$docRoot)
);

$output = [];
exec($command, $output);
self::$pid = (int) $output[0];

file_put_contents(__DIR__ . '/server.pid', self::$pid);
sleep(1);
}

public static function stop()
{
$pid = (int) @file_get_contents(__DIR__ . '/server.pid');
if ($pid) {
exec('kill ' . $pid);
unlink(__DIR__ . '/server.pid');
}
}
}

0 comments on commit 03d91e7

Please sign in to comment.