3 * Copyright 2012-2017 Anthon Pang. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @author Anthon Pang <apang@softwaredevelopment.ca>
20 * @author Fabrizio Branca <mail@fabrizio-branca.de>
23 namespace WebDriver\SauceLabs;
25 use WebDriver\ServiceFactory;
28 * WebDriver\SauceLabs\SauceRest class
47 * @param string $userId Your Sauce user name
48 * @param string $accessKey Your Sauce API key
50 public function __construct($userId, $accessKey)
52 $this->userId = $userId;
53 $this->accessKey = $accessKey;
57 * Execute Sauce Labs REST API command
59 * @param string $requestMethod HTTP request method
60 * @param string $url URL
61 * @param mixed $parameters Parameters
65 * @throws \WebDriver\Exception\CurlExec
67 * @see http://saucelabs.com/docs/saucerest
69 protected function execute($requestMethod, $url, $parameters = null)
71 $extraOptions = array(
72 CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
73 CURLOPT_USERPWD => $this->userId . ':' . $this->accessKey,
75 // don't verify SSL certificates
76 CURLOPT_SSL_VERIFYPEER => false,
77 CURLOPT_SSL_VERIFYHOST => false,
79 CURLOPT_HTTPHEADER => array('Expect:'),
80 CURLOPT_FAILONERROR => true,
83 $url = 'https://saucelabs.com/rest/v1/' . $url;
85 list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);
87 return json_decode($rawResult, true);
91 * Get account details: /rest/v1/users/:userId (GET)
93 * @param string $userId
97 public function getAccountDetails($userId)
99 return $this->execute('GET', 'users/' . $userId);
103 * Check account limits: /rest/v1/limits (GET)
107 public function getAccountLimits()
109 return $this->execute('GET', 'limits');
113 * Create new sub-account: /rest/v1/users/:userId (POST)
115 * For "partners", $accountInfo also contains 'plan' => (one of 'free', 'small', 'team', 'com', or 'complus')
117 * @param array $accountInfo array('username' => ..., 'password' => ..., 'name' => ..., 'email' => ...)
119 * @return array array('access_key' => ..., 'minutes' => ..., 'id' => ...)
121 public function createSubAccount($accountInfo)
123 return $this->execute('POST', 'users/' . $this->userId, $accountInfo);
127 * Update sub-account service plan: /rest/v1/users/:userId/subscription (POST)
129 * @param string $userId User ID
130 * @param string $plan Plan
134 public function updateSubAccount($userId, $plan)
136 return $this->execute('POST', 'users/' . $userId . '/subscription', array('plan' => $plan));
140 * Unsubscribe a sub-account: /rest/v1/users/:userId/subscription (DELETE)
142 * @param string $userId User ID
146 public function unsubscribeSubAccount($userId)
148 return $this->execute('DELETE', 'users/' . $userId . '/subscription');
152 * Get current account activity: /rest/v1/:userId/activity (GET)
156 public function getActivity()
158 return $this->execute('GET', $this->userId . '/activity');
162 * Get historical account usage: /rest/v1/:userId/usage (GET)
164 * @param string $start Optional start date YYYY-MM-DD
165 * @param string $end Optional end date YYYY-MM-DD
169 public function getUsage($start = null, $end = null)
171 $query = http_build_query(array(
176 return $this->execute('GET', $this->userId . '/usage' . (strlen($query) ? '?' . $query : ''));
180 * Get jobs: /rest/v1/:userId/jobs (GET)
182 * @param boolean $full
186 public function getJobs($full = null)
188 $query = http_build_query(array(
189 'full' => (isset($full) && $full) ? 'true' : null,
192 return $this->execute('GET', $this->userId . '/jobs' . (strlen($query) ? '?' . $query : ''));
196 * Get full information for job: /rest/v1/:userId/jobs/:jobId (GET)
198 * @param string $jobId
202 public function getJob($jobId)
204 return $this->execute('GET', $this->userId . '/jobs/' . $jobId);
208 * Update existing job: /rest/v1/:userId/jobs/:jobId (PUT)
210 * @param string $jobId Job ID
211 * @param array $jobInfo Job information
215 public function updateJob($jobId, $jobInfo)
217 return $this->execute('PUT', $this->userId . '/jobs/' . $jobId, $jobInfo);
221 * Stop job: /rest/v1/:userId/jobs/:jobId/stop (PUT)
223 * @param string $jobId
227 public function stopJob($jobId)
229 return $this->execute('PUT', $this->userId . '/jobs/' . $jobId . '/stop');
233 * Delete job: /rest/v1/:userId/jobs/:jobId (DELETE)
235 * @param string $jobId
239 public function deleteJob($jobId)
241 return $this->execute('DELETE', $this->userId . '/jobs/' . $jobId);
245 * Get running tunnels for a given user: /rest/v1/:userId/tunnels (GET)
249 public function getTunnels()
251 return $this->execute('GET', $this->userId . '/tunnels');
255 * Get full information for a tunnel: /rest/v1/:userId/tunnels/:tunnelId (GET)
257 * @param string $tunnelId
261 public function getTunnel($tunnelId)
263 return $this->execute('GET', $this->userId . '/tunnels/' . $tunnelId);
267 * Shut down a tunnel: /rest/v1/:userId/tunnels/:tunnelId (DELETE)
269 * @param string $tunnelId
273 public function shutdownTunnel($tunnelId)
275 return $this->execute('DELETE', $this->userId . '/tunnels/' . $tunnelId);
279 * Get current status of Sauce Labs' services: /rest/v1/info/status (GET)
281 * @return array array('wait_time' => ..., 'service_operational' => ..., 'status_message' => ...)
283 public function getStatus()
285 return $this->execute('GET', 'info/status');
289 * Get currently supported browsers: /rest/v1/info/browsers (GET)
291 * @param string $termination Optional termination (one of "all", "selenium-rc", or "webdriver')
295 public function getBrowsers($termination = false)
298 return $this->execute('GET', 'info/browsers/' . $termination);
301 return $this->execute('GET', 'info/browsers');
305 * Get number of tests executed so far on Sauce Labs: /rest/v1/info/counter (GET)
309 public function getCounter()
311 return $this->execute('GET', 'info/counter');