Skip to content

Commit

Permalink
Merge pull request #2 from jbcr/main
Browse files Browse the repository at this point in the history
close session and set test state
  • Loading branch information
macintoshplus authored Jun 4, 2021
2 parents 71d971e + 063ae45 commit 3cfa6bd
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/composer.lock
/vendor
/.php_cs.cache
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Version 1.2.0

* Fix Lambdatest ending session.
* Before stop the session, define the test execution status on Lambdatest

# Version 1.1.0

* Use the Facebook web driver instead of Selenium2 web driver.
* Before launch, throw an exception if no concurrency automation test is available.


# Version 1.0.0

* Allow configuring Lambdatest credential with configuration or environment variables.
5 changes: 4 additions & 1 deletion src/Driver/LambdatestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function buildDriver(array $config)
throw new TooManyParallelExecutionException(sprintf('Unable to launch anothe parallel automation test. max concurency: %s, current running test: %s', $data['data']['max_concurrency'], $data['data']['running']));
}

return parent::buildDriver($config);
$def = parent::buildDriver($config);
$def->setClass(LambdatestWebDriver::class);

return $def;
}
}
41 changes: 41 additions & 0 deletions src/Driver/LambdatestWebDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Macintoshplus\Lambdatest\Driver;

use Behat\Mink\Exception\DriverException;
use Facebook\WebDriver\Remote\RemoteWebDriver;

/*
* Override the WebDriver to add quit call on session stop.
* Use the LambdatestWebDriver allow to set the test result on lambdatest.
*/
final class LambdatestWebDriver extends \SilverStripe\MinkFacebookWebDriver\FacebookWebDriver
{
protected $webDriver;

private $started = false;

public function setWebDriver(RemoteWebDriver $webDriver)
{
$this->webDriver = $webDriver;

return parent::setWebDriver($webDriver);
}

public function stop()
{
if ($this->isStarted() === false) {
return;
}

parent::stop();

try {
$this->webDriver->quit();
} catch (Exception $e) {
throw new DriverException('Could not quit connection', 0, $e);
}
}
}
11 changes: 11 additions & 0 deletions src/LambdatestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Macintoshplus\Lambdatest;

use Behat\MinkExtension\ServiceContainer\MinkExtension;
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class LambdatestExtension implements ExtensionInterface
{
Expand Down Expand Up @@ -41,6 +44,14 @@ public function configure(ArrayNodeDefinition $builder)
*/
public function load(ContainerBuilder $container, array $config)
{
//SessionStateListener
$definition = new Definition('Macintoshplus\Lambdatest\Listener\SessionStateListener', [
new Reference(MinkExtension::MINK_ID),
'%mink.javascript_session%',
'%mink.available_javascript_sessions%',
]);
$definition->addTag(EventDispatcherExtension::SUBSCRIBER_TAG, ['priority' => 0]);
$container->setDefinition('mink.lambdatest.listener.sessions', $definition);
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Listener/SessionStateListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Macintoshplus\Lambdatest\Listener;

use Behat\Mink\Mink;
use Behat\Testwork\EventDispatcher\Event\AfterExerciseCompleted;
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
use Macintoshplus\Lambdatest\Driver\LambdatestWebDriver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class SessionStateListener implements EventSubscriberInterface
{
/**
* @var Mink
*/
private $mink;

/** @var string|null */
private $javascriptSession;

/** @var string[] */
private $javascriptsSessions;

/**
* SessionStateListener constructor.
*
* @param string|null $javascriptSession
* @param string[] $javascriptsSessions
*/
public function __construct(Mink $mink, $javascriptSession, array $javascriptsSessions = [])
{
$this->mink = $mink;
$this->javascriptSession = $javascriptSession;
$this->javascriptsSessions = $javascriptsSessions;
}

public static function getSubscribedEvents()
{
return [
ExerciseCompleted::AFTER => ['tearDownMinkSessions', 255],
];
}

/*
* Define the session test state for all lambdatest session
*/
public function tearDownMinkSessions(AfterExerciseCompleted $event)
{
foreach ($this->javascriptsSessions as $sessionName) {
if ($this->mink->hasSession($sessionName) === false) {
continue;
}
$driver = $this->mink->getSession($sessionName)->getDriver();
if ($driver instanceof LambdatestWebDriver) {
$driver->executeScript('lambda-status='.($event->getTestResult()->isPassed() ? 'passed' : 'failed'));
}
}
}
}

0 comments on commit 3cfa6bd

Please sign in to comment.