[ticket/14315] Add more functionality to ui test cases

PHPBB3-14315
This commit is contained in:
Marc Alexander 2016-02-16 22:45:31 +01:00
parent 6f41fcb3d6
commit 845639ba37

View file

@ -34,6 +34,18 @@ class phpbb_ui_test_case extends phpbb_test_case
static protected $install_success = false; static protected $install_success = false;
static protected $db; static protected $db;
/**
* Session ID for current test's session (each test makes its own)
* @var string
*/
protected $sid;
/**
* Language array used by phpBB
* @var array
*/
protected $lang = array();
static public function setUpBeforeClass() static public function setUpBeforeClass()
{ {
parent::setUpBeforeClass(); parent::setUpBeforeClass();
@ -87,6 +99,11 @@ class phpbb_ui_test_case extends phpbb_test_case
{ {
$this->fail('Installing phpBB has failed.'); $this->fail('Installing phpBB has failed.');
} }
// Clear the language array so that things
// that were added in other tests are gone
$this->lang = array();
$this->add_lang('common');
} }
protected function tearDown() protected function tearDown()
@ -258,4 +275,195 @@ class phpbb_ui_test_case extends phpbb_test_case
} }
return self::$db; return self::$db;
} }
/**
* Login to the ACP
* You must run login() before calling this.
*/
protected function admin_login($username = 'admin')
{
$this->add_lang('acp/common');
// Requires login first!
if (empty($this->sid))
{
$this->fail('$this->sid is empty. Make sure you call login() before admin_login()');
return;
}
$this->visit('adm/index.php?sid=' . $this->sid);
$this->assertContains($this->lang('LOGIN_ADMIN_CONFIRM'), self::$webDriver->getPageSource());
self::find_element('cssSelector', 'input[name=username]')->clear()->sendKeys($username);
self::find_element('cssSelector', 'input[type=password]')->sendKeys($username . $username);
self::find_element('cssSelector', 'input[name=login]')->click();
$this->assertContains($this->lang('ADMIN_PANEL'), $this->find_element('cssSelector', 'h1')->getText());
$cookies = self::$webDriver->manage()->getCookies();
// The session id is stored in a cookie that ends with _sid - we assume there is only one such cookie
foreach ($cookies as $cookie)
{
if (substr($cookie['name'], -4) == '_sid')
{
$this->sid = $cookie['value'];
}
}
}
protected function add_lang($lang_file)
{
if (is_array($lang_file))
{
foreach ($lang_file as $file)
{
$this->add_lang($file);
}
}
$lang_path = __DIR__ . "/../../phpBB/language/en/$lang_file.php";
$lang = array();
if (file_exists($lang_path))
{
include($lang_path);
}
$this->lang = array_merge($this->lang, $lang);
}
protected function add_lang_ext($ext_name, $lang_file)
{
if (is_array($lang_file))
{
foreach ($lang_file as $file)
{
$this->add_lang_ext($ext_name, $file);
}
return;
}
$lang_path = __DIR__ . "/../../phpBB/ext/{$ext_name}/language/en/$lang_file.php";
$lang = array();
if (file_exists($lang_path))
{
include($lang_path);
}
$this->lang = array_merge($this->lang, $lang);
}
protected function lang()
{
$args = func_get_args();
$key = $args[0];
if (empty($this->lang[$key]))
{
throw new RuntimeException('Language key "' . $key . '" could not be found.');
}
$args[0] = $this->lang[$key];
return call_user_func_array('sprintf', $args);
}
/**
* assertContains for language strings
*
* @param string $needle Search string
* @param string $haystack Search this
* @param string $message Optional failure message
*/
public function assertContainsLang($needle, $haystack, $message = null)
{
$this->assertContains(html_entity_decode($this->lang($needle), ENT_QUOTES), $haystack, $message);
}
/**
* assertNotContains for language strings
*
* @param string $needle Search string
* @param string $haystack Search this
* @param string $message Optional failure message
*/
public function assertNotContainsLang($needle, $haystack, $message = null)
{
$this->assertNotContains(html_entity_decode($this->lang($needle), ENT_QUOTES), $haystack, $message);
}
protected function login($username = 'admin')
{
$this->add_lang('ucp');
$this->visit('ucp.php');
$this->assertContains($this->lang('LOGIN_EXPLAIN_UCP'), self::$webDriver->getPageSource());
self::find_element('cssSelector', 'input[name=username]')->sendKeys($username);
self::find_element('cssSelector', 'input[name=password]')->sendKeys($username . $username);
self::find_element('cssSelector', 'input[name=login]')->click();
$this->assertNotContains($this->lang('LOGIN'), $this->find_element('className', 'navbar')->getText());
$cookies = self::$webDriver->manage()->getCookies();
// The session id is stored in a cookie that ends with _sid - we assume there is only one such cookie
foreach ($cookies as $cookie)
{
if (substr($cookie['name'], -4) == '_sid')
{
$this->sid = $cookie['value'];
}
}
}
/**
* Take screenshot. Can be used for debug purposes.
*
* @param \Facebook\WebDriver\Remote\RemoteWebElement $element
* @return string Element screenshot path
* @throws Exception When screenshot can't be created
*/
public function takeScreenshot($element = null) {
// Change the Path to your own settings
$screenshot = time() . ".png";
// Change the driver instance
self::$webDriver->takeScreenshot($screenshot);
if(!file_exists($screenshot)) {
throw new Exception('Could not save screenshot');
}
if( ! (bool) $element) {
return $screenshot;
}
$element_screenshot = time() . ".png"; // Change the path here as well
$element_width = $element->getSize()->getWidth();
$element_height = $element->getSize()->getHeight();
$element_src_x = $element->getLocation()->getX();
$element_src_y = $element->getLocation()->getY();
// Create image instances
$src = imagecreatefrompng($screenshot);
$dest = imagecreatetruecolor($element_width, $element_height);
// Copy
imagecopy($dest, $src, 0, 0, $element_src_x, $element_src_y, $element_width, $element_height);
imagepng($dest, $element_screenshot);
// unlink($screenshot); // unlink function might be restricted in mac os x.
if( ! file_exists($element_screenshot)) {
throw new Exception('Could not save element screenshot');
}
return $element_screenshot;
}
} }