diff --git a/CHANGELOG.md b/CHANGELOG.md index 649e9bfe..286a560a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,12 @@ ## Unreleased +### Added +- Check if browser given to `run-tests` command is supported (this helps avoiding typos, interchange of browser and environment etc.). + ### Changed - The `logs/results.xml` could now be also accessed locally (the XSLT is now embedded right into the file, so we don't encounter same-origin policy problems). -- Use tagged version 0.6.0 of [php-webdriver](https://github.com/facebook/php-webdriver) +- Use tagged version 0.6.0 of [php-webdriver](https://github.com/facebook/php-webdriver). ### Fixed - Properly trigger PHPUnit colored (ANSI) mode when Steward itself is in ANSI mode. diff --git a/src-tests/Console/Command/RunTestsCommandTest.php b/src-tests/Console/Command/RunTestsCommandTest.php index b34dab3e..2445e0a4 100644 --- a/src-tests/Console/Command/RunTestsCommandTest.php +++ b/src-tests/Console/Command/RunTestsCommandTest.php @@ -105,6 +105,47 @@ public function directoryOptionsProvider() ]; } + /** + * @dataProvider browserNameProvider + * @param string $browserName + * @param bool $shouldThrowException + */ + public function testShouldThrowExceptionIfUnsupportedBrowserSelected($browserName, $shouldThrowException) + { + if ($shouldThrowException) { + $this->setExpectedException('\RuntimeException', 'Browser "' . $browserName . '" is not supported'); + } + + $seleniumAdapterMock = $this->getSeleniumAdapterMock(); + $this->command->setSeleniumAdapter($seleniumAdapterMock); + + $this->tester->execute( + ['command' => $this->command->getName(), 'environment' => 'prod', 'browser' => $browserName] + ); + + if (!$shouldThrowException) { + $output = $this->tester->getDisplay(); + $this->assertContains('Browser: ' . strtolower($browserName), $output); + $this->assertContains('No testcases found, exiting.', $output); + } + } + + /** + * @return array + */ + public function browserNameProvider() + { + return [ + // $browserName, $shouldThrowException + 'firefox is supported' => ['firefox', false], + 'chrome is supported' => ['chrome', false], + 'phantomjs is supported' => ['phantomjs', false], + 'browser name is case insensitive' => ['FIREFOX', false], + 'not supported browser' => ['mosaic', true], + 'unprintable character in browser name' => ['firefox​', true], + ]; + } + public function testShouldStopIfServerIsNotResponding() { $seleniumAdapterMock = $this->getMockBuilder(SeleniumServerAdapter::class) diff --git a/src/Console/Command/RunTestsCommand.php b/src/Console/Command/RunTestsCommand.php index f8c882c6..3d8c33d9 100644 --- a/src/Console/Command/RunTestsCommand.php +++ b/src/Console/Command/RunTestsCommand.php @@ -28,6 +28,14 @@ class RunTestsCommand extends Command protected $seleniumAdapter; /** @var ProcessSetCreator */ protected $processSetCreator; + /** @var array */ + protected $supportedBrowsers = [ + \WebDriverBrowserType::FIREFOX, + \WebDriverBrowserType::CHROME, + \WebDriverBrowserType::IE, + \WebDriverBrowserType::SAFARI, + \WebDriverBrowserType::PHANTOMJS, + ]; const ARGUMENT_ENVIRONMENT = 'environment'; const ARGUMENT_BROWSER = 'browser'; @@ -147,10 +155,32 @@ protected function initialize(InputInterface $input, OutputInterface $output) ) ); - $output->writeln(sprintf('Browser: %s', $input->getArgument(self::ARGUMENT_BROWSER))); + // If browser name or env is empty, ends initialization and let the Console/Command fail on input validation + if (empty($input->getArgument(self::ARGUMENT_BROWSER)) + || empty($input->getArgument(self::ARGUMENT_ENVIRONMENT)) + ) { + return; + } + + // Browser name is case insensitive, normalize it to lower case + $input->setArgument(self::ARGUMENT_BROWSER, strtolower($input->getArgument(self::ARGUMENT_BROWSER))); + $browser = $input->getArgument(self::ARGUMENT_BROWSER); + + // Check if browser is supported + if (!in_array($browser, $this->supportedBrowsers)) { + throw new \RuntimeException( + sprintf( + 'Browser "%s" is not supported (use one of: %s)', + $browser, + implode(', ', $this->supportedBrowsers) + ) + ); + } + + $output->writeln(sprintf('Browser: %s', $browser)); $output->writeln(sprintf('Environment: %s', $input->getArgument(self::ARGUMENT_ENVIRONMENT))); - // Tests directories exists + // Check if directories exists $this->testDirectories( $input, $output,