34
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
35
* POSSIBILITY OF SUCH DAMAGE.
36
36
*
37
- * @package PHPUnit_Selenium
38
- * @author Giorgio Sironi <[email protected] >
39
- * @copyright 2010-2013 Sebastian Bergmann <[email protected] >
40
- * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
41
37
* @link http://www.phpunit.de/
42
- * @since File available since Release 1.2.0
43
38
*/
44
39
45
40
namespace PHPUnit \Extensions \Selenium2TestCase ;
50
45
/**
51
46
* Driver for creating browser session with Selenium 2 (WebDriver API).
52
47
*
53
- * @package PHPUnit_Selenium
54
- * @author Giorgio Sironi <[email protected] >
55
- * @copyright 2010-2013 Sebastian Bergmann <[email protected] >
56
- * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
57
- * @version Release: @package_version@
58
48
* @link http://www.phpunit.de/
59
- * @since Class available since Release 1.2.0
60
49
*/
61
50
class Driver
62
51
{
@@ -65,23 +54,22 @@ class Driver
65
54
66
55
public function __construct (URL $ seleniumServerUrl , $ timeout = 60 )
67
56
{
68
- $ this ->seleniumServerUrl = $ seleniumServerUrl ;
57
+ $ this ->seleniumServerUrl = $ seleniumServerUrl ;
69
58
$ this ->seleniumServerRequestsTimeout = $ timeout ;
70
59
}
71
60
72
61
public function startSession (array $ desiredCapabilities , URL $ browserUrl )
73
62
{
74
- $ sessionCreation = $ this ->seleniumServerUrl ->descend ("/wd/hub/session " );
75
- $ response = $ this ->curl ('POST ' , $ sessionCreation , array (
76
- 'desiredCapabilities ' => $ desiredCapabilities
77
- ));
78
- $ sessionPrefix = $ response ->getURL ();
63
+ $ sessionCreation = $ this ->seleniumServerUrl ->descend ('/wd/hub/session ' );
64
+ $ response = $ this ->curl ('POST ' , $ sessionCreation , ['desiredCapabilities ' => $ desiredCapabilities ,]);
65
+ $ sessionPrefix = $ response ->getURL ();
79
66
80
67
$ timeouts = new Timeouts (
81
68
$ this ,
82
69
$ sessionPrefix ->descend ('timeouts ' ),
83
70
$ this ->seleniumServerRequestsTimeout * 1000
84
71
);
72
+
85
73
return new Session (
86
74
$ this ,
87
75
$ sessionPrefix ,
@@ -93,59 +81,66 @@ public function startSession(array $desiredCapabilities, URL $browserUrl)
93
81
/**
94
82
* Performs an HTTP request to the Selenium 2 server.
95
83
*
96
- * @param string $method 'GET'|'POST'|'DELETE'|...
84
+ * @param string $method 'GET'|'POST'|'DELETE'|...
97
85
* @param string $url
98
- * @param array $params JSON parameters for POST requests
86
+ * @param array $params JSON parameters for POST requests
99
87
*/
100
- public function curl ($ http_method , URL $ url , $ params = NULL )
88
+ public function curl ($ httpMethod , URL $ url , $ params = null )
101
89
{
102
90
$ curl = curl_init ($ url ->getValue ());
103
91
curl_setopt ($ curl , CURLOPT_TIMEOUT , $ this ->seleniumServerRequestsTimeout );
104
- curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , TRUE );
105
- curl_setopt ($ curl ,
106
- CURLOPT_HTTPHEADER ,
107
- array (
108
- 'Content-type: application/json;charset=UTF-8 ' ,
109
- 'Accept: application/json;charset=UTF-8 '
110
- ));
111
-
112
- if ($ http_method === 'POST ' ) {
113
- curl_setopt ($ curl , CURLOPT_POST , TRUE );
92
+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
93
+ curl_setopt (
94
+ $ curl ,
95
+ CURLOPT_HTTPHEADER ,
96
+ [
97
+ 'Content-type: application/json;charset=UTF-8 ' ,
98
+ 'Accept: application/json;charset=UTF-8 ' ,
99
+ ]
100
+ );
101
+
102
+ if ($ httpMethod === 'POST ' ) {
103
+ curl_setopt ($ curl , CURLOPT_POST , true );
114
104
if ($ params && is_array ($ params )) {
115
105
curl_setopt ($ curl , CURLOPT_POSTFIELDS , json_encode ($ params ));
116
106
} else {
117
107
curl_setopt ($ curl , CURLOPT_POSTFIELDS , '' );
118
108
}
119
- curl_setopt ($ curl , CURLOPT_FOLLOWLOCATION , TRUE );
120
- } else if ($ http_method == 'DELETE ' ) {
109
+
110
+ curl_setopt ($ curl , CURLOPT_FOLLOWLOCATION , true );
111
+ } elseif ($ httpMethod === 'DELETE ' ) {
121
112
curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , 'DELETE ' );
122
113
}
123
114
124
115
$ rawResponse = trim (curl_exec ($ curl ));
125
116
if (curl_errno ($ curl )) {
126
117
throw new NoSeleniumException (
127
118
'Error connection[ ' . curl_errno ($ curl ) . '] to ' .
128
- $ url ->getValue () . ': ' . curl_error ($ curl )
119
+ $ url ->getValue () . ': ' . curl_error ($ curl )
129
120
);
130
121
}
122
+
131
123
$ info = curl_getinfo ($ curl );
132
- if ($ info ['http_code ' ] == 0 ) {
124
+ if ($ info ['http_code ' ] === 0 ) {
133
125
throw new NoSeleniumException ();
134
126
}
135
- if ($ info ['http_code ' ] == 404 ) {
136
- throw new BadMethodCallException ("The command $ url is not recognized by the server. " );
127
+
128
+ if ($ info ['http_code ' ] === 404 ) {
129
+ throw new BadMethodCallException (sprintf ('The command %s is not recognized by the server. ' , $ url ));
137
130
}
131
+
138
132
if (($ info ['http_code ' ] >= 400 ) && ($ info ['http_code ' ] < 500 )) {
139
- throw new BadMethodCallException ("Something unexpected happened: ' $ rawResponse ' " );
133
+ throw new BadMethodCallException (sprintf ( "Something unexpected happened: '%s' " , $ rawResponse ) );
140
134
}
135
+
141
136
curl_close ($ curl );
142
- $ content = json_decode ($ rawResponse , TRUE );
137
+ $ content = json_decode ($ rawResponse , true );
143
138
144
139
if ($ content === null && json_last_error () !== JSON_ERROR_NONE ) {
145
140
throw new \PHPUnit \Extensions \Selenium2TestCase \Exception (
146
141
sprintf (
147
- "JSON decoding of remote response failed. \n" .
148
- "Error code: %d \n" .
142
+ "JSON decoding of remote response failed. \n" .
143
+ "Error code: %d \n" .
149
144
"The response: '%s' \n" ,
150
145
json_last_error (),
151
146
$ rawResponse
@@ -163,7 +158,7 @@ public function curl($http_method, URL $url, $params = NULL)
163
158
$ message = $ value ['message ' ];
164
159
}
165
160
166
- $ status = isset ( $ content ['status ' ]) ? $ content [ ' status ' ] : 0 ;
161
+ $ status = $ content ['status ' ] ?? 0 ;
167
162
if ($ status !== WebDriverException::Success) {
168
163
throw new WebDriverException ($ message , $ status );
169
164
}
@@ -173,8 +168,10 @@ public function curl($http_method, URL $url, $params = NULL)
173
168
174
169
public function execute (Command $ command )
175
170
{
176
- return $ this ->curl ($ command ->httpMethod (),
177
- $ command ->url (),
178
- $ command ->jsonParameters ());
171
+ return $ this ->curl (
172
+ $ command ->httpMethod (),
173
+ $ command ->url (),
174
+ $ command ->jsonParameters ()
175
+ );
179
176
}
180
177
}
0 commit comments