|
| 1 | +package io.github.lambdatest; |
| 2 | + |
| 3 | +import org.openqa.selenium.WebDriver; |
| 4 | +import io.github.lambdatest.utils.LoggerUtil; |
| 5 | +import io.github.lambdatest.utils.SmartUIUtil; |
| 6 | +import io.github.lambdatest.constants.Constants; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides methods to fetch aggregate visual comparison results from SmartUI. |
| 13 | + * |
| 14 | + * <p>Two overloaded methods are available: |
| 15 | + * <ul> |
| 16 | + * <li>{@link #smartuiResults(WebDriver)} - fetches results for a specific session (driver's sessionId)</li> |
| 17 | + * <li>{@link #smartuiResults()} - fetches results for the entire active build</li> |
| 18 | + * </ul> |
| 19 | + */ |
| 20 | +public class SmartUIResults { |
| 21 | + |
| 22 | + private static final Logger log = LoggerUtil.createLogger("lambdatest-java-sdk"); |
| 23 | + private static final SmartUIUtil smartUIUtils = new SmartUIUtil(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Fetches SmartUI visual comparison results for the session associated with the given WebDriver. |
| 27 | + * |
| 28 | + * <p>This method extracts the sessionId from the RemoteWebDriver and queries the SmartUI server |
| 29 | + * for screenshot data filtered by that session. The response contains screenshots grouped by name |
| 30 | + * with a summary indicating variant and screenshot counts. |
| 31 | + * |
| 32 | + * @param driver A Selenium WebDriver instance (must be a RemoteWebDriver) |
| 33 | + * @return JSONObject containing screenshots grouped by name and a summary with type "session" |
| 34 | + * @throws Exception if the driver is null, not a RemoteWebDriver, or the request fails |
| 35 | + */ |
| 36 | + public static JSONObject smartuiResults(WebDriver driver) throws Exception { |
| 37 | + if (driver == null) { |
| 38 | + throw new IllegalArgumentException(Constants.Errors.SELENIUM_DRIVER_NULL); |
| 39 | + } |
| 40 | + |
| 41 | + if (!smartUIUtils.isSmartUIRunning()) { |
| 42 | + throw new IllegalStateException(Constants.Errors.SMARTUI_NOT_RUNNING); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + // Extract sessionId from the driver (null-safe) |
| 47 | + org.openqa.selenium.remote.SessionId sid = ((org.openqa.selenium.remote.RemoteWebDriver) driver).getSessionId(); |
| 48 | + if (sid == null) { |
| 49 | + throw new IllegalStateException("Unable to get sessionId from the driver"); |
| 50 | + } |
| 51 | + String sessionId = sid.toString(); |
| 52 | + |
| 53 | + log.info("Fetching SmartUI results for sessionId: " + sessionId); |
| 54 | + |
| 55 | + // CLI server resolves buildId and projectToken from its context |
| 56 | + String resultsResponse = smartUIUtils.getSmartUIResults(sessionId); |
| 57 | + log.info("SmartUI results fetched successfully for sessionId: " + sessionId); |
| 58 | + |
| 59 | + return new JSONObject(resultsResponse); |
| 60 | + |
| 61 | + } catch (ClassCastException e) { |
| 62 | + throw new IllegalArgumentException("Driver must be an instance of RemoteWebDriver to extract sessionId", e); |
| 63 | + } catch (Exception e) { |
| 64 | + throw e; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Fetches SmartUI visual comparison results for the entire active build. |
| 70 | + * |
| 71 | + * <p>This method queries the SmartUI server for all screenshot data in the current build |
| 72 | + * without filtering by session. The response contains screenshots grouped by name |
| 73 | + * with a summary indicating variant and screenshot counts. |
| 74 | + * |
| 75 | + * @return JSONObject containing screenshots grouped by name and a summary with type "build" |
| 76 | + * @throws Exception if the SmartUI server is not running or the request fails |
| 77 | + */ |
| 78 | + public static JSONObject smartuiResults() throws Exception { |
| 79 | + if (!smartUIUtils.isSmartUIRunning()) { |
| 80 | + throw new IllegalStateException(Constants.Errors.SMARTUI_NOT_RUNNING); |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + log.info("Fetching SmartUI results for entire build"); |
| 85 | + |
| 86 | + // CLI server resolves buildId from its active build context |
| 87 | + String resultsResponse = smartUIUtils.getSmartUIResults(null); |
| 88 | + log.info("SmartUI results fetched successfully for build"); |
| 89 | + |
| 90 | + return new JSONObject(resultsResponse); |
| 91 | + |
| 92 | + } catch (Exception e) { |
| 93 | + throw e; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments