3434 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3535 * POSSIBILITY OF SUCH DAMAGE.
3636 *
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
4137 * @link http://www.phpunit.de/
42- * @since File available since Release 1.2.0
4338 */
4439
4540namespace PHPUnit \Extensions \Selenium2TestCase ;
5045/**
5146 * Driver for creating browser session with Selenium 2 (WebDriver API).
5247 *
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@
5848 * @link http://www.phpunit.de/
59- * @since Class available since Release 1.2.0
6049 */
6150class Driver
6251{
@@ -65,23 +54,22 @@ class Driver
6554
6655 public function __construct (URL $ seleniumServerUrl , $ timeout = 60 )
6756 {
68- $ this ->seleniumServerUrl = $ seleniumServerUrl ;
57+ $ this ->seleniumServerUrl = $ seleniumServerUrl ;
6958 $ this ->seleniumServerRequestsTimeout = $ timeout ;
7059 }
7160
7261 public function startSession (array $ desiredCapabilities , URL $ browserUrl )
7362 {
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 ();
7966
8067 $ timeouts = new Timeouts (
8168 $ this ,
8269 $ sessionPrefix ->descend ('timeouts ' ),
8370 $ this ->seleniumServerRequestsTimeout * 1000
8471 );
72+
8573 return new Session (
8674 $ this ,
8775 $ sessionPrefix ,
@@ -93,59 +81,66 @@ public function startSession(array $desiredCapabilities, URL $browserUrl)
9381 /**
9482 * Performs an HTTP request to the Selenium 2 server.
9583 *
96- * @param string $method 'GET'|'POST'|'DELETE'|...
84+ * @param string $method 'GET'|'POST'|'DELETE'|...
9785 * @param string $url
98- * @param array $params JSON parameters for POST requests
86+ * @param array $params JSON parameters for POST requests
9987 */
100- public function curl ($ http_method , URL $ url , $ params = NULL )
88+ public function curl ($ httpMethod , URL $ url , $ params = null )
10189 {
10290 $ curl = curl_init ($ url ->getValue ());
10391 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 );
114104 if ($ params && is_array ($ params )) {
115105 curl_setopt ($ curl , CURLOPT_POSTFIELDS , json_encode ($ params ));
116106 } else {
117107 curl_setopt ($ curl , CURLOPT_POSTFIELDS , '' );
118108 }
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 ' ) {
121112 curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , 'DELETE ' );
122113 }
123114
124115 $ rawResponse = trim (curl_exec ($ curl ));
125116 if (curl_errno ($ curl )) {
126117 throw new NoSeleniumException (
127118 'Error connection[ ' . curl_errno ($ curl ) . '] to ' .
128- $ url ->getValue () . ': ' . curl_error ($ curl )
119+ $ url ->getValue () . ': ' . curl_error ($ curl )
129120 );
130121 }
122+
131123 $ info = curl_getinfo ($ curl );
132- if ($ info ['http_code ' ] == 0 ) {
124+ if ($ info ['http_code ' ] === 0 ) {
133125 throw new NoSeleniumException ();
134126 }
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 ));
137130 }
131+
138132 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 ) );
140134 }
135+
141136 curl_close ($ curl );
142- $ content = json_decode ($ rawResponse , TRUE );
137+ $ content = json_decode ($ rawResponse , true );
143138
144139 if ($ content === null && json_last_error () !== JSON_ERROR_NONE ) {
145140 throw new \PHPUnit \Extensions \Selenium2TestCase \Exception (
146141 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" .
149144 "The response: '%s' \n" ,
150145 json_last_error (),
151146 $ rawResponse
@@ -163,7 +158,7 @@ public function curl($http_method, URL $url, $params = NULL)
163158 $ message = $ value ['message ' ];
164159 }
165160
166- $ status = isset ( $ content ['status ' ]) ? $ content [ ' status ' ] : 0 ;
161+ $ status = $ content ['status ' ] ?? 0 ;
167162 if ($ status !== WebDriverException::Success) {
168163 throw new WebDriverException ($ message , $ status );
169164 }
@@ -173,8 +168,10 @@ public function curl($http_method, URL $url, $params = NULL)
173168
174169 public function execute (Command $ command )
175170 {
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+ );
179176 }
180177}
0 commit comments