koel/tests/e2e/WebDriverShortcuts.php

257 lines
4.9 KiB
PHP
Raw Normal View History

2016-11-15 07:54:41 +00:00
<?php
namespace E2E;
use Facebook\WebDriver\Interactions\Internal\WebDriverDoubleClickAction;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
2016-11-23 13:22:47 +00:00
use Facebook\WebDriver\WebDriverExpectedCondition;
2016-11-15 07:54:41 +00:00
use Facebook\WebDriver\WebDriverKeys;
trait WebDriverShortcuts
{
/**
* @var RemoteWebDriver
*/
protected $driver;
/**
* @param $selector WebDriverElement|string
*
* @return WebDriverElement
*/
protected function el($selector)
{
2016-11-15 10:03:59 +00:00
if (is_string($selector)) {
return $this->driver->findElement(WebDriverBy::cssSelector($selector));
2016-11-15 07:54:41 +00:00
}
2016-11-15 10:03:59 +00:00
return $selector;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Get a list of elements by a selector.
*
* @param $selector
*
* @return \Facebook\WebDriver\Remote\RemoteWebElement[]
*/
2016-11-15 07:54:41 +00:00
protected function els($selector)
{
return $this->driver->findElements(WebDriverBy::cssSelector($selector));
}
2016-11-24 03:56:00 +00:00
/**
* Type a string.
*
* @param $string
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function type($string)
{
2016-11-24 03:56:00 +00:00
$this->driver->getKeyboard()->sendKeys($string);
return $this;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Type into an element.
*
* @param $element
* @param $string
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function typeIn($element, $string)
{
$this->click($element)->clear();
return $this->type($string);
}
2016-11-24 03:56:00 +00:00
/**
* Press a key.
*
* @param string $key
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function press($key = WebDriverKeys::ENTER)
{
2016-11-24 03:56:00 +00:00
$this->driver->getKeyboard()->pressKey($key);
return $this;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Press enter.
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function enter()
{
return $this->press();
}
2016-11-24 03:56:00 +00:00
/**
* Click an element.
*
* @param $element
*
* @return WebDriverElement
*/
2016-11-15 07:54:41 +00:00
protected function click($element)
{
return $this->el($element)->click();
}
2016-11-24 03:56:00 +00:00
/**
* Right-click an element.
*
* @param $element
*
* @return \Facebook\WebDriver\Remote\RemoteMouse
*/
2016-11-15 07:54:41 +00:00
protected function rightClick($element)
{
return $this->driver->getMouse()->contextClick($this->el($element)->getCoordinates());
}
2016-11-24 03:56:00 +00:00
/**
* Double-click and element.
*
* @param $element
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function doubleClick($element)
{
2016-11-24 03:56:00 +00:00
(new WebDriverDoubleClickAction($this->driver->getMouse(), $this->el($element)))->perform();
2016-11-15 07:54:41 +00:00
2016-11-24 03:56:00 +00:00
return $this;
2016-11-15 07:54:41 +00:00
}
/**
* Sleep (implicit wait) for some seconds.
*
* @param $seconds
2016-11-24 03:56:00 +00:00
*
* @return $this
2016-11-15 07:54:41 +00:00
*/
protected function sleep($seconds)
{
$this->driver->manage()->timeouts()->implicitlyWait($seconds);
2016-11-24 03:56:00 +00:00
return $this;
2016-11-15 07:54:41 +00:00
}
/**
* Wait until a condition is met.
*
2016-11-23 16:47:10 +00:00
* @param $func (closure|WebDriverExpectedCondition)
2016-11-15 07:54:41 +00:00
* @param int $timeout
*
* @throws \Exception
2016-11-23 16:47:10 +00:00
*
2016-11-24 03:56:00 +00:00
* @return $this
2016-11-15 07:54:41 +00:00
*/
protected function waitUntil($func, $timeout = 10)
{
2016-11-24 03:56:00 +00:00
$this->driver->wait($timeout)->until($func);
return $this;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Wait and validate an element to be visible.
*
* @param $selector
*
* @return $this
*
* @throws \Exception
*/
2016-11-23 15:36:12 +00:00
public function see($selector)
2016-11-23 13:22:47 +00:00
{
2016-11-24 03:56:00 +00:00
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
2016-11-23 13:22:47 +00:00
WebDriverBy::cssSelector($selector)
));
2016-11-24 03:56:00 +00:00
return $this;
2016-11-23 13:22:47 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Wait and validate an element to be invisible.
*
* @param $selector string The element's CSS selector.
*
* @return $this
*
* @throws \Exception
*/
2016-11-23 15:36:12 +00:00
public function notSee($selector)
2016-11-23 13:22:47 +00:00
{
2016-11-24 03:56:00 +00:00
$this->waitUntil(WebDriverExpectedCondition::invisibilityOfElementLocated(
2016-11-23 13:22:47 +00:00
WebDriverBy::cssSelector($selector)
));
2016-11-24 03:56:00 +00:00
return $this;
2016-11-23 13:22:47 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Wait and validate a text to be visible in an element.
*
* @param $text
* @param $selector string The element's CSS selector.
*
* @return $this
* @throws \Exception
*/
2016-11-23 15:36:12 +00:00
public function seeText($text, $selector)
2016-11-23 13:22:47 +00:00
{
$this->waitUntil(WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector($selector), $text
));
2016-11-24 03:56:00 +00:00
return $this;
2016-11-23 13:22:47 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Navigate back.
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function back()
{
2016-11-24 03:56:00 +00:00
$this->driver->navigate()->back();
return $this;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Navigate forward.
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function forward()
{
2016-11-24 03:56:00 +00:00
$this->driver->navigate()->forward();
return $this;
2016-11-15 07:54:41 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Refresh the page.
*
* @return $this
*/
2016-11-15 07:54:41 +00:00
protected function refresh()
{
2016-11-24 03:56:00 +00:00
$this->driver->navigate()->refresh();
return $this;
2016-11-15 07:54:41 +00:00
}
}