How to use switchToNativeContext method of com.testsigma.agent.mobile.DriverSessionCommand class

Best Testsigma code snippet using com.testsigma.agent.mobile.DriverSessionCommand.switchToNativeContext

Source:DriverSessionCommand.java Github

copy

Full Screen

...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) {184 try {185 switchToNativeContext(sessionContainer.getSessionMap().get(sessionId), mobileElement);186 } catch (Exception e) {187 log.error(e.getMessage(), e);188 throw new MobileAutomationServerCommandExecutionException(e.getMessage(), e);189 }190 }191 }192 }193 public String pageScreenshot(String sessionId) throws MobileAutomationServerCommandExecutionException {194 try {195 RemoteWebDriver remoteWebDriver = sessionContainer.getSessionMap().get(sessionId);196 ScreenshotAction screenshotAction = new ScreenshotAction();197 screenshotAction.setDriver(remoteWebDriver);198 ActionResult result = screenshotAction.run();199 if (ActionResult.FAILED.equals(result)) {...

Full Screen

Full Screen

switchToNativeContext

Using AI Code Generation

copy

Full Screen

1import com.testsigma.agent.mobile.DriverSessionCommand;2import com.testsigma.agent.mobile.MobileDriverSession;3import com.testsigma.agent.mobile.MobileDriverSessionManager;4import com.testsigma.agent.mobile.MobileDriverSessionManagerFactory;5MobileDriverSessionManager mobileDriverSessionManager = MobileDriverSessionManagerFactory.getMobileDriverSessionManager();6MobileDriverSession mobileDriverSession = mobileDriverSessionManager.getMobileDriverSession();7DriverSessionCommand driverSessionCommand = new DriverSessionCommand(mobileDriverSession);8driverSessionCommand.switchToNativeContext();9driverSessionCommand.switchToWebViewContext();10driverSessionCommand.switchToNativeContext();11driverSessionCommand.switchToWebViewContext();12List<String> contextList = driverSessionCommand.getContexts();13driverSessionCommand.switchToWebViewContext();14driverSessionCommand.switchToNativeContext();15driverSessionCommand.switchToWebViewContext();16List<String> contextList = driverSessionCommand.getContexts();17driverSessionCommand.switchToWebViewContext();18driverSessionCommand.switchToNativeContext();19driverSessionCommand.switchToWebViewContext();20List<String> contextList = driverSessionCommand.getContexts();21driverSessionCommand.switchToWebViewContext();22driverSessionCommand.switchToNativeContext();23driverSessionCommand.switchToWebViewContext();24List<String> contextList = driverSessionCommand.getContexts();

Full Screen

Full Screen

switchToNativeContext

Using AI Code Generation

copy

Full Screen

1import com.testsigma.agent.mobile.DriverSessionCommand;2import com.testsigma.agent.mobile.DriverSessionCommandFactory;3import com.testsigma.agent.mobile.MobileDriverSession;4import com.testsigma.agent.mobile.MobileDriverSessionFactory;5MobileDriverSession driverSession = MobileDriverSessionFactory.getDriverSession();6DriverSessionCommand driverSessionCommand = DriverSessionCommandFactory.getDriverSessionCommand(driverSession);7driverSessionCommand.switchToNativeContext();8driverSessionCommand.switchToWebViewContext();9driverSessionCommand.switchToWebViewContext("WEBVIEW_1");10driverSessionCommand.switchToWebViewContext(1);11driverSessionCommand.switchToWebViewContext("Google");12driverSessionCommand.switchToWebViewContext("Go*");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful