Skip to content

Instantly share code, notes, and snippets.

@vertexvaar
Last active November 25, 2023 18:20
Show Gist options
  • Save vertexvaar/2183ba67b96a1d995256d506947ba40e to your computer and use it in GitHub Desktop.
Save vertexvaar/2183ba67b96a1d995256d506947ba40e to your computer and use it in GitHub Desktop.
vxvr.de parallel browser tests in Chrome and Firefox with co-stack/stack-test
<?php
declare(strict_types=1);
namespace App\Tests\Acceptance;
use CoStack\StackTest\BrowserTestCase;
use CoStack\StackTest\Factory\SessionFactory;
use CoStack\StackTest\Session;
use DateTimeImmutable;
use DateTimeZone;
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverBy;
class FrontendTest extends BrowserTestCase
{
protected Session $session;
protected function setUp(): void
{
parent::setUp();
$this->session = (new SessionFactory())->create();
}
public function testFrontendWorks(): void
{
$this->session->get('https://local.vxvr.de/');
self::assertPageContains($this->session, 'OLIVER EGLSEDER');
self::assertPageContains($this->session, 'DEVELOPER');
self::assertPageContains($this->session, '©2023 OLIVER EGLSEDER');
}
public function testStartMenu(): void
{
$this->session->get('https://local.vxvr.de/');
self::assertPageNotContains($this->session, 'Timestamp');
self::assertPageNotContains($this->session, 'RNGesus');
self::assertPageNotContains($this->session, 'One Time Secret');
self::assertPageNotContains($this->session, 'Link utils');
self::assertPageNotContains($this->session, 'Base 64');
$this->session->click(WebDriverBy::id('start-menu'));
self::assertPageContains($this->session, 'Timestamp');
self::assertPageContains($this->session, 'RNGesus');
self::assertPageContains($this->session, 'One Time Secret');
self::assertPageContains($this->session, 'Link utils');
self::assertPageContains($this->session, 'Base 64');
}
/**
* @depends testStartMenu
*/
public function testTimestampWidget(): void
{
$this->session->get('https://local.vxvr.de/');
self::assertPageNotContains($this->session, 'Unixtimestamp');
$this->session->click(WebDriverBy::id('start-menu'));
$this->session->click(WebDriverBy::id('start-menu-modal-timestamp'));
self::assertPageContains($this->session, 'Unixtimestamp');
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$timestamp = $now->format('U');
$this->session->fillField(WebDriverBy::id('widget-timestamp-input'), $timestamp);
$this->session->click(WebDriverBy::cssSelector('[data-action="timestamp#convert"]'));
$utcFormat = $now->format('d.m.Y, H:i:s');
$utcOutputSelector = WebDriverBy::id('widget-timestamp-output-utc');
self::assertElementContains($this->session, $utcFormat, $utcOutputSelector);
$localNow = $now->setTimezone(new DateTimeZone('Europe/Berlin'));
$localFormat = $localNow->format('d.m.Y, H:i:s');
$localOutputSelector = WebDriverBy::id('widget-timestamp-output-europeberlin');
self::assertElementContains($this->session, $localFormat, $localOutputSelector);
}
/**
* @depends testStartMenu
*/
public function testRNGesusWidget(): void
{
$this->session->get('https://local.vxvr.de/');
self::assertPageNotContains($this->session, 'RNGesus');
$this->session->click(WebDriverBy::id('start-menu'));
$this->session->click(WebDriverBy::id('start-menu-modal-rngesus'));
self::assertPageContains($this->session, 'RNGesus');
$this->session->fillField(WebDriverBy::id('widget-timestamp-input-min'), '420691337');
$this->session->fillField(WebDriverBy::id('widget-timestamp-input-max'), '420691337');
$this->session->click(WebDriverBy::cssSelector('[data-action="rngesus#generate"]'));
self::assertElementEquals($this->session, '420691337', WebDriverBy::id('widget-timestamp-output-value'));
}
/**
* @depends testStartMenu
*/
public function testOneTimeSecret(): void
{
$this->session->get('https://local.vxvr.de/');
self::assertPageNotContains($this->session, 'One Time Secret');
$this->session->click(WebDriverBy::id('start-menu'));
$this->session->click(WebDriverBy::id('start-menu-modal-ots'));
self::assertPageContains($this->session, 'One Time Secret');
$this->session->click(WebDriverBy::cssSelector('[data-action="ots#enableStore"]'));
self::assertPageContains($this->session, 'Your secret data');
$this->session->fillField(WebDriverBy::id('ots-data'), 'Secret test text string');
$this->session->fillField(WebDriverBy::id('ots-password'), 'Secret password');
$this->session->click(WebDriverBy::cssSelector('[data-action="ots#store"]'));
$this->session->waitUntil(static function (WebDriver $driver): bool {
$element = $driver->findElement(WebDriverBy::id('ots-id'));
$value = $element->getAttribute('value');
return trim($value) !== '';
});
$tokenPerBrowser = [];
$elements = $this->session->findElements(WebDriverBy::id('ots-id'));
foreach ($elements->getElementsPerDriver() as $browserName => $elements) {
$tokenPerBrowser[$browserName] = $elements[0]->getAttribute('value');
}
$this->session->click(WebDriverBy::cssSelector('[data-action="ots#reset"]'));
$this->session->click(WebDriverBy::cssSelector('[data-action="ots#enableReceive"]'));
foreach ($this->session->drivers as $browserName => $driver) {
$secretToken = $tokenPerBrowser[$browserName];
$driver->findElement(WebDriverBy::id('ots-receive-secret'))->sendKeys($secretToken);
}
self::assertElementEquals($this->session, '', WebDriverBy::id('ots-received-data'));
$this->session->click(WebDriverBy::cssSelector('[data-ots-target="recSecretButton"]'));
$this->session->waitUntil(static function(WebDriver $driver): bool {
$element = $driver->findElement(WebDriverBy::id('ots-received-data'));
$value = $element->getAttribute('value');
return trim($value) !== '';
});
self::assertElementEquals($this->session, 'Secret test text string', WebDriverBy::id('ots-received-data'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment