diff --git a/tests/TestBaseClass.php b/tests/TestBaseClass.php index 3628bc5b0ab..c21cc147a13 100644 --- a/tests/TestBaseClass.php +++ b/tests/TestBaseClass.php @@ -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)) { diff --git a/tests/TestBaseClassView.php b/tests/TestBaseClassView.php index db64d1e3bab..5fc6392a471 100644 --- a/tests/TestBaseClassView.php +++ b/tests/TestBaseClassView.php @@ -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'); + if (empty($username)) { + $username = 'admin'; + } + + $password = getenv('PASSWORD'); + if (empty($password)) { + $password = 'password'; + } + + $this->adminLogin($username, $password); } /** @@ -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 + ) + ); } } diff --git a/tests/TestBaseClassWeb.php b/tests/TestBaseClassWeb.php index 9e9999636bd..0936096f766 100644 --- a/tests/TestBaseClassWeb.php +++ b/tests/TestBaseClassWeb.php @@ -18,6 +18,7 @@ use Facebook\WebDriver\WebDriver; use Facebook\WebDriver\WebDriverBy; use Facebook\WebDriver\WebDriverExpectedCondition; +use Facebook\WebDriver\Exception\TimeOutException; /** * Class TestBaseClassWeb @@ -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(); } /** @@ -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.' + ); + } } } diff --git a/tests/questions/DateTimeValidationTest.php b/tests/questions/DateTimeValidationTest.php index 81406a2da13..dad44739270 100644 --- a/tests/questions/DateTimeValidationTest.php +++ b/tests/questions/DateTimeValidationTest.php @@ -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'; @@ -31,7 +33,7 @@ public static function setupBeforeClass() $translateLinksFields = false; $newSurveyName = null; - $result = importSurveyFile( + $result = \importSurveyFile( $surveyFile, $translateLinksFields, $newSurveyName,