Skip to content

Commit

Permalink
Dev: Wait for presence of welcome jumbotron (test); take screenshots …
Browse files Browse the repository at this point in the history
…at exception
  • Loading branch information
olleharstedt committed Nov 6, 2017
1 parent 21a7c5f commit e0e7251
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 24 deletions.
17 changes: 11 additions & 6 deletions tests/TestBaseClass.php
Expand Up @@ -11,23 +11,28 @@ class TestBaseClass extends TestCase
*/
protected static $testHelper = null;


/**
* @var int
*/
public static $surveyId = null;

public function setUp()
public static function setupBeforeClass()
{
parent::setUp();
self::$testHelper = new TestHelper();

self::$testHelper->importAll();

}

public function setUp()
{
parent::setUp();
}

protected static function importSurvey($fileName){
/**
* @param string $fileName
* @return void
*/
protected static function importSurvey($fileName)
{
\Yii::app()->session['loginID'] = 1;
$surveyFile = $fileName;
if (!file_exists($surveyFile)) {
Expand Down
41 changes: 34 additions & 7 deletions tests/TestBaseClassView.php
Expand Up @@ -16,16 +16,34 @@
use Facebook\WebDriver\WebDriverBy;

/**
* Class TestBaseClassWeb
* this is the base class for functional tests that need browser simulation
* @package ls\tests
*/
class TestBaseClassView extends TestBaseClassWeb
{

/**
*
*/
public static function setupBeforeClass()
{
parent::setupBeforeClass();
}

public function setUp()
{
parent::setUp();
$this->adminLogin('admin', 'password');

$username = getenv('USERNAME');

This comment has been minimized.

Copy link
@TonisOrmisson

TonisOrmisson Nov 7, 2017

Collaborator

The USERNAME conflicts with system variable. This is never (?) empty.

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Nov 7, 2017

Author Collaborator

Ah! Good catch, will fix.

if (empty($username)) {
$username = 'admin';
}

$password = getenv('PASSWORD');
if (empty($password)) {
$password = 'password';
}

$this->adminLogin($username, $password);
}

/**
Expand All @@ -34,16 +52,25 @@ public function setUp()
*/
protected function findViewTag($name, $view)
{
$this->openView($view);
$url = $this->getUrl($view);
$this->openView($url);
$element = null;

$screenshot = $this->webDriver->takeScreenshot();
file_put_contents(__DIR__ . '/_output/'.$name.'.png', $screenshot);
try {
$element = $this->webDriver->findElement(WebDriverBy::id('action::'.$name));
} catch (\Exception $e) {
$screenshot = $this->webDriver->takeScreenshot();
file_put_contents(__DIR__ . '/_output/'.$name.'.png', $screenshot);
//throw new Exception($e->getMessage());
}
$this->assertNotEmpty($element, sprintf('FAILED viewing %s on route %s', $name, $view['route']));
$this->assertNotEmpty(
$element,
sprintf(
'FAILED viewing %s on route %s, full url %s',
$name,
$view['route'],
$url
)
);
}
}
69 changes: 59 additions & 10 deletions tests/TestBaseClassWeb.php
Expand Up @@ -18,6 +18,7 @@
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Exception\TimeOutException;

/**
* Class TestBaseClassWeb
Expand All @@ -40,13 +41,14 @@ class TestBaseClassWeb extends TestBaseClass
public function setUp()
{
parent::setUp();

if (empty(getenv('DOMAIN'))) {
die('Must specify DOMAIN environment variable to run this test, like "DOMAIN=localhost/limesurvey" or "DOMAIN=limesurvey.localhost".');
}

$capabilities = DesiredCapabilities::phantomjs();
$this->webDriver = RemoteWebDriver::create("http://localhost:{$this->webPort}/", $capabilities);

$this->webDriver->manage()->window()->maximize();
}

/**
Expand All @@ -62,28 +64,75 @@ public function tearDown()
* @param array $view
* @return WebDriver
*/
public function openView($view)
public function openView($url)
{
if (!is_string($url)) {
throw new \Exception('$url must be a string, is ' . json_encode($url));
}
return $this->webDriver->get($url);
}

/**
* @param array $view
*/
public function getUrl(array $view)
{
$domain = getenv('DOMAIN');
if (empty($domain)) {
$domain = '';
}
$url = "http://{$domain}/index.php/admin/".$view['route'];
return $this->webDriver->get($url);
return "http://{$domain}/index.php/admin/".$view['route'];
}

public function adminLogin($userName, $passWord)
/**
* @param string $userName
* @param string $password
* @return void
*/
public function adminLogin($userName, $password)
{
$this->openView(['route'=>'authentication/sa/login']);
$url = $this->getUrl(['route'=>'authentication/sa/login']);
$this->openView($url);

try {
$this->webDriver->wait(2)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::id('user')
)
);
} catch (TimeOutException $ex) {
//$name =__DIR__ . '/_output/loginfailed.png';
$screenshot = $this->webDriver->takeScreenshot();
file_put_contents(__DIR__ .'/tmp.png', $screenshot);
$this->assertTrue(
false,
sprintf(
'Could not login on url %s: Could not find element with id "user".',
$url
)
);
}

$userNameField = $this->webDriver->findElement(WebDriverBy::id("user"));
$userNameField->clear()->sendKeys($userName);
$passWordField = $this->webDriver->findElement(WebDriverBy::id("password"));
$passWordField->clear()->sendKeys($passWord);
$passWordField->clear()->sendKeys($password);

$submit = $this->webDriver->findElement(WebDriverBy::name('login_submit'));
$submit->click();
return $this->webDriver->wait(10, 1000)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::id('welcome-jumbotron'))
);
try {
$this->webDriver->wait(2)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::id('welcome-jumbotron')
)
);
} catch (TimeOutException $ex) {
$screenshot = $this->webDriver->takeScreenshot();
file_put_contents(__DIR__ .'/tmp.png', $screenshot);
$this->assertTrue(
false,
'Found no welcome jumbotron after login.'
);
}
}
}
4 changes: 3 additions & 1 deletion tests/questions/DateTimeValidationTest.php
Expand Up @@ -22,6 +22,8 @@ class DateTimeValidationTest extends TestBaseClassWeb
*/
public static function setupBeforeClass()
{
parent::setupBeforeClass();

\Yii::app()->session['loginID'] = 1;

$surveyFile = __DIR__ . '/../data/surveys/limesurvey_survey_834477.lss';
Expand All @@ -31,7 +33,7 @@ public static function setupBeforeClass()

$translateLinksFields = false;
$newSurveyName = null;
$result = importSurveyFile(
$result = \importSurveyFile(
$surveyFile,
$translateLinksFields,
$newSurveyName,
Expand Down

0 comments on commit e0e7251

Please sign in to comment.