Skip to content

Instantly share code, notes, and snippets.

@colincalnan
Forked from jhedstrom/FeatureContext.php
Created May 17, 2017 22:30
Show Gist options
  • Save colincalnan/e1b9cec37899699f8ab7da2b2ab17847 to your computer and use it in GitHub Desktop.
Save colincalnan/e1b9cec37899699f8ab7da2b2ab17847 to your computer and use it in GitHub Desktop.
Using the Behat Drupal Extension on sites with basic auth
<?php
/**
* Run before every scenario.
*
* @BeforeScenario
*/
public function beforeScenario() {
if ($basic_auth = $this->getDrupalParameter('basic_auth')) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Selenium2Driver) {
// Continue if this is a Selenium driver, since this is handled in
// locatePath().
}
else {
// Setup basic auth.
$this->getSession()->setBasicAuth($basic_auth['username'], $basic_auth['password']);
}
}
}
/**
* Override MinkContext::locatePath() to work around Selenium not supporting
* basic auth.
*/
public function locatePath($path) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Selenium2Driver && $basic_auth = $this->getDrupalParameter('basic_auth'))\
{
// Add the basic auth parameters to the base url. This only works for
// Firefox.
$startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
$startUrl = str_replace('://', '://' . $basic_auth['username'] . ':' . $basic_auth['password']\
. '@', $startUrl);
return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
}
else {
return parent::locatePath($path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment