Best SeLion code snippet using com.paypal.selion.utils.SauceLabsRestApi.getEntity
Source:SauceLabsRestApi.java
...55 entity.append(input);56 }57 }58 }59 public String getEntity() {60 return entity.toString();61 }62 public JsonObject getEntityAsJsonObject() {63 return new JsonParser().parse(getEntity()).getAsJsonObject();64 }65 public int getStatus() {66 return status;67 }68 @Override69 public String toString() {70 return "[" + getStatus() + ", " + getEntity() + "]";71 }72 }73 /**74 * Creates a new instance. Uses the current (@link SauceConfigReader#getInstance()} to initialize sauce connection75 * details (url, authKey, etc) that will be used.76 */77 public SauceLabsRestApi() {78 SauceConfigReader reader = SauceConfigReader.getInstance();79 sauceAuthenticationKey = reader.getAuthenticationKey();80 sauceUrl = reader.getURL();81 sauceTimeout = reader.getSauceTimeout();82 sauceRetryCount = reader.getSauceRetry();83 accountCache = new ConcurrentHashMap<String, Boolean>();84 }85 private SauceLabsHttpResponse doSauceRequest(String path) throws IOException {86 return doSauceRequest(new URL(sauceUrl + path), sauceAuthenticationKey, sauceTimeout, sauceRetryCount);87 }88 private SauceLabsHttpResponse doSauceRequest(URL url, String authKey, int timeout, int retry) throws IOException {89 LOGGER.entering(url.toExternalForm());90 HttpURLConnection conn = null;91 SauceLabsHttpResponse result = new SauceLabsHttpResponse();92 int numRetriesOnFailure = 0;93 do {94 try {95 conn = (HttpURLConnection) url.openConnection();96 conn.setConnectTimeout(timeout);97 conn.setReadTimeout(timeout);98 conn.setRequestMethod("GET");99 conn.setRequestProperty("Accept", "application/json");100 conn.setRequestProperty("Authorization", "Basic " + authKey);101 result = new SauceLabsHttpResponse(conn);102 } catch (IOException e) {103 if (numRetriesOnFailure == retry) {104 throw e;105 }106 } finally {107 if (conn != null) {108 conn.disconnect();109 }110 }111 } while ((result.getStatus() != HttpStatus.SC_OK) && (numRetriesOnFailure++ < retry));112 LOGGER.exiting(result);113 return result;114 }115 /**116 * Get the total number of test cases running in sauce labs for the primary account.117 *118 * @return number of test cases running, <code>-1</code> on failure calling sauce labs.119 */120 public int getNumberOfTCRunning() {121 LOGGER.entering();122 int tcRunning = -1;123 try {124 SauceLabsHttpResponse result = doSauceRequest("/activity");125 JsonObject obj = result.getEntityAsJsonObject();126 tcRunning = obj.getAsJsonObject("totals").get("all").getAsInt();127 } catch (JsonSyntaxException | IllegalStateException | IOException e) {128 LOGGER.log(Level.SEVERE, "Unable to get number of test cases running.", e);129 }130 LOGGER.exiting(tcRunning);131 return tcRunning;132 }133 /**134 * Get the number of test cases running in sauce labs for the sauce labs sub-account/user135 *136 * @param user137 * id of the sub-account138 * @return number of test case running or <code>-1</code> on failure139 */140 public int getNumberOfTCRunningForSubAccount(String user) {141 LOGGER.entering(user);142 int tcRunning = -1;143 try {144 SauceLabsHttpResponse result = doSauceRequest("/activity");145 JsonObject obj = result.getEntityAsJsonObject();146 tcRunning = obj.getAsJsonObject("subaccounts").getAsJsonObject(user).get("all").getAsInt();147 } catch (JsonSyntaxException | IllegalStateException | IOException e) {148 LOGGER.log(Level.SEVERE, "Unable to get number of test cases running for sub-account.", e);149 }150 LOGGER.exiting(tcRunning);151 return tcRunning;152 }153 /**154 * Get the maximum number of test case that can run in parallel for the primary account.155 *156 * @return maximum number of test case or <code>-1</code> on failure calling sauce labs157 */158 public int getMaxConcurrency() {159 LOGGER.entering();160 if (maxTestCase == -1) {161 try {162 SauceLabsHttpResponse result = doSauceRequest("/limits");163 JsonObject obj = result.getEntityAsJsonObject();164 maxTestCase = obj.get("concurrency").getAsInt();165 } catch (JsonSyntaxException | IllegalStateException | IOException e) {166 LOGGER.log(Level.SEVERE, "Unable to get max concurrency.", e);167 }168 }169 LOGGER.exiting(maxTestCase);170 return maxTestCase;171 }172 private void addToAccountCache(String md5, boolean valid) {173 if (accountCache.size() >= MAX_CACHE) {174 // don't let the cache grow more than MAX_CACHE175 accountCache.clear();176 }177 accountCache.put(md5, valid);...
Source:SauceLabsRestApiTest.java
...30 "{\"subaccounts\": {\"foobar\": {\"all\": 1}},\"totals\": {\"all\": 2},\"concurrency\": 5}";31 @Test32 public void getMaxConcurrency() throws Exception {33 SauceLabsHttpResponse mockHttpResponse = mock(SauceLabsHttpResponse.class);34 doReturn(mockApiResult).when(mockHttpResponse, "getEntity");35 when(mockHttpResponse.getEntityAsJsonObject()).thenCallRealMethod();36 SauceLabsRestApi apiMock = mock(SauceLabsRestApi.class);37 Whitebox.setInternalState(apiMock, "maxTestCase", -1);38 doReturn(mockHttpResponse).when(apiMock, "doSauceRequest", Mockito.anyString());39 when(apiMock.getMaxConcurrency()).thenCallRealMethod();40 assertEquals(apiMock.getMaxConcurrency(), 5);41 }42 @Test43 public void getNumberOfTCRunningForSubAccount() throws Exception {44 SauceLabsHttpResponse mockHttpResponse = mock(SauceLabsHttpResponse.class);45 doReturn(mockApiResult).when(mockHttpResponse, "getEntity");46 when(mockHttpResponse.getEntityAsJsonObject()).thenCallRealMethod();47 SauceLabsRestApi apiMock = mock(SauceLabsRestApi.class);48 doReturn(mockHttpResponse).when(apiMock, "doSauceRequest", Mockito.anyString());49 when(apiMock.getNumberOfTCRunningForSubAccount(Mockito.anyString())).thenCallRealMethod();50 assertEquals(apiMock.getNumberOfTCRunningForSubAccount("foobar"), 1);51 }52 @Test53 public void getNumberOfTCRunning() throws Exception {54 SauceLabsHttpResponse mockHttpResponse = mock(SauceLabsHttpResponse.class);55 doReturn(mockApiResult).when(mockHttpResponse, "getEntity");56 when(mockHttpResponse.getEntityAsJsonObject()).thenCallRealMethod();57 SauceLabsRestApi apiMock = mock(SauceLabsRestApi.class);58 doReturn(mockHttpResponse).when(apiMock, "doSauceRequest", Mockito.anyString());59 when(apiMock.getNumberOfTCRunning()).thenCallRealMethod();60 assertEquals(apiMock.getNumberOfTCRunning(), 2);61 }62 @Test63 public void isAuthenticated() throws Exception {64 SauceLabsHttpResponse mockHttpResponse = mock(SauceLabsHttpResponse.class);65 doReturn(HttpStatus.SC_OK).when(mockHttpResponse, "getStatus");66 SauceLabsRestApi apiMock = mock(SauceLabsRestApi.class);67 Whitebox.setInternalState(apiMock, "accountCache", new HashMap<String, Boolean>());68 doReturn(mockHttpResponse).when(apiMock, "doSauceRequest", Mockito.any(), Mockito.anyString(),69 Mockito.anyInt(), Mockito.anyInt());70 when(apiMock.isAuthenticated(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();...
getEntity
Using AI Code Generation
1SauceLabsRestApi api = new SauceLabsRestApi();2System.out.println(entity);3SauceLabsRestApi api = new SauceLabsRestApi();4System.out.println(entity);5SauceLabsRestApi api = new SauceLabsRestApi();6System.out.println(entity);7SauceLabsRestApi api = new SauceLabsRestApi();8System.out.println(entity);
getEntity
Using AI Code Generation
1import com.paypal.selion.utils.SauceLabsRestApi;2import org.json.JSONObject;3public class 3 {4 public static void main(String[] args) throws Exception {5 String jobID = args[0];6 JSONObject job = SauceLabsRestApi.getEntity(jobID);7 System.out.println(job);8 }9}10{11}
getEntity
Using AI Code Generation
1package com.paypal.selion;2import java.io.IOException;3import org.json.JSONException;4import org.json.JSONObject;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.testng.Assert;7import org.testng.annotations.Test;8import com.paypal.selion.annotations.WebTest;9import com.paypal.selion.platform.grid.Grid;10import com.paypal.selion.platform.grid.browsercapabilities.DefaultCapabilitiesBuilder;11import com.paypal.selion.platform.utilities.WebDriverWaitUtils;12import com.paypal.selion.testcomponents.BasicPageImpl;13import com.paypal.selion.testcomponents.HomePageImpl;14import com.paypal.selion.testcomponents.SeLionSauceLabsPageImpl;15import com.paypal.selion.testcomponents.SeLionSauceLabsPageImpl.SeLionSauceLabsPageControls;16import com.paypal.selion.utils.SauceLabsRestApi;17public class SauceLabsRestApiTest {18 public void testSauceLabsRestApi() throws IOException, JSONException {19 DesiredCapabilities capabilities = new DefaultCapabilitiesBuilder().getCapabilities();20 capabilities.setCapability("name", "SeLion SauceLabsRestApi test");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!