Skip to content

Commit

Permalink
Merge pull request #7345 from connorshea/improve-chrome-webdriver-rob…
Browse files Browse the repository at this point in the history
…o-task

Get ChromeDriver's latest release in Robo task.
  • Loading branch information
Dillon-Brown authored Jul 4, 2019
2 parents ae97a5b + d876bfa commit a0d5f5a
Showing 1 changed file with 67 additions and 32 deletions.
99 changes: 67 additions & 32 deletions lib/Robo/Plugin/Commands/TestEnvironmentCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function configureTests(
} elseif ($os->isOsLinux()) {
$this->say('Linux detected');
$this->installUnixEnvironmentVariables($opts);
} elseif ($os->isOsMacOsX()) {
$this->say('Mac OS X detected');
} elseif ($os->isOsMacOSX()) {
$this->say('macOS detected');
$this->installUnixEnvironmentVariables($opts);
} elseif ($os->isOsBSD()) {
$this->say('BSD detected');
Expand All @@ -117,34 +117,64 @@ public function configureTests(
}

/**
* Download and run the chrome web driver
* Download and install ChromeDriver.
* @command chromedriver:install
* @param array $opts
* @option bool $reinstall Forces the Chrome WebDriver executable to be reinstalled, can be used to get a newer version.
* @usage chromedriver:install --reinstall
*/
public function driverRunChrome($opts = ['url_base' => '/wd/hub'])
public function chromeDriverInstall($opts = ['reinstall' => false])
{
$this->say('Driver Run Chrome');
$this->say('Installing ChromeDriver...');
$os = new OperatingSystem();
$paths = new Paths();
$url = $this->getChromeWebDriverUrl();
$basePath = $os->toOsPath($paths->getProjectPath().'/build/tmp/');
$basePath = $os->toOsPath($paths->getProjectPath() . '/build/tmp');

if (!file_exists($basePath)) {
if (mkdir($basePath, 0777, true) === false) {
throw new \RuntimeException('Unable to create file structure ' . $basePath);
}
} elseif ($opts['reinstall']) {
$this->_deleteDir($basePath);
if (mkdir($basePath, 0777, true) === false) {
throw new \RuntimeException('Unable to create file structure ' . $basePath);
throw new \RuntimeException('Unable to create file structure ' . $basePath);
}
}

$zipPath = $basePath . DIRECTORY_SEPARATOR . 'webdriver.zip';
$unzippedPath = $basePath . DIRECTORY_SEPARATOR . 'webdriver';

if (!file_exists($unzippedPath)) {
$this->say('Downloading ChromeDriver.');
$this->download($url, $zipPath);
$this->unzip($zipPath, $unzippedPath);
$this->say('ChromeDriver install completed.');
} else {
$this->say('ChromeDriver has already been downloaded.');
}
}

$this->runChromeWebDriver($unzippedPath, $opts['url_base']);
/**
* Run ChromeDriver.
* @command chromedriver:run
* @param array $opts
* @option string $url_base The base URL from which the WebDriver will be run.
*/
public function chromeDriverRun($opts = ['url_base' => '/wd/hub'])
{
$this->say('Running ChromeDriver...');
$os = new OperatingSystem();
$paths = new Paths();
$basePath = $os->toOsPath($paths->getProjectPath() . '/build/tmp/');

$unzippedPath = $basePath . DIRECTORY_SEPARATOR . 'webdriver';

$this->say('Driver Run Chrome Completed');
if (!file_exists($unzippedPath)) {
throw new \RuntimeException('ChromeDriver is not installed in ' . $unzippedPath);
}

$this->runChromeWebDriver($unzippedPath, $opts['url_base']);
}

/**
Expand Down Expand Up @@ -172,8 +202,8 @@ public function fakeTravis(
} elseif ($os->isOsLinux()) {
$this->say('Linux detected');
$this->installUnixEnvironmentVariables($opts);
} elseif ($os->isOsMacOsX()) {
$this->say('Mac OS X detected');
} elseif ($os->isOsMacOSX()) {
$this->say('macOS detected');
$this->installUnixEnvironmentVariables($opts);
} elseif ($os->isOsBSD()) {
$this->say('BSD detected');
Expand All @@ -182,9 +212,9 @@ public function fakeTravis(
$this->say('Solaris detected');
$this->installUnixEnvironmentVariables($opts);
} elseif ($os->isOsUnknown()) {
throw new \DomainException('Unknown Operating system');
throw new \DomainException('Unknown operating system');
} else {
throw new \DomainException('Unable to detect Operating system');
throw new \DomainException('Unable to detect operating system');
}

$this->say('Fake Travis Environment Complete');
Expand Down Expand Up @@ -318,31 +348,34 @@ private function toUnixEnvironmentVariables(array $opts)


/**
* @param string $version
* Gets the URL for installing the latest version of ChromeDriver.
* @return string url
*/
private function getChromeWebDriverUrl($version = '2.38')
private function getChromeWebDriverUrl()
{
$os = new OperatingSystem();
$latestRelease = file_get_contents('https://chromedriver.storage.googleapis.com/LATEST_RELEASE', false);

if ($os->isOsWindows()) {
$this->say('Windows detected');
return 'https://chromedriver.storage.googleapis.com/' . $version . '/chromedriver_win32.zip';
return 'https://chromedriver.storage.googleapis.com/' . $latestRelease . '/chromedriver_win32.zip';
} elseif ($os->isOsLinux()) {
$this->say('Linux detected');
return 'https://chromedriver.storage.googleapis.com/' . $version . '/chromedriver_linux64.zip';
} elseif ($os->isOsMacOsX()) {
$this->say('Mac OS X detected');
return 'https://chromedriver.storage.googleapis.com/' . $version . '/chromedriver_mac64.zip';
return 'https://chromedriver.storage.googleapis.com/' . $latestRelease . '/chromedriver_linux64.zip';
} elseif ($os->isOsMacOSX()) {
$this->say('macOS detected');
return 'https://chromedriver.storage.googleapis.com/' . $latestRelease . '/chromedriver_mac64.zip';
} elseif ($os->isOsBSD()) {
$this->say('BSD detected');
throw new \DomainException('Unsupported Operating system');
throw new \DomainException('Unsupported operating system');
} elseif ($os->isOsSolaris()) {
$this->say('Solaris detected');
throw new \DomainException('Unsupported Operating system');
throw new \DomainException('Unsupported operating system');
} elseif ($os->isOsUnknown()) {
throw new \DomainException('Unknown Operating system');
throw new \DomainException('Unknown operating system');
} else {
throw new \DomainException('Unable to detect operating system');
}
throw new \DomainException('Unable to detect Operating system');
}

/**
Expand All @@ -367,6 +400,7 @@ private function download($url, $toPath)
*/
private function unzip($zipPath, $unzippedPath)
{
$this->say("Unzipping {$zipPath}.");
$zip = new \ZipArchive();
$res = $zip->open($zipPath);
if ($res === true) {
Expand Down Expand Up @@ -395,29 +429,30 @@ private function runChromeWebDriver($basePath, $urlBase = '/wd/hub')
. DIRECTORY_SEPARATOR
. 'chromedriver';
chmod($binPath, 100);
} elseif ($os->isOsMacOsX()) {
} elseif ($os->isOsMacOSX()) {
$this->say('macOS detected');
$binPath = $basePath
. DIRECTORY_SEPARATOR
. 'chromedriver';
chmod($binPath, 100);
} elseif ($os->isOsBSD()) {
$this->say('BSD detected');
throw new \DomainException('Unsupported Operating system');
throw new \DomainException('Unsupported operating system');
} elseif ($os->isOsSolaris()) {
$this->say('Solaris detected');
throw new \DomainException('Unsupported Operating system');
throw new \DomainException('Unsupported operating system');
} elseif ($os->isOsUnknown()) {
throw new \DomainException('Unknown Operating system');
throw new \DomainException('Unknown operating system');
} else {
throw new \DomainException('Unable to detect Operating system');
throw new \DomainException('Unable to detect operating system');
}

if (!file_exists($binPath)) {
throw new \RuntimeException('Unable to find chrome driver ' . $binPath);
throw new \RuntimeException('Unable to find ChromeDriver ' . $binPath);
}

$this->say('Hint: open terminal and run `'.$os->toOsPath('./vendor/bin/codecept').'run [test suite] --env chrome-driver`');
$this->say('Starting Chrome Driver');
$this->say('Hint: open terminal and run `'.$os->toOsPath('./vendor/bin/codecept').' run [test suite] --env chrome-driver`');
$this->say('Starting ChromeDriver');
$this->_exec(
$binPath
. ' --url-base='
Expand Down

0 comments on commit a0d5f5a

Please sign in to comment.