File: Browser.php

package info (click to toggle)
roundcube 1.6.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,888 kB
  • sloc: javascript: 195,591; php: 76,917; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (314 lines) | stat: -rw-r--r-- 7,604 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php

namespace Tests\Browser;

use Facebook\WebDriver\WebDriverKeys;
use PHPUnit\Framework\Assert;
use Tests\Browser\Components;

/**
 * Laravel Dusk Browser extensions
 */
class Browser extends \Laravel\Dusk\Browser
{
    /**
     * Assert number of (visible) elements
     */
    public function assertElementsCount($selector, $expected_count, $visible = true)
    {
        $elements = $this->elements($selector);
        $count = count($elements);

        if ($visible) {
            foreach ($elements as $element) {
                if (!$element->isDisplayed()) {
                    $count--;
                }
            }
        }

        Assert::assertEquals($expected_count, $count);

        return $this;
    }

    /**
     * Assert specified rcmail.env value
     */
    public function assertEnvEquals($key, $expected)
    {
        Assert::assertEquals($expected, $this->getEnv($key));

        return $this;
    }

    /**
     * Assert specified checkbox state
     */
    public function assertCheckboxState($selector, $state)
    {
        if ($state) {
            $this->assertChecked($selector);
        }
        else {
            $this->assertNotChecked($selector);
        }

        return $this;
    }

    /**
     * Assert that the given element has specified class assigned.
     */
    public function assertHasClass($selector, $class_name)
    {
        $fullSelector = $this->resolver->format($selector);
        $element      = $this->resolver->findOrFail($selector);
        $classes      = explode(' ', (string) $element->getAttribute('class'));

        Assert::assertContains($class_name, $classes);

        return $this;
    }

    /**
     * Assert Task menu state
     */
    public function assertTaskMenu($selected)
    {
        $this->with(new Components\Taskmenu(), function ($browser) use ($selected) {
            $browser->assertMenuState($selected);
        });

        return $this;
    }

    /**
     * Assert toolbar menu state
     */
    public function assertToolbarMenu($active, $disabled = [], $missing = [])
    {
        $this->with(new Components\Toolbarmenu(), function ($browser) use ($active, $disabled, $missing) {
            $browser->assertMenuState($active, $disabled, $missing);
        });

        return $this;
    }

    /**
     * Close toolbar menu (on phones)
     */
    public function closeToolbarMenu()
    {
        $this->with(new Components\Toolbarmenu(), function ($browser) {
            $browser->closeMenu();
        });

        return $this;
    }

    /**
     * Select taskmenu item
     */
    public function clickTaskMenuItem($name)
    {
        $this->with(new Components\Taskmenu(), function ($browser) use ($name) {
            $browser->clickMenuItem($name);
        });

        return $this;
    }

    /**
     * Select toolbar menu item
     */
    public function clickToolbarMenuItem($name, $dropdown_action = null)
    {
        $this->with(new Components\Toolbarmenu(), function ($browser) use ($name, $dropdown_action) {
            $browser->clickMenuItem($name, $dropdown_action);
        });

        return $this;
    }

    /**
     * Shortcut to click an element while holding CTRL key
     */
    public function ctrlClick($selector)
    {
        $this->driver->getKeyboard()->pressKey(WebDriverKeys::LEFT_CONTROL);
        $this->element($selector)->click();
        $this->driver->getKeyboard()->releaseKey(WebDriverKeys::LEFT_CONTROL);

        return $this;
    }

    /**
     * Return rcmail.env entry
     */
    public function getEnv($key)
    {
        return $this->driver->executeScript("return rcmail.env['{$key}']");
    }

    /**
     * Visit specified task/action with logon if needed
     */
    public function go($task = 'mail', $action = null, $login = true)
    {
        $this->with(new Components\App(), function ($browser) use ($task, $action, $login) {
            $browser->gotoAction($task, $action, $login);
        });

        return $this;
    }

    /**
     * Check if in Phone mode
     */
    public static function isPhone()
    {
        return getenv('TESTS_MODE') == 'phone';
    }

    /**
     * Check if in Tablet mode
     */
    public static function isTablet()
    {
        return getenv('TESTS_MODE') == 'tablet';
    }

    /**
     * Check if in Desktop mode
     */
    public static function isDesktop()
    {
        return !self::isPhone() && !self::isTablet();
    }

    /**
     * Handler for actions that expect to open a new window
     *
     * @param callable $callback Function to execute with Browser object as argument
     *
     * @return array Main window handle and new window handle
     */
    public function openWindow($callback)
    {
        $current_window = $this->driver->getWindowHandle();
        $before_handles = $this->driver->getWindowHandles();

        $callback($this);

        $after_handles = $this->driver->getWindowHandles();
        $new_window    = array_first(array_diff($after_handles, $before_handles));

        return [$current_window, $new_window];
    }

    /**
     * Change state of the Elastic's pretty checkbox
     */
    public function setCheckboxState($selector, $state)
    {
        // Because you can't operate on the original checkbox directly
        $this->ensurejQueryIsAvailable();

        if ($state) {
            $run = "if (!element.prev().is(':checked')) element.click()";
        }
        else {
            $run = "if (element.prev().is(':checked')) element.click()";
        }

        $this->script(
            "var element = jQuery('$selector')[0] || jQuery('input[name=$selector]')[0];"
            ."element = jQuery(element).next('.custom-control-label'); $run;"
        );

        return $this;
    }

    /**
     * Returns content of a downloaded file
     */
    public function readDownloadedFile($filename)
    {
        $filename = TESTS_DIR . "downloads/$filename";

        // Give the browser a chance to finish download
        if (!file_exists($filename)) {
            sleep(2);
        }

        Assert::assertFileExists($filename);

        return file_get_contents($filename);
    }

    /**
     * Removes downloaded file
     */
    public function removeDownloadedFile($filename)
    {
        @unlink(TESTS_DIR . "downloads/$filename");

        return $this;
    }

    /**
     * Close UI (notice/confirmation/loading/error/warning) message
     */
    public function closeMessage($type)
    {
        $selector = '#messagestack > div.' . $type;

        $this->click($selector);

        return $this;
    }

    /**
     * Wait until the UI is unlocked
     */
    public function waitUntilNotBusy()
    {
        $this->waitUntil("!rcmail.busy");

        return $this;
    }

    /**
     * Wait for UI (notice/confirmation/loading/error/warning) message
     * and assert it's text
     */
    public function waitForMessage($type, $text)
    {
        $selector = '#messagestack > div.' . $type;

        $this->waitFor($selector)->assertSeeIn($selector, $text);

        return $this;
    }

    /**
     * Execute code within body context.
     * Useful to execute code that selects elements outside of a component context
     */
    public function withinBody($callback)
    {
        if ($this->resolver->prefix != 'body') {
            $orig_prefix = $this->resolver->prefix;
            $this->resolver->prefix = 'body';
        }

        call_user_func($callback, $this);

        if (isset($orig_prefix)) {
            $this->resolver->prefix = $orig_prefix;
        }

        return $this;
    }
}