Best Testsigma code snippet using com.testsigma.automator.drivers.DriverManager.startSession
Source:DriverManager.java  
...33  private Boolean restartDriverSession = Boolean.FALSE;34  private Instant sessionStartInstant;35  private Instant sessionEndInstant;36  private String initialSessionId;37  private String restartSessionId;38  private Boolean isRestart = Boolean.FALSE;39  DriverManager() {40    this.executionUuid = EnvironmentRunner.getRunnerExecutionId();41    this.testDeviceEntity = EnvironmentRunner.getRunnerEnvironmentEntity();42    this.executionLabType = testDeviceEntity.getExecutionLabType();43    this.settings = testDeviceEntity.getEnvSettings();44    this.testDeviceSettings = testDeviceEntity.getEnvSettings();45    TestPlanRunSettingEntity executionSettings = testDeviceEntity.getTestPlanSettings();46    this.testDeviceSettings.setElementTimeout(executionSettings.getPageTimeOut());47    this.testDeviceSettings.setPageLoadTimeout(executionSettings.getElementTimeOut());48  }49  public static DriverManager getDriverManager() {50    return _driverManager.get();51  }52  public static void setDriverManager(DriverManager driverManager) {53    _driverManager.set(driverManager);54  }55  public static void getDriverManager(TestDeviceEntity testDeviceEntity,56                                      WorkspaceType workspaceType, Platform os, Long environmentResultId,57                                      DriverSessionType driverSessionType) throws AutomatorException {58    DriverManager driverManager = null;59    switch (workspaceType) {60      case WebApplication:61        driverManager = new WebDriverManager();62        break;63      case MobileWeb:64        if (os.equals(Platform.Android)) {65          driverManager = new AndroidWebDriverManager();66        } else {67          driverManager = new IOSWebDriverManager();68        }69        break;70      case AndroidNative:71        driverManager = new AndroidNativeDriverManager();72        break;73      case IOSNative:74        driverManager = new IOSNativeDriverManager();75        break;76      default:77        break;78    }79    if (driverManager != null) {80      DriverManager.setDriverManager(driverManager);81      driverManager.startSession(driverSessionType,82        environmentResultId, Boolean.FALSE);83    }84  }85  public static RemoteWebDriver getRemoteWebDriver() {86    RemoteWebDriver driver = null;87    if (DriverManager.getDriverManager().getDriver() != null) {88      driver = DriverManager.getDriverManager().getDriver().getRemoteWebDriver();89    }90    return driver;91  }92  public boolean isRestart() {93    return this.isRestart;94  }95  public abstract void performCleanUpAction(OnAbortedAction actionType) throws AutomatorException;96  protected abstract RemoteWebDriver createDriverSession() throws AutomatorException, IOException;97  protected void beforeSessionCreateActions() throws AutomatorException {98    log.debug("Executing before create session actions for execution UUID - " + executionUuid);99    sessionStartInstant = Instant.now();100  }101  protected void afterSessionCreateActions()102    throws AutomatorException {103    log.debug("Executing after create session actions for execution UUID - " + executionUuid);104  }105  public void startSession(DriverSessionType driverSessionType, Long entityId, Boolean isRestart) throws AutomatorException {106    RemoteWebDriver remoteWebDriver;107    beforeSessionCreateActions();108    try {109      remoteWebDriver = createDriverSession();110      storeSessionId(driverSessionType, entityId);111      this.isRestart = isRestart;112      if (!isRestart) {113        this.initialSessionId = getSessionId();114        log.info("Initial Session ID:" + this.initialSessionId);115      } else {116        this.restartSessionId = getSessionId();117        log.info("Restarted Session ID:" + this.restartSessionId);118      }119    } catch (Exception e) {120      log.error(e.getMessage(), e);121      String errorMessage = parseErrorMessage(e.getMessage());122      if (StringUtils.isBlank(errorMessage)) {123        errorMessage = AutomatorMessages.EXCEPTION_WEBDRIVER_NOTCREATED + " - " + e.getMessage();124      } else if(e.getMessage().contains("NO_PARALLEL_RUN")){125        errorMessage = AutomatorMessages.NO_PARALLEL_RUNS;126        throw new TestsigmaNoParallelRunException(ErrorCodes.NO_PARALLEL_RUN,errorMessage);127      }else {128        errorMessage = "Unable to create a new Test Session due to unexpected failure(0x537). " + errorMessage;129      }130      endSession();131      throw new AutomatorException(ErrorCodes.DRIVER_NOT_CREATED, errorMessage);132    }133    if (remoteWebDriver != null) {134      log.info("Driver Session ID - " + getSessionId());135    } else {136      throw new AutomatorException(ErrorCodes.DRIVER_NOT_CREATED, AutomatorMessages.EXCEPTION_WEBDRIVER_NOTCREATED);137    }138    afterSessionCreateActions();139  }140  public void endSession() throws AutomatorException {141    try {142      if (getDriver() != null && (getDriver().getRemoteWebDriver() != null)) {143        log.info("Ending session(if exists) with execution UUID - " + executionUuid + " and session ID - "144          + getSessionId());145        RemoteWebDriver driver = getDriver().getRemoteWebDriver();146        try {147          beforeEndSessionActions();148          driver.quit();149        } catch (Exception e) {150          log.error(e.getMessage(), e);151          driver.quit();152        }153        afterEndSessionActions();154      } else {155        log.debug("There is no driver session with executionID - " + executionUuid);156      }157    } catch (Exception e) {158      throw new AutomatorException(e.getMessage(), e);159    }160  }161  protected void beforeEndSessionActions() throws AutomatorException {162    log.debug("Executing before end session actions for execution UUID - " + executionUuid);163  }164  protected void afterEndSessionActions() throws AutomatorException {165    log.debug("Executing after end session actions for execution UUID - " + executionUuid);166    new RuntimeDataProvider().clearRunTimeData(executionUuid);167    getDriver().setRemoteWebDriver(null);168    setDriver(null);169    sessionEndInstant = Instant.now();170    log.info("Total session time - " + TimeUtil.getFormattedDuration(sessionStartInstant, sessionEndInstant));171  }172  private String parseErrorMessage(String errorMessage) {173    String parsedErrorMessage = "";174    try {175      String[] tokens;176      if (errorMessage != null) {177        tokens = errorMessage.split("Original error:");178        if (tokens.length > 1) {179          tokens = tokens[1].split("Build info: version:");180          if (tokens.length > 1) {181            parsedErrorMessage = tokens[0];182          }183        }184        if(errorMessage.contains(SessionErrorType.PLATFORM_OS_NOT_SUPPORTED.name())){185          parsedErrorMessage = MSG_OS_NOT_SUPPORTED;186        }else if(errorMessage.contains(SessionErrorType.PLATFORM_BROWSER_VERSION_NOT_SUPPORTED.name())){187          parsedErrorMessage = MSG_BROWSER_NOT_SUPPORTED;188        }else if(errorMessage.contains(SessionErrorType.LAB_MINUTES_EXCEEDED.name())){189          parsedErrorMessage = MSG_LAB_MINUTES_EXCEEDED;190        }else if(errorMessage.contains(SessionErrorType.NO_PARALLEL_RUN.name())){191          parsedErrorMessage = MSG_NO_PARALLEL_RUN;192        }193      }194    } catch (Exception e) {195      log.error(e.getMessage(), e);196    }197    return parsedErrorMessage;198  }199  public void storeSessionId(DriverSessionType driverSessionType, Long entityId) throws AutomatorException {200    try {201      switch (driverSessionType) {202        case ENVIRONMENT_SESSION:203          storeEnvironmentSessionId(entityId);204          break;205        case TEST_SUITE_SESSION:206          storeTestSuiteSessionId(entityId);207          break;208        case TEST_CASE_SESSION:209          storeTestCaseSessionId(entityId);210          break;211        default:212          log.error("Unknown driver session type value provided - " + driverSessionType);213      }214    } catch (Exception e) {215      endSession();216      throw e;217    }218  }219  private void storeEnvironmentSessionId(Long entityId) throws AutomatorException {220    try {221      TestDeviceResultRequest testDeviceResultRequest = new TestDeviceResultRequest();222      testDeviceResultRequest.setId(entityId);223      testDeviceResultRequest.setSessionId(getSessionId());224      AutomatorConfig.getInstance().getAppBridge().updateEnvironmentResultData(testDeviceResultRequest);225    } catch (Exception e) {226      log.error(e.getMessage(), e);227      throw new AutomatorException(e.getMessage(), e);228    }229  }230  private void storeTestSuiteSessionId(Long entityId) throws AutomatorException {231    try {232      TestSuiteResultRequest testSuiteResultRequest = new TestSuiteResultRequest();233      testSuiteResultRequest.setId(entityId);234      testSuiteResultRequest.setSessionId(getSessionId());235      AutomatorConfig.getInstance().getAppBridge().updateTestSuiteResultData(testSuiteResultRequest);236    } catch (Exception e) {237      log.error(e.getMessage(), e);238      throw new AutomatorException(e.getMessage(), e);239    }240  }241  private void storeTestCaseSessionId(Long entityId) throws AutomatorException {242    try {243      TestCaseResultRequest testCaseResultRequest = new TestCaseResultRequest();244      testCaseResultRequest.setId(entityId);245      testCaseResultRequest.setSessionId(getSessionId());246      AutomatorConfig.getInstance().getAppBridge().updateTestCaseResultData(testCaseResultRequest);247    } catch (Exception e) {248      log.error(e.getMessage(), e);249      throw new AutomatorException(e.getMessage(), e);250    }251  }252  private boolean isTestsigmaLabMobileExecution() {253    return (WorkspaceType.isMobileApp(testDeviceEntity.getWorkspaceType())) &&254      (testDeviceEntity.getExecutionLabType() == ExecutionLabType.TestsigmaLab);255  }256  public String getSessionId() {257    return getRemoteWebDriver().getSessionId().toString();258  }259  public String getOngoingSessionId() {260    return StringUtils.isNotBlank(restartSessionId) ? restartSessionId : initialSessionId;261  }262}...Source:WebTestsuiteRunner.java  
...7public class WebTestsuiteRunner extends TestsuiteRunner {8  public WebTestsuiteRunner() {9    super();10  }11  public void startSession(Long entityId, DriverSessionType driverSessionType) throws AutomatorException {12    DriverManager.getDriverManager(testDeviceEntity, getWorkspaceType(), testDeviceSettings.getOs(),13      entityId, driverSessionType);14  }15}...startSession
Using AI Code Generation
1package com.testsigma.automator.drivers;2import java.util.HashMap;3import java.util.Map;4import org.openqa.selenium.WebDriver;5public class DriverManager {6	private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();7	private static final ThreadLocal<String> sessionId = new ThreadLocal<>();8	public static WebDriver getDriver() {9		return driver.get();10	}11	public static void setDriver(WebDriver driver) {12		DriverManager.driver.set(driver);13	}14	public static String getSessionId() {15		return sessionId.get();16	}17	public static void setSessionId(String sessionId) {18		DriverManager.sessionId.set(sessionId);19	}20	public static void startSession(String browserName, String browserVersion, String platformName, String platformVersion, String url) {21		Map<String, String> capabilities = new HashMap<>();22		capabilities.put("browserName", browserName);23		capabilities.put("browserVersion", browserVersion);24		capabilities.put("platformName", platformName);25		capabilities.put("platformVersion", platformVersion);26		capabilities.put("url", url);27		DriverManager.setDriver(new DriverFactory().getDriver(capabilities));28		DriverManager.setSessionId(((RemoteWebDriver) DriverManager.getDriver()).getSessionId().toString());29	}30}31package com.testsigma.automator.drivers;32import java.io.IOException;33import java.util.HashMap;34import java.util.Map;35import org.openqa.selenium.WebDriver;36import com.testsigma.automator.drivers.DriverManager;37import com.testsigma.automator.drivers.DriverFactory;38import com.testsigma.automator.drivers.DriverType;39import com.testsigma.automator.drivers.RemoteWebDriver;40public class DriverManager {41	private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();42	private static final ThreadLocal<String> sessionId = new ThreadLocal<>();43	public static WebDriver getDriver() {44		return driver.get();45	}46	public static void setDriver(WebDriver driver) {47		DriverManager.driver.set(driver);48	}49	public static String getSessionId() {50		return sessionId.get();51	}52	public static void setSessionId(String sessionId) {53		DriverManager.sessionId.set(sessionId);54	}55	public static void startSession(String browserName, String browserVersion, String platformName, String platformVersion, String url) {56		Map<String, String> capabilities = new HashMap<>();startSession
Using AI Code Generation
1import com.testsigma.automator.drivers.DriverManager;2public class 2 {3    public static void main(String[] args) throws Exception {4        String sessionId = DriverManager.startSession("chrome");5        System.out.println("Session Id is: " + sessionId);6    }7}8import com.testsigma.automator.drivers.DriverManager;9public class 3 {10    public static void main(String[] args) throws Exception {11        String sessionId = DriverManager.startSession("firefox");12        System.out.println("Session Id is: " + sessionId);13    }14}15import com.testsigma.automator.drivers.DriverManager;16public class 4 {17    public static void main(String[] args) throws Exception {18        String sessionId = DriverManager.startSession("ie");19        System.out.println("Session Id is: " + sessionId);20    }21}22import com.testsigma.automator.drivers.DriverManager;23public class 5 {24    public static void main(String[] args) throws Exception {25        String sessionId = DriverManager.startSession("edge");26        System.out.println("Session Id is: " + sessionId);27    }28}29import com.testsigma.automator.drivers.DriverManager;30public class 6 {31    public static void main(String[] args) throws Exception {32        String sessionId = DriverManager.startSession("safari");33        System.out.println("Session Id is: " + sessionId);34    }35}36import com.testsigma.automatorstartSession
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.remote.DesiredCapabilities;3import com.testsigma.automator.drivers.DriverManager;4public class 2 {5    public static void main(String[] args) throws Exception {6        DesiredCapabilities caps = DesiredCapabilities.chrome();7        caps.setCapability("platform", "Windows 10");8        caps.setCapability("version", "latest");9        DriverManager.endSession(driver);10    }11}12import org.openqa.selenium.WebDriver;13import org.openqa.selenium.remote.DesiredCapabilities;14import com.testsigma.automator.drivers.DriverManager;15public class 3 {16    public static void main(String[] args) throws Exception {17        DesiredCapabilities caps = DesiredCapabilities.chrome();18        caps.setCapability("platform", "Windows 10");19        caps.setCapability("version", "latest");20        DriverManager.endSession(driver);21    }22}23import org.openqa.seleniumLearn 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!!
