Completed
Push — develop ( 723ec8...69e22a )
by Peter
08:18
created

AbstractTestCase   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 220
rs 10
wmc 27
lcom 1
cbo 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 2
A onNotSuccessfulTest() 0 6 2
A getSession() 0 4 1
A getBrowser() 0 4 2
A getBrowserBin() 0 4 1
A setBrowserBin() 0 5 1
A resolveHost() 0 6 2
A resolvePort() 0 6 2
A resolveUri() 0 8 2
A resolveBrowser() 0 17 4
A resolveWindowSize() 0 7 2
B resolveCapabilities() 0 17 5
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
use Exception;
13
use PHPWebDriver_WebDriver;
14
use RuntimeException;
15
16
/**
17
 * Base test case for Selenium WebDriver tests
18
 */
19
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
20
{
21
    use WebTrait;
22
    use WaitTrait;
23
    use ClickTrait;
24
    use InputTrait;
25
    use NotifyTrait;
26
    use WindowTrait;
27
    use ElementTrait;
28
    use ElementsTrait;
29
    use ScreenshotTrait;
30
31
    /**
32
     * Selenium Web Driver host URI
33
     *
34
     * @var string
35
     */
36
    protected static $webDriverHost = 'http://localhost:%s/wd/hub';
37
38
    /**
39
     * Selenium Web Driver port
40
     *
41
     * @var string
42
     */
43
    protected static $webDriverPort = '4444';
44
45
    /**
46
     * @var string
47
     */
48
    protected static $webDriverBrowser = 'firefox';
49
50
    /**
51
     * @var string
52
     */
53
    protected static $webDriverScreen = '1920x1080';
54
55
    /**
56
     * @var string
57
     */
58
    protected $browser;
59
60
    /**
61
     * @var string
62
     */
63
    protected $browserBin;
64
65
    /**
66
     * @var string
67
     */
68
    protected $uri;
69
70
    /**
71
     * @var PHPWebDriver_WebDriver
72
     */
73
    protected $webDriver;
74
75
    /**
76
     * @var \PHPWebDriver_WebDriverSession|WebDriver\SessionInterface
77
     */
78
    protected $session;
79
80
    /**
81
     * Resolves URI to open session
82
     */
83
    protected function setUp()
84
    {
85
        $this->uri       = $this->resolveUri();
86
        $this->webDriver = new PHPWebDriver_WebDriver($this->resolveHost());
87
        $this->session   = $this->webDriver->session($this->resolveBrowser(), $this->resolveCapabilities());
88
89
        $this->session->window()->postSize($this->resolveWindowSize());
90
    }
91
92
    /**
93
     * @return void
94
     */
95
    protected function tearDown()
96
    {
97
        $this->hasFailed() or $this->session->close();
0 ignored issues
show
Bug introduced by
The method close() does not seem to exist on object<WebinoDev\Test\Se...river\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
    }
99
100
    /**
101
     * @param Exception $exc
102
     */
103
    protected function onNotSuccessfulTest(Exception $exc)
104
    {
105
        $this->notifyError($exc);
106
        $this->session and $this->session->close();
0 ignored issues
show
Bug introduced by
The method close() does not seem to exist on object<WebinoDev\Test\Se...river\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        parent::onNotSuccessfulTest($exc);
108
    }
109
110
    /**
111
     * @return \PHPWebDriver_WebDriverSession|WebDriver\SessionInterface
112
     */
113
    public function getSession()
114
    {
115
        return $this->session;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    protected function getBrowser()
122
    {
123
        return $this->browser ? $this->browser : $this::$webDriverBrowser;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    protected function getBrowserBin()
130
    {
131
        return $this->browserBin;
132
    }
133
134
    /**
135
     * @param string $bin
136
     * @return string
137
     */
138
    protected function setBrowserBin($bin)
139
    {
140
        $this->browserBin = (string) $bin;
141
        return $this;
142
    }
143
144
    /**
145
     * Resolve Selenium WebDriver host
146
     *
147
     * @return string
148
     */
149
    protected function resolveHost()
150
    {
151
        $host = getenv('HOST');
152
        empty($host) and $host = $this::$webDriverHost;
153
        return sprintf($host, $this->resolvePort());
154
    }
155
156
    /**
157
     * Resolve Selenium WebDriver port
158
     *
159
     * @return string
160
     */
161
    protected function resolvePort()
162
    {
163
        $port = getenv('PORT');
164
        empty($port) and $port = $this::$webDriverPort;
165
        return $port;
166
    }
167
168
    /**
169
     * Resolve test target URI
170
     *
171
     * @return string
172
     */
173
    protected function resolveUri()
174
    {
175
        $uri = getenv('URI');
176
        if (empty($uri)) {
177
            throw new RuntimeException('Expected URI env');
178
        }
179
        return $uri;
180
    }
181
182
    /**
183
     * Resolve test session browser
184
     *
185
     * @return string
186
     */
187
    protected function resolveBrowser()
188
    {
189
        $browser = getenv('BROWSER');
190
        empty($browser) and $browser = $this::$webDriverBrowser;
191
192
        switch ($browser) {
193
            case 'chromium':
194
                $browser = 'chrome';
195
                $this->setBrowserBin('/usr/bin/chromium-browser');
196
                break;
197
            case 'firefox':
198
                $this->setBrowserBin('/var/lib/webino/firefox/firefox');
199
                break;
200
        }
201
202
        return $this->browser = $browser;
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    protected function resolveWindowSize()
209
    {
210
        $screen = getenv('SCREEN');
211
        empty($screen) and $screen = $this::$webDriverScreen;
212
        $size = explode('x', $screen);
213
        return ['width' => (int) $size[0], 'height' => (int) $size[1]];
214
    }
215
216
    /**
217
     * Resolve test session capabilities
218
     *
219
     * @return array
220
     */
221
    protected function resolveCapabilities()
222
    {
223
        switch ($this->getBrowser()) {
224
            case 'chrome':
225
                // Fixes OpenVZ
226
                $opts = ['args' => ['no-sandbox', 'start-maximized']];
227
                $bin  = $this->getBrowserBin();
228
                $bin and $opts+= ['binary' => $bin];
229
                return ['chromeOptions' => $opts];
230
            case 'firefox':
231
                $opts = [];
232
                $bin  = $this->getBrowserBin();
233
                $bin and $opts+= ['firefox_binary' => $bin];
234
                return $opts;
235
        }
236
        return [];
237
    }
238
}
239