Best Testsigma code snippet using com.testsigma.agent.mobile.DriverSessionCommand.switchToContextByName
Source:DriverSessionCommand.java
...77 public void tapOnElement(String sessionId, MobileElement mobileElement) throws MobileAutomationServerCommandExecutionException {78 try {79 RemoteWebDriver remoteWebDriver = sessionContainer.getSessionMap().get(sessionId);80 if (mobileElement.getWebViewName() != null)81 this.switchToContextByName(remoteWebDriver, mobileElement);82 MobileInspectorTapOnElement mobileInspectorTapOnElement = new MobileInspectorTapOnElement();83 mobileInspectorTapOnElement.setElementPropertiesEntityMap(createElementPropertiesMap(FindByType.XPATH, mobileElement.getXpath()));84 mobileInspectorTapOnElement.setDriver(remoteWebDriver);85 ActionResult result = mobileInspectorTapOnElement.run();86 if (ActionResult.FAILED.equals(result)) {87 log.error(mobileInspectorTapOnElement.getErrorMessage());88 throw new Exception("Failed to tap on element " + " : " + mobileInspectorTapOnElement.getErrorMessage());89 }90 } catch (Exception e) {91 log.error(e.getMessage(), e);92 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);93 } finally {94 if (mobileElement.getWebViewName() != null) {95 try {96 switchToNativeContext(sessionContainer.getSessionMap().get(sessionId), mobileElement);97 } catch (Exception e) {98 log.error(e.getMessage(), e);99 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);100 }101 }102 }103 }104 private void switchToContextByName(RemoteWebDriver remoteWebDriver, MobileElement mobileElement) throws Exception {105 SwitchToContextWithNameAction switchToContext = new SwitchToContextWithNameAction();106 switchToContext.setTestDataPropertiesEntityMap(createTestDataPropertiesMap(mobileElement.getWebViewName()));107 switchToContext.setDriver(remoteWebDriver);108 ActionResult result = switchToContext.run();109 if (ActionResult.FAILED.equals(result)) {110 log.error(switchToContext.getErrorMessage());111 throw new Exception("Failed to Switch to context " + " : " + switchToContext.getErrorMessage());112 }113 }114 private void switchToNativeContext(RemoteWebDriver remoteWebDriver, MobileElement mobileElement) throws Exception {115 SwitchToNativeAppContextAction switchToContext = new SwitchToNativeAppContextAction();116 switchToContext.setDriver(remoteWebDriver);117 ActionResult result = switchToContext.run();118 if (ActionResult.FAILED.equals(result)) {119 log.error(switchToContext.getErrorMessage());120 throw new MobileAutomationServerCommandExecutionException("Failed to Switch to back to native context " + " : " + switchToContext.getErrorMessage());121 }122 }123 public void clearElement(String sessionId, MobileElement mobileElement) throws MobileAutomationServerCommandExecutionException {124 try {125 RemoteWebDriver remoteWebDriver = sessionContainer.getSessionMap().get(sessionId);126 if (mobileElement.getWebViewName() != null)127 this.switchToContextByName(remoteWebDriver, mobileElement);128 ClearElementAction clearElementAction = new ClearElementAction();129 clearElementAction.setElementPropertiesEntityMap(createElementPropertiesMap(FindByType.XPATH, mobileElement.getXpath()));130 clearElementAction.setDriver(remoteWebDriver);131 ActionResult result = clearElementAction.run();132 if (ActionResult.FAILED.equals(result)) {133 log.error(clearElementAction.getErrorMessage());134 throw new Exception("Failed to clear element " + " : " + clearElementAction.getErrorMessage());135 }136 } catch (Exception e) {137 log.error(e.getMessage(), e);138 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);139 } finally {140 if (mobileElement.getWebViewName() != null) {141 try {142 switchToNativeContext(sessionContainer.getSessionMap().get(sessionId), mobileElement);143 } catch (Exception e) {144 log.error(e.getMessage(), e);145 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);146 }147 }148 }149 }150 private Map<String, ElementPropertiesEntity> createElementPropertiesMap(FindByType findByType, String locatorValue) {151 ElementPropertiesEntity elementPropertiesEntity = new ElementPropertiesEntity();152 Map<String, ElementPropertiesEntity> elementPropertiesEntityMap = new HashMap<>();153 elementPropertiesEntity.setFindByType(findByType);154 elementPropertiesEntity.setLocatorValue(locatorValue);155 elementPropertiesEntityMap.put(NaturalTextActionConstants.TESTS_TEP_DATA_MAP_KEY_ELEMENT, elementPropertiesEntity);156 return elementPropertiesEntityMap;157 }158 private Map<String, TestDataPropertiesEntity> createTestDataPropertiesMap(String testDataValue) {159 Map<String, TestDataPropertiesEntity> testDataPropertiesEntityMap = new HashMap<>();160 TestDataPropertiesEntity testDataPropertiesEntity = new TestDataPropertiesEntity();161 testDataPropertiesEntity.setTestDataValue(testDataValue);162 testDataPropertiesEntityMap.put(NaturalTextActionConstants.TEST_STEP_DATA_MAP_KEY_TEST_DATA, testDataPropertiesEntity);163 return testDataPropertiesEntityMap;164 }165 public void sendKeys(String sessionId, MobileElement mobileElement, String keys) throws MobileAutomationServerCommandExecutionException {166 try {167 RemoteWebDriver remoteWebDriver = sessionContainer.getSessionMap().get(sessionId);168 if (mobileElement.getWebViewName() != null)169 this.switchToContextByName(remoteWebDriver, mobileElement);170 SendKeysAction sendKeysAction = new SendKeysAction();171 sendKeysAction.setElementPropertiesEntityMap(createElementPropertiesMap(FindByType.XPATH, mobileElement.getXpath()));172 sendKeysAction.setTestDataPropertiesEntityMap(createTestDataPropertiesMap(keys));173 sendKeysAction.setDriver(remoteWebDriver);174 ActionResult result = sendKeysAction.run();175 if (ActionResult.FAILED.equals(result)) {176 log.error(sendKeysAction.getErrorMessage());177 throw new Exception("Failed to send keys to element " + " : " + sendKeysAction.getErrorMessage());178 }179 } catch (Exception e) {180 log.error(e.getMessage(), e);181 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);182 } finally {183 if (mobileElement.getWebViewName() != null) {...
switchToContextByName
Using AI Code Generation
1package com.testsigma.agent.mobile;2import java.util.List;3import java.util.Map;4import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.RemoteWebDriver;8import com.testsigma.agent.core.Command;9import com.testsigma.agent.core.CommandContext;10import com.testsigma.agent.core.CommandExecutionException;11import com.testsigma.agent.core.CommandExecutor;12import com.testsigma.agent.core.CommandResult;13import com.testsigma.agent.core.CommandUtils;14import com.testsigma.agent.core.DriverSession;15import com.testsigma.agent.core.DriverSessionManager;16import com.testsigma.agent.core.TestsigmaException;17import com.testsigma.agent.core.TestsigmaLogger;18import com.testsigma.agent.core.TestsigmaUtils;19import com.testsigma.agent.core.data.TestData;20import com.testsigma.agent.core.data.TestDataRepository;21import com.testsigma.agent.core.data.TestDataRepositoryFactory
switchToContextByName
Using AI Code Generation
1import com.testsigma.agent.mobile.DriverSessionCommand;2import com.testsigma.agent.mobile.DriverSessionCommandFactory;3import com.testsigma.agent.mobile.MobileDriverSession;4import com.testsigma.agent.mobile.MobileDriverSessionManager;5import com.testsigma.agent.mobile.MobileDriverSessionType;6MobileDriverSessionManager mobileDriverSessionManager = new MobileDriverSessionManager();7MobileDriverSession mobileDriverSession = mobileDriverSessionManager.getMobileDriverSession("session-id");8DriverSessionCommandFactory driverSessionCommandFactory = new DriverSessionCommandFactory();9DriverSessionCommand driverSessionCommand = driverSessionCommandFactory.getCommand(mobileDriverSession, MobileDriverSessionType.SWITCH_TO_CONTEXT_BY_NAME);10Map<String, Object> args = new HashMap<String, Object>();11args.put("contextName", "WEBVIEW_com.testsigma.mobile.sampleapp");12driverSessionCommand.execute(args);13import com.testsigma.agent.mobile.DriverSessionCommand;14import com.testsigma.agent.mobile.DriverSessionCommandFactory;15import com.testsigma.agent.mobile.MobileDriverSession;16import com.testsigma.agent.mobile.MobileDriverSessionManager;17import com.testsigma.agent.mobile.MobileDriverSessionType;18MobileDriverSessionManager mobileDriverSessionManager = new MobileDriverSessionManager();19MobileDriverSession mobileDriverSession = mobileDriverSessionManager.getMobileDriverSession("session-id");20DriverSessionCommandFactory driverSessionCommandFactory = new DriverSessionCommandFactory();21DriverSessionCommand driverSessionCommand = driverSessionCommandFactory.getCommand(mobileDriverSession, MobileDriverSessionType.SWITCH_TO_CONTEXT_BY_NAME);22Map<String, Object> args = new HashMap<String, Object>();23args.put("contextName", "WEBVIEW_com.testsigma.mobile.sampleapp");24driverSessionCommand.execute(args);
switchToContextByName
Using AI Code Generation
1import com.testsigma.agent.mobile.DriverSessionCommand;2import com.testsigma.agent.mobile.DriverSessionCommandFactory;3DriverSessionCommand driverSessionCommand = DriverSessionCommandFactory.getDriverSessionCommand();4driverSessionCommand.switchToContextByName("WEBVIEW_1");5System.out.println(driverSessionCommand.getCurrentContext());6System.out.println(driverSessionCommand.getCurrentContextName());7System.out.println(driverSessionCommand.getCurrentContextHandle());8System.out.println(driverSessionCommand.getContextHandles());9System.out.println(driverSessionCommand.getContextNames());10System.out.println(driverSessionCommand.getContextNames());11System.out.println(driverSessionCommand.isNativeContext());12System.out.println(driverSessionCommand.isWebviewContext());13System.out.println(driverSessionCommand.isHybridContext());14System.out.println(driverSessionCommand.isNativeContext());15System.out.println(driverSessionCommand.isWebviewContext());16System.out.println(driverSessionCommand.isHybridContext());17System.out.println(driverSessionCommand.getCurrentContext());18System.out.println(driverSessionCommand.getCurrentContextName());19System.out.println(driverSessionCommand.getCurrentContextHandle());20System.out.println(driverSessionCommand.getContextHandles());21System.out.println(driverSessionCommand.getContextNames());22System.out.println(driverSessionCommand.getContextNames());23System.out.println(driverSessionCommand.isNativeContext());24System.out.println(driverSessionCommand.isWebviewContext());25System.out.println(driverSessionCommand.isHybridContext());26System.out.println(driverSessionCommand.isNativeContext());27System.out.println(driverSessionCommand.isWebviewContext());
Learn 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!!