Thomas Bruederli
2013-07-12 deb2b8d0804d1d25a3f28266747ce9041495b372
commit | author | age
a2a1f8 1 <?php
AM 2
3 /*
4  +-----------------------------------------------------------------------+
5  | tests/Selenium/bootstrap.php                                          |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2009-2013, The Roundcube Dev Team                       |
9  |                                                                       |
10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
13  |                                                                       |
14  | PURPOSE:                                                              |
15  |   Environment initialization script for unit tests                    |
16  +-----------------------------------------------------------------------+
17  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
18  | Author: Aleksander Machniak <alec@alec.pl>                            |
19  +-----------------------------------------------------------------------+
20 */
21
22 if (php_sapi_name() != 'cli')
23   die("Not in shell mode (php-cli)");
24
25 if (!defined('INSTALL_PATH')) define('INSTALL_PATH', realpath(dirname(__FILE__) . '/../../') . '/' );
26
27 define('TESTS_DIR', dirname(__FILE__) . '/');
28
29 if (@is_dir(TESTS_DIR . 'config')) {
deb2b8 30     define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config');
a2a1f8 31 }
AM 32
33 require_once(INSTALL_PATH . 'program/include/iniset.php');
34
35 // Extend include path so some plugin test won't fail
36 $include_path = ini_get('include_path') . PATH_SEPARATOR . TESTS_DIR . '..';
37 if (set_include_path($include_path) === false) {
38     die("Fatal error: ini_set/set_include_path does not work.");
39 }
40
deb2b8 41 $rcmail = rcube::get_instance('test');
a2a1f8 42
AM 43 define('TESTS_URL',     $rcmail->config->get('tests_url'));
44 define('TESTS_BROWSER', $rcmail->config->get('tests_browser', 'firefox'));
45 define('TESTS_USER',    $rcmail->config->get('tests_username'));
46 define('TESTS_PASS',    $rcmail->config->get('tests_password'));
47 define('TESTS_SLEEP',   $rcmail->config->get('tests_sleep', 5));
48
49 PHPUnit_Extensions_Selenium2TestCase::shareSession(true);
50
51 // @TODO: remove user record from DB before running tests
52 // @TODO: make sure mailbox has some content (always the same) or is empty
846669 53 // @TODO: plugins: enable all?
a2a1f8 54
AM 55 /**
56  * Base class for all tests in this directory
57  */
58 class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase
59 {
60     protected function setUp()
61     {
62 //        $this->rc = rcube::get_instance();
63         $this->setBrowser(TESTS_BROWSER);
846669 64
AM 65         // Set root to our index.html, for better performance
66         // See https://github.com/sebastianbergmann/phpunit-selenium/issues/217
67         $this->setBrowserUrl(TESTS_URL . '/tests/Selenium');
a2a1f8 68     }
AM 69
70     protected function login()
71     {
72         $this->go('mail');
73
74         $user_input = $this->byCssSelector('form input[name="_user"]');
75         $pass_input = $this->byCssSelector('form input[name="_pass"]');
76         $submit     = $this->byCssSelector('form input[type="submit"]');
77
78         $user_input->value(TESTS_USER);
79         $pass_input->value(TESTS_PASS);
80
81         // submit login form
82         $submit->click();
83
84         // wait after successful login
85         sleep(TESTS_SLEEP);
86     }
87
88     protected function go($task = 'mail', $action = null)
89     {
90         $this->url(TESTS_URL . '/?_task=' . $task);
91
92         // wait for interface load (initial ajax requests, etc.)
93         sleep(TESTS_SLEEP);
94
95         if ($action) {
96             $this->click_button($action);
97
98             sleep(TESTS_SLEEP);
99         }
100     }
101
102     protected function get_env()
103     {
104         return $this->execute(array(
105             'script' => 'return rcmail.env;',
106             'args' => array(),
107         ));
108     }
109
110     protected function get_buttons($action)
111     {
112         $buttons = $this->execute(array(
113             'script' => "return rcmail.buttons['$action'];",
114             'args' => array(),
115         ));
116
117         if (is_array($buttons)) {
118             foreach ($buttons as $idx => $button) {
119                 $buttons[$idx] = $button['id'];
120             }
121         }
122
123         return (array) $buttons;
124     }
125
126     protected function get_objects()
127     {
128         return $this->execute(array(
129             'script' => "var i,r = []; for (i in rcmail.gui_objects) r.push(i); return r;",
130             'args' => array(),
131         ));
132     }
133
134     protected function click_button($action)
135     {
136         $buttons = $this->get_buttons($action);
137         $id      = array_shift($buttons);
138
139         // this doesn't work for me
140         $this->byId($id)->click();
141     }
142
143     protected function ajaxResponse($action, $script = '', $button = false)
144     {
145         if (!$script && !$button) {
146             $script = "rcmail.command('$action')";
147         }
148
149         $script = 
150         "if (!window.test_ajax_response) {
151             window.test_ajax_response_object = {};
152             function test_ajax_response(response)
153             {
154                 if (response.response && response.response.action) {
155                     window.test_ajax_response_object[response.response.action] = response.response;
156                 }
157             }
158             rcmail.addEventListener('responsebefore', test_ajax_response);
159         }
160         window.test_ajax_response_object['$action'] = null;
161         $script;
162         ";
163
164         // run request
165         $this->execute(array(
166             'script' => $script,
167             'args' => array(),
168         ));
169
170         if ($button) {
171             $this->click_button($action);
172         }
173
174         // wait
175         sleep(TESTS_SLEEP);
176
177         // get response
178         $response = $this->execute(array(
179             'script' => "return window.test_ajax_response_object['$action'];",
180             'args' => array(),
181         ));
182
183         return $response;
184     }
185 }