AbstractTestCase::getBrowserBin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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