diff --git a/src/main/java/com/browserstack/automate/Automate.java b/src/main/java/com/browserstack/automate/Automate.java index b5040f3..d3ae30f 100644 --- a/src/main/java/com/browserstack/automate/Automate.java +++ b/src/main/java/com/browserstack/automate/Automate.java @@ -42,6 +42,18 @@ Session updateSessionStatus(String sessionId, String getSessionVideo(String sessionId) throws SessionNotFound, AutomateException; + String getSessionConsoleLogs(String sessionId) throws SessionNotFound, AutomateException; + + String getSessionConsoleLogs(Session session) throws AutomateException; + + String getSessionHARLogs(String sessionId) throws SessionNotFound, AutomateException; + + String getSessionHARLogs(Session session) throws AutomateException; + + String getSessionAppiumLogs(String sessionId) throws SessionNotFound, AutomateException; + + String getSessionAppiumLogs(Session session) throws AutomateException; + boolean deleteSession(String sessionId) throws SessionNotFound, AutomateException; String recycleKey() throws AutomateException; diff --git a/src/main/java/com/browserstack/automate/AutomateClient.java b/src/main/java/com/browserstack/automate/AutomateClient.java index 7d13d3c..62ecb1f 100644 --- a/src/main/java/com/browserstack/automate/AutomateClient.java +++ b/src/main/java/com/browserstack/automate/AutomateClient.java @@ -18,7 +18,7 @@ */ public final class AutomateClient extends BrowserStackClient implements Automate { - private static final String BASE_URL = "https://www.browserstack.com/automate"; + private static final String BASE_URL = "https://api.browserstack.com/automate"; private static final String CACHE_KEY_BROWSERS = "browsers"; /** @@ -434,6 +434,117 @@ public final String getSessionVideo(final String sessionId) return getSession(sessionId).getVideoUrl(); } + /** + * Fetches the console logs for a session. + * + * @param sessionId ID that uniquely identifies a session. + * @return Console logs for the session. + * @throws SessionNotFound + * @throws AutomateException + */ + public final String getSessionConsoleLogs(final String sessionId) throws SessionNotFound, AutomateException { + return getSessionConsoleLogs(getSession(sessionId)); + } + + /** + * Fetches the console logs for a session. + * + * @param session {@link Session} for which to retrieve logs. + * @return Console logs for the session. + * @throws AutomateException + */ + public final String getSessionConsoleLogs(final Session session) throws AutomateException { + if (session == null) { + throw new AutomateException("Invalid session", 400); + } + + if (session.getBrowserConsoleLogsUrl() == null) { + throw new AutomateException("Session logs not found", 404); + } + + try { + BrowserStackRequest request = newRequest(Method.GET, session.getBrowserConsoleLogsUrl(), false); + request.getHttpRequest().getHeaders().setAccept("*/*"); + return request.asString(); + } catch (BrowserStackException e) { + throw new AutomateException(e); + } + } + + /** + * Fetches the HAR logs for a session. + * + * @param sessionId ID that uniquely identifies a session. + * @return HAR logs for the session. + * @throws SessionNotFound + * @throws AutomateException + */ + public final String getSessionHARLogs(final String sessionId) throws SessionNotFound, AutomateException { + return getSessionHARLogs(getSession(sessionId)); + } + + /** + * Fetches the HAR logs for a session. + * + * @param session {@link Session} for which to retrieve logs. + * @return HAR logs for the session. + * @throws AutomateException + */ + public final String getSessionHARLogs(final Session session) throws AutomateException { + if (session == null) { + throw new AutomateException("Invalid session", 400); + } + + if (session.getHarLogsUrl() == null) { + throw new AutomateException("Session logs not found", 404); + } + + try { + BrowserStackRequest request = newRequest(Method.GET, session.getHarLogsUrl(), false); + request.getHttpRequest().getHeaders().setAccept("*/*"); + return request.asString(); + } catch (BrowserStackException e) { + throw new AutomateException(e); + } + } + + /** + * Fetches the Appium logs for a session. + * + * @param sessionId ID that uniquely identifies a session. + * @return Appium logs for the session. + * @throws SessionNotFound + * @throws AutomateException + */ + public final String getSessionAppiumLogs(final String sessionId) throws SessionNotFound, AutomateException { + return getSessionAppiumLogs(getSession(sessionId)); + } + + /** + * Fetches the Appium logs for a session. + * + * @param session {@link Session} for which to retrieve logs. + * @return Appium logs for the session. + * @throws AutomateException + */ + public final String getSessionAppiumLogs(final Session session) throws AutomateException { + if (session == null) { + throw new AutomateException("Invalid session", 400); + } + + if (session.getAppiumLogsUrl() == null) { + throw new AutomateException("Session logs not found", 404); + } + + try { + BrowserStackRequest request = newRequest(Method.GET, session.getAppiumLogsUrl(), false); + request.getHttpRequest().getHeaders().setAccept("*/*"); + return request.asString(); + } catch (BrowserStackException e) { + throw new AutomateException(e); + } + } + /** * Deletes the session identified by the supplied identifier. * diff --git a/src/main/java/com/browserstack/automate/model/Session.java b/src/main/java/com/browserstack/automate/model/Session.java index 55abb73..a6e3c00 100644 --- a/src/main/java/com/browserstack/automate/model/Session.java +++ b/src/main/java/com/browserstack/automate/model/Session.java @@ -6,8 +6,10 @@ import com.browserstack.automate.exception.SessionNotFound; import com.browserstack.client.BrowserStackClient; import com.browserstack.client.model.BrowserStackObject; +import com.browserstack.client.util.Tools; import com.fasterxml.jackson.annotation.*; +import javax.tools.Tool; import java.util.HashMap; import java.util.Map; @@ -62,6 +64,15 @@ public class Session extends BrowserStackObject { @JsonProperty("name") private String name; + @JsonProperty("browser_console_logs_url") + private String browserConsoleLogsUrl; + + @JsonProperty("har_logs_url") + private String harLogsUrl; + + @JsonProperty("appium_logs_url") + private String appiumLogsUrl; + @JsonIgnore private Map additionalProperties = new HashMap(); @@ -93,13 +104,37 @@ public final Session updateStatus(final SessionStatus sessionStatus) throws Sess } public final String getLogs() throws AutomateException { - if (logUrl == null) { + if (Tools.isStringEmpty(logUrl)) { throw new AutomateException("Session logs not found", 404); } return ((AutomateClient) getClient()).getSessionLogs(this); } + public final String getConsoleLogs() throws AutomateException { + if (Tools.isStringEmpty(browserConsoleLogsUrl)) { + throw new AutomateException("Session console logs not found", 404); + } + + return ((AutomateClient) getClient()).getSessionConsoleLogs(this); + } + + public final String getHARLogs() throws AutomateException { + if (Tools.isStringEmpty(harLogsUrl)) { + throw new AutomateException("Session HAR logs not found", 404); + } + + return ((AutomateClient) getClient()).getSessionHARLogs(this); + } + + public final String getAppiumLogs() throws AutomateException { + if (Tools.isStringEmpty(appiumLogsUrl)) { + throw new AutomateException("Session Appium logs not found", 404); + } + + return ((AutomateClient) getClient()).getSessionAppiumLogs(this); + } + /** * @return The id */ @@ -356,6 +391,54 @@ private void setOs(String os) { this.os = os; } + /** + * @return The browserConsoleLogsUrl + */ + @JsonProperty("browser_console_logs_url") + public String getBrowserConsoleLogsUrl() { + return browserConsoleLogsUrl; + } + + /** + * @param browserConsoleLogsUrl The browser_console_logs_url + */ + @JsonProperty("browser_console_logs_url") + private void setBrowserConsoleLogsUrl(String browserConsoleLogsUrl) { + this.browserConsoleLogsUrl = browserConsoleLogsUrl; + } + + /** + * @return The harLogsUrl + */ + @JsonProperty("har_logs_url") + public String getHarLogsUrl() { + return harLogsUrl; + } + + /** + * @param harLogsUrl The har_logs_url + */ + @JsonProperty("har_logs_url") + private void setHarLogsUrl(String harLogsUrl) { + this.harLogsUrl = harLogsUrl; + } + + /** + * @return The appiumLogsUrl + */ + @JsonProperty("appium_logs_url") + public String getAppiumLogsUrl() { + return appiumLogsUrl; + } + + /** + * @param appiumLogsUrl The appium_logs_url + */ + @JsonProperty("appium_logs_url") + private void setAppiumLogsUrl(String appiumLogsUrl) { + this.appiumLogsUrl = appiumLogsUrl; + } + @JsonAnyGetter protected Map getAdditionalProperties() { return this.additionalProperties; @@ -381,6 +464,9 @@ private boolean copyFrom(Session s) { setLogUrl(s.getLogUrl()); setStatus(s.getStatus()); setReason(s.getReason()); + setBrowserConsoleLogsUrl(s.getBrowserConsoleLogsUrl()); + setHarLogsUrl(s.getHarLogsUrl()); + setAppiumLogsUrl(s.getAppiumLogsUrl()); this.additionalProperties = s.getAdditionalProperties(); return true; } diff --git a/src/test/java/com/browserstack/automate/AutomateClientTest.java b/src/test/java/com/browserstack/automate/AutomateClientTest.java index 5bd7b36..06c333c 100644 --- a/src/test/java/com/browserstack/automate/AutomateClientTest.java +++ b/src/test/java/com/browserstack/automate/AutomateClientTest.java @@ -235,6 +235,74 @@ public void testGetSessionVideo() { } } + @Test + public void testGetSessionConsoleLogs() { + // TODO: Verify if logs are non-empty + // Cannot currently be tested during in-progress sessions + try { + String buildId = automateClient.getBuilds().get(0).getId(); + List sessions = automateClient.getSessions(buildId); + + String logs = sessions.get(0).getConsoleLogs(); + assertTrue("Session Console Logs", logs != null); + + logs = automateClient.getSessionConsoleLogs(sessions.get(0).getId()); + assertTrue("Session Console Logs", logs != null); + } catch (BuildNotFound e) { + assertTrue(false); + } catch (SessionNotFound e) { + assertTrue(false); + } catch (AutomateException e) { + assertTrue(false); + } + } + + @Test + public void testGetSessionHARLogs() { + // TODO: Verify if logs are non-empty + // Cannot currently be tested during in-progress sessions + // This test may fail sometimes. If the session does not have network logs enabled. + try { + String buildId = automateClient.getBuilds().get(0).getId(); + List sessions = automateClient.getSessions(buildId); + + String logs = sessions.get(0).getHARLogs(); + assertTrue("Session HAR Logs", logs != null); + + logs = automateClient.getSessionHARLogs(sessions.get(0).getId()); + assertTrue("Session HAR Logs", logs != null); + } catch (BuildNotFound e) { + assertTrue(false); + } catch (SessionNotFound e) { + assertTrue(false); + } catch (AutomateException e) { + assertTrue(false); + } + } + + @Test + public void testGetSessionAppiumLogs() { + // TODO: Verify if logs are non-empty + // Cannot currently be tested during in-progress sessions + // This test may fail sometimes. If the session does not have appium logs enabled. + try { + String buildId = automateClient.getBuilds().get(0).getId(); + List sessions = automateClient.getSessions(buildId); + + String logs = sessions.get(0).getAppiumLogs(); + assertTrue("Session Appium Logs", logs != null); + + logs = automateClient.getSessionAppiumLogs(sessions.get(0).getId()); + assertTrue("Session Appium Logs", logs != null); + } catch (BuildNotFound e) { + assertTrue(false); + } catch (SessionNotFound e) { + assertTrue(false); + } catch (AutomateException e) { + assertTrue(false); + } + } + // @Test public void testRecycleKey() { try {