Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.testfabrik.webmate.javasdk.selenium;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.testfabrik.webmate.javasdk.*;
import com.testfabrik.webmate.javasdk.browsersession.BrowserSessionId;
import com.testfabrik.webmate.javasdk.testmgmt.Test;
import com.testfabrik.webmate.javasdk.user.UserId;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
/**
* Facade to webmate's Selenium subsystem.
*/
public class SeleniumServiceClient {
private SeleniumServiceApiClient apiClient;
private static final Logger LOG = LoggerFactory.getLogger(SeleniumServiceClient.class);
private static class SeleniumServiceApiClient extends WebmateApiClient {
private static final UriTemplate getSeleniumsessionTemplate = new UriTemplate("/seleniumsession/${sessionId}");
private static final UriTemplate getSeleniumsessionForBrowserSessionTemplate = new UriTemplate("/seleniumsession/");
private static final UriTemplate getSeleniumCapabilitiesForProjectTemplate = new UriTemplate("/projects/${projectId}/selenium/capabilities");
private static final UriTemplate getSeleniumsessionsForProjectTemplate = new UriTemplate("/projects/${projectId}/seleniumsession");
private static final UriTemplate getSeleniumsessionIdsForProjectTemplate = new UriTemplate("/projects/${projectId}/seleniumsession/id");
private static final UriTemplate stopSeleniumsessionTemplate = new UriTemplate("/seleniumsession/${sessionId}/stop");
public SeleniumServiceApiClient(WebmateAuthInfo authInfo, WebmateEnvironment environment) {
super(authInfo, environment);
}
public SeleniumServiceApiClient(WebmateAuthInfo authInfo, WebmateEnvironment environment, HttpClientBuilder httpClientBuilder) {
super(authInfo, environment, httpClientBuilder);
}
/**
* Helper method to build a selenium session from json.
*/
private SeleniumSession buildSeleniumSessionFromJson(JsonNode sessionJson) {
ObjectMapper om = new ObjectMapper();
try {
return om.treeToValue(sessionJson, SeleniumSession.class);
} catch (JsonProcessingException e) {
throw new WebmateApiClientException("Error deserializing SeleniumSession data: " + e.getMessage());
}
}
/**
* Helper method to build a selenium capability from json.
*/
private SeleniumCapability buildSeleniumCapabilityFromJson(JsonNode capabilityJson) {
return new SeleniumCapability(
BrowserType.getEnum(capabilityJson.at("/browserName").asText()),
capabilityJson.at("/version").asText(),
capabilityJson.at("/platform").asText(),
capabilityJson.at("/supportsProxy").asBoolean(),
capabilityJson.at("/browserlanguage").asText()
);
}
private Browser buildBrowserFromJson(JsonNode browserJson) {
return new Browser(
BrowserType.getEnum(browserJson.at("/browserType").asText()),
browserJson.at("/version").asText(),
browserJson.at("/platform").asText()
);
}
public SeleniumSession getSeleniumsession(WebmateSeleniumSessionId sessionId) {
ApiResponse response = sendGET(getSeleniumsessionTemplate, ImmutableMap.of("sessionId", sessionId.toString()));
Optional optHttpResponse = response.getOptHttpResponse();
if (!optHttpResponse.isPresent()) {
throw new WebmateApiClientException("The requested Seleniumsession could not be found");
}
ObjectMapper om = new ObjectMapper();
try {
JsonNode sessionJson = om.readTree(EntityUtils.toString(optHttpResponse.get().getEntity()));
return buildSeleniumSessionFromJson(sessionJson);
}
catch (IOException e) {
throw new WebmateApiClientException("The Seleniumsession could not be retrieved");
}
}
public SeleniumSession getSeleniumsessionForBrowserSession(BrowserSessionId browserSessionId) {
ApiResponse response = sendGET(getSeleniumsessionForBrowserSessionTemplate,
ImmutableMap.of(),
ImmutableList.of(new BasicNameValuePair("expeditionId", browserSessionId.toString())));
Optional optHttpResponse = response.getOptHttpResponse();
if (!optHttpResponse.isPresent()) {
throw new WebmateApiClientException("There is no Seleniumsession associated with browsersession " + browserSessionId);
}
ObjectMapper mapper = JacksonMapper.getInstance();
SeleniumSession session;
try {
String testJson = EntityUtils.toString(optHttpResponse.get().getEntity());
session = mapper.readValue(testJson, SeleniumSession.class);
} catch (IOException e) {
throw new WebmateApiClientException("Error reading Selenium session: " + e.getMessage(), e);
}
return session;
}
public Collection getSeleniumCapabilitiesForProject(ProjectId projectId) {
Map params = ImmutableMap.of("projectId", projectId.toString());
ApiResponse apiResponse = sendGET(getSeleniumCapabilitiesForProjectTemplate, params);
Optional optHttpResponse = apiResponse.getOptHttpResponse();
if (!optHttpResponse.isPresent()) {
throw new WebmateApiClientException("There has been an error retrieving the Selenium capabilities for the project");
}
ArrayList capabilities = new ArrayList<>();
ObjectMapper om = new ObjectMapper();
try {
JsonNode capabilityListJson = om.readTree(EntityUtils.toString(optHttpResponse.get().getEntity()));
Iterator capabilityListIter = capabilityListJson.iterator();
while (capabilityListIter.hasNext()) {
JsonNode capabilityJson = capabilityListIter.next();
capabilities.add(buildSeleniumCapabilityFromJson(capabilityJson));
}
} catch (IOException e) {
throw new WebmateApiClientException("The list of Selenium capabilities could not be retrieved");
}
return capabilities;
}
public Collection getSeleniumsessionsForProject(ProjectId projectId, WebmateSeleniumSessionId after, Integer count, String state) {
ArrayList queryParams = new ArrayList<>();
if (after != null) {
queryParams.add(new BasicNameValuePair("after", after.toString()));
}
if (count != null) {
queryParams.add(new BasicNameValuePair("count", count.toString()));
}
if (state != null) {
queryParams.add(new BasicNameValuePair("state", state));
}
Map params = ImmutableMap.of("projectId", projectId.toString());
ApiResponse apiResponse;
if (!queryParams.isEmpty()) {
apiResponse = sendGET(getSeleniumsessionsForProjectTemplate, params, queryParams);
}
else {
apiResponse = sendGET(getSeleniumsessionsForProjectTemplate, params);
}
ArrayList sessions = new ArrayList<>();
Optional optHttpResponse = apiResponse.getOptHttpResponse();
if (!optHttpResponse.isPresent()) {
throw new WebmateApiClientException("There has been an error retrieving the Selenium sessions for the project");
}
ObjectMapper om = new ObjectMapper();
try {
JsonNode sessionListJson = om.readTree(EntityUtils.toString(optHttpResponse.get().getEntity()));
Iterator sessionListIter = sessionListJson.iterator();
while (sessionListIter.hasNext()) {
JsonNode sessionJson = sessionListIter.next();
sessions.add(buildSeleniumSessionFromJson(sessionJson));
}
} catch (IOException e) {
throw new WebmateApiClientException("The List of Selenium sessions could not be retrieved");
}
return sessions;
}
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, WebmateSeleniumSessionId after, Integer count, String state) {
ArrayList queryParams = new ArrayList<>();
if (after != null) {
queryParams.add(new BasicNameValuePair("after", after.toString()));
}
if (count != null) {
queryParams.add(new BasicNameValuePair("count", count.toString()));
}
if (state != null) {
queryParams.add(new BasicNameValuePair("state", state));
}
Map params = ImmutableMap.of("projectId", projectId.toString());
ApiResponse apiResponse;
if (!queryParams.isEmpty()) {
apiResponse = sendGET(getSeleniumsessionIdsForProjectTemplate, params, queryParams);
}
else {
apiResponse = sendGET(getSeleniumsessionIdsForProjectTemplate, params);
}
ArrayList ids = new ArrayList<>();
Optional optHttpResponse = apiResponse.getOptHttpResponse();
if (!optHttpResponse.isPresent()) {
throw new WebmateApiClientException("There has been an error retrieving the Selenium session IDs for the project");
}
ObjectMapper om = new ObjectMapper();
try {
JsonNode idListJson = om.readTree(EntityUtils.toString(optHttpResponse.get().getEntity()));
Iterator idListIter = idListJson.iterator();
while (idListIter.hasNext()) {
JsonNode idJson = idListIter.next();
ids.add(new WebmateSeleniumSessionId(UUID.fromString(idJson.asText())));
}
} catch (IOException e) {
throw new WebmateApiClientException("The List of Selenium session ids could not be retrieved");
}
return ids;
}
public void stopSeleniumsession(WebmateSeleniumSessionId sessionId) {
Map params = ImmutableMap.of("sessionId", sessionId.toString());
// As the sendPOST method already checks for errors if the return code is not 200, we do not need to do it here.
sendPOST(stopSeleniumsessionTemplate, params);
}
}
/**
* Create a SeleniumServiceClient from a WebmateAPISession.
*
* @param session The WebmateApiSession the DeviceClient is supposed to be based on.
*/
public SeleniumServiceClient(WebmateAPISession session) {
this.apiClient = new SeleniumServiceApiClient(session.authInfo, session.environment);
}
/**
* Create a SeleniumServiceClient from a WebmateAPISession and a HttpClientBuilder.
*
* @param session The WebmateApiSession the DeviceClient is supposed to be based on.
* @param httpClientBuilder The HttpClientBuilder that is used for building the underlying connection.
*/
public SeleniumServiceClient(WebmateAPISession session, HttpClientBuilder httpClientBuilder) {
this.apiClient = new SeleniumServiceApiClient(session.authInfo, session.environment, httpClientBuilder);
}
/**
* Get a Selenium session by ID.
*
* @param sessionId ID of the selenium session to retrieve
* @return SeleniumSession with the requested session ID
* @throws WebmateApiClientException if a HTTP error occurred or the session could not be found/retrieved
*/
public SeleniumSession getSeleniumsession(WebmateSeleniumSessionId sessionId) throws WebmateApiClientException {
return this.apiClient.getSeleniumsession(sessionId);
}
/**
* Get a Selenium session for browser session.
*
* @param browserSessionId ID of the browser session that is associated with the Selenium session to be returned.
* @return SeleniumSession with the requested session ID
* @throws WebmateApiClientException if a HTTP error occurred or the session could not be found/retrieved
*/
public SeleniumSession getSeleniumSessionForBrowserSession(BrowserSessionId browserSessionId) throws WebmateApiClientException {
return this.apiClient.getSeleniumsessionForBrowserSession(browserSessionId);
}
/**
* Get all Selenium capabilities for a project by project ID.
*
* @param projectId ID of the project of which the capabilities should be retrieved
* @return List of all Selenium capabilities in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium capabilities could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumCapabilitiesForProject(ProjectId projectId) throws WebmateApiClientException {
return this.apiClient.getSeleniumCapabilitiesForProject(projectId);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given Id.
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, WebmateSeleniumSessionId after, Integer count, String state) throws WebmateApiClientException {
return this.apiClient.getSeleniumsessionsForProject(projectId, after, count, state);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, null, null, null);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given Id.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, WebmateSeleniumSessionId after) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, after, null, null);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, int count) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, null, count, null);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, String state) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, null, null, state);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given Id.
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, WebmateSeleniumSessionId after, int count) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, after, count, null);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given Id.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, WebmateSeleniumSessionId after, String state) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, after, null, state);
}
/**
* Get all Selenium sessions for a project by project ID.
*
* @param projectId The ID of the project of which Selenium sessions shall be retrieved
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium sessions in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium sessions could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionsForProject(ProjectId projectId, int count, String state) throws WebmateApiClientException {
return getSeleniumsessionsForProject(projectId, null, count, state);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given ID.
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, WebmateSeleniumSessionId after, Integer count, String state) throws WebmateApiClientException {
return this.apiClient.getSeleniumsessionIdsForProject(projectId, after, count, state);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, null, null, null);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given ID.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, WebmateSeleniumSessionId after) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, after, null, null);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, int count) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, null, count, null);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, String state) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, null, null, state);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given ID.
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, WebmateSeleniumSessionId after, int count) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, after, count, null);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param after (optional) An ID of a SeleniumSession, the call (and all of its other query parameters) will only take Sessions into account that were created after the Session with the given ID.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, WebmateSeleniumSessionId after, String state) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, after, null, state);
}
/**
* Get all Selenium session IDs for a project by project ID.
*
* @param projectId The ID of the project of which Selenium session IDs shall be retrieved
* @param count (optional) The length of the output is restricted to the given integer, remaining (aka older) Sessions are not returned. Use of this parameter is highly recommended to avoid a gigantic result that needs to be send over the network.
* @param state (optional) Only SeleniumSessions in the given state are considered and returned by the call, all other Sessions are filtered out.
* @return (Filtered) List of Selenium session IDs in the given project (Actual type: ArrayList)
* @throws WebmateApiClientException if a HTTP error occurred or the Selenium session IDs could not be retrieved (e.g. due to missing permissions)
*/
public Collection getSeleniumsessionIdsForProject(ProjectId projectId, int count, String state) throws WebmateApiClientException {
return getSeleniumsessionIdsForProject(projectId, null, count, state);
}
/**
* Stop a Selenium session by ID.
*
* @param sessionId ID of the Selenium session to be stopped
* @throws WebmateApiClientException if an HTTP error occurred or the Selenium session could not be found (e.g. due to missing permissions, or wrong ID)
*/
public void stopSeleniumsession(WebmateSeleniumSessionId sessionId) throws WebmateApiClientException{
this.apiClient.stopSeleniumsession(sessionId);
}
}