Snippets
Created by
Steve Adams
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | <?php
namespace Your\Namespace\Tests\Features;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\Exception\StaleElementReferenceException;
/**
* @coversNothing
*/
abstract class AbstractBaseFeatureTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RemoteWebDriver
*/
protected $webDriver;
/**
* Set this in phpunit.xml
* @var string
*/
protected $url = WEBDRIVER_BASE_SITE_URL;
/**
* @var DesiredCapabilities
*/
protected $desiredCapabilities;
/**
* Sets the desiredCapabilities field based on the WEBDRIVER_BROWSER_NAME constant defined in phpunit.xml. Currently
* supports chrome and firefox, other values will result in an exception.
*/
public function __construct()
{
parent::__construct();
switch (WEBDRIVER_BROWSER_NAME) {
case 'chrome':
$this->desiredCapabilities = DesiredCapabilities::chrome();
break;
case 'firefox':
$this->desiredCapabilities = DesiredCapabilities::firefox();
break;
default:
throw new \Exception('Running tests on ' . ucfirst(WEBDRIVER_BROWSER_NAME) . ' is not supported.');
}
}
protected function setUp()
{
$this->webDriver = RemoteWebDriver::create(
WEBDRIVER_HOST,
$this->desiredCapabilities
);
}
protected function tearDown()
{
$this->webDriver->quit();
}
/**
* Loads the page at $url. Returns control to the caller after the page has finished loading.
*
* @param string $url
*/
protected function getPageAndWaitForLoad($url)
{
$this->webDriver->get($url);
$this->webDriver->wait(10, 100)->until(function () {
return $this->webDriver->executeScript('return document.readyState') == 'complete';
});
}
/**
* Finds the element identified by $condition and passes it to clickThroughToNewPageByElement. Returns control to
* the caller after the new page finishes loading.
*
* @param WebDriverBy $condition
*/
protected function clickThroughToNewPageBy(WebDriverBy $condition)
{
$element = $this->webDriver->findElement($condition);
$this->clickThroughToNewPageByElement($element);
}
/**
* Syntax sugar for calling clickThroughToNewPageBy with a link's text.
*
* @param string $linkText
*/
protected function clickThroughToNewPageByLinkText($linkText)
{
$this->clickThroughToNewPageBy(WebDriverBy::linkText($linkText));
}
/**
* Syntax sugar for calling clickThroughToNewPageBy with a CSS selector.
*
* @param string $cssSelector
*/
protected function clickThroughToNewPageByCssSelector($cssSelector)
{
$this->clickThroughToNewPageBy(WebDriverBy::cssSelector($cssSelector));
}
/**
* Syntax sugar for calling clickThroughToNewPageBy with an ID.
*
* @param string $id
*/
protected function clickThroughToNewPageById($id)
{
$this->clickThroughToNewPageBy(WebDriverBy::id($id));
}
/**
* Syntax sugar for calling clickThroughToNewPageBy with an XPath selector.
*
* @param string $xPath
*/
protected function clickThroughToNewPageByXPath($xPath)
{
$this->clickThroughToNewPageBy(WebDriverBy::xpath($xPath));
}
/**
* Clicks on the given element and waits for a new page to load.
*
* @param RemoteWebElement $element
*/
protected function clickThroughToNewPageByElement(RemoteWebElement $element)
{
$element->click();
$this->webDriver->wait(10, 100)->until(WebDriverExpectedCondition::stalenessOf($element));
}
/**
* Gets an element using xpath but waits until it's present by a given condition.
*
* @param string $xpath The xpath for the element - Defaults to presenceOfElementLocated.
* @param string $condition A WebDriverExpectedCondition
*
* @return WebDriverRemoteElement
*/
protected function getElementByXpath($xpath, $condition = "presenceOfElementLocated")
{
$this->webDriver->wait(10, 100)->until(
WebDriverExpectedCondition::$condition(
WebDriverBy::xpath($xpath)
)
);
return $this->webDriver->findElement(WebDriverBy::xpath($xpath));
}
/**
* Gets elements using xpath
*
* @param string $xpath The xpath for the elements.
*
* @return array
*/
protected function getElementsByXpath($xpath)
{
return $this->webDriver->findElements(WebDriverBy::xpath($xpath));
}
/**
* Click 'okay' on an alert.
*
* @return AbstractBaseFeatureTest
*/
protected function acceptAlert()
{
$this->webDriver->wait()->until(
WebDriverExpectedCondition::alertIsPresent()
);
$this->webDriver->switchTo()->alert()->accept();
return $this;
}
/**
* Click 'cancel' on an alert.
*
* @return AbstractBaseFeatureTest
*/
protected function dismissAlert()
{
$this->webDriver->wait()->until(
WebDriverExpectedCondition::alertIsPresent()
);
$this->webDriver->switchTo()->alert()->dismiss();
return $this;
}
/**
* Output text to the console during a test.
*
* @param string $value Text to output.
* @param string $colour Which colour to make the text.
*/
protected function output($value, $colour = null)
{
if ($colour) {
$colour = $this->getOutputColour($colour);
} else {
$colour = "";
}
fwrite(STDOUT, $colour . "{$value} \033[0m \n");
}
/**
* Get a colour code for outputting text.
*
* @param int $colour
*
* @return string The code for the given colour.
*/
protected function getOutputColour($colour)
{
$colours = [
"white" => 37,
"error" => 31,
"success" => 32,
"warning" => 33,
"info" => 34
];
return "\033[{$colours[$colour]}m";
}
/**
* Tries clicking an element. This repeated attempting pattern catches stale
* element exceptions which, until I wrote this method, were the bane of my existence.\
* This method isn't essential for every click, but it helps a lot with cases
* where JavaScript manages the DOM after a previous click occurred. For clicks
* before Javascript needs to run, this is probably overkill.
*
* @param RemoteWebElement $element The element to try clicking.
* @param string $elementXpath Path to the element to try re-referencing it.
* @param int $numAttempts How many times to try clicking.
* @param int $wait How long to wait between clicks in milliseconds.
*
* @return AbstractBaseFeatureTest
*/
protected function tryClicking(RemoteWebElement $element, $elementXpath = null, $numAttempts = 5, $wait = 10)
{
$attempts = 0;
while ($attempts < $numAttempts) {
$attempts ++;
try {
$element->click();
return $this;
} catch (StaleElementReferenceException $exception) {
if ($elementXpath) {
$this->output(
"Re-referencing element due to stale element reference...",
"error"
);
$element = $this->getElementByXpath($elementXpath, "elementToBeClickable");
}
$remaining = $numAttempts - $attempts;
$this->output(
"Caught StaleElementReferenceException; waiting {$wait}ms then clicking again ({$remaining} tries remaining)...",
"error"
);
$this->webDriver->manage()->timeouts()->implicitlyWait($wait);
}
}
// Looks like we made it here, so we should let the exception happen.
throw $exception;
return $this;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.