Skip to content

Added new attributes for automation_session object and updated endpoint #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/browserstack/automate/Automate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
113 changes: 112 additions & 1 deletion src/main/java/com/browserstack/automate/AutomateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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.
*
Expand Down
88 changes: 87 additions & 1 deletion src/main/java/com/browserstack/automate/model/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Object> additionalProperties = new HashMap<String, Object>();

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<String, Object> getAdditionalProperties() {
return this.additionalProperties;
Expand All @@ -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;
}
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/com/browserstack/automate/AutomateClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Session> 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail sometimes. If the last run session haven't enabled network logs.

// 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<Session> 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() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail in case the last session is run on desktop or appium logs capability was not passed in that.

// 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<Session> 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 {
Expand Down