How to use context method of io.appium.java_client.remote.SupportsContextSwitching class

Best io.appium code snippet using io.appium.java_client.remote.SupportsContextSwitching.context

GenericWebDriverManagerTests.java

Source:GenericWebDriverManagerTests.java Github

copy

Full Screen

...67 when(webDriverProvider.get()).thenReturn(webDriver);68 when(((HasCapabilities) webDriver).getCapabilities()).thenReturn(capabilities);69 return webDriver;70 }71 private void mockMobileDriverContext(SupportsContextSwitching contextSwitchingDriver,72 Set<String> returnContextHandles)73 {74 when(webDriverProvider.getUnwrapped(SupportsContextSwitching.class)).thenReturn(contextSwitchingDriver);75 when(contextSwitchingDriver.getContext()).thenReturn(WEBVIEW_CONTEXT);76 when(contextSwitchingDriver.getContextHandles()).thenReturn(returnContextHandles);77 }78 private GenericWebDriverManager spyIsMobile(boolean returnValue)79 {80 var spy = Mockito.spy(driverManager);81 doReturn(returnValue).when(spy).isMobile();82 return spy;83 }84 @Test85 void testPerformActionInNativeContextNotMobile()86 {87 var webDriverConfigured = mock(WebDriver.class);88 when(webDriverProvider.get()).thenReturn(webDriverConfigured);89 GenericWebDriverManager spy = spyIsMobile(false);90 spy.performActionInNativeContext(webDriver -> assertEquals(webDriverConfigured, webDriver));91 verifyNoInteractions(webDriverConfigured);92 }93 @Test94 void testPerformActionInNativeContext()95 {96 var contextSwitchingDriver = mock(SupportsContextSwitching.class,97 withSettings().extraInterfaces(HasCapabilities.class));98 Set<String> contexts = new HashSet<>();99 contexts.add(WEBVIEW_CONTEXT);100 contexts.add(NATIVE_APP_CONTEXT);101 mockMobileDriverContext(contextSwitchingDriver, contexts);102 var spy = spyIsMobile(true);103 spy.performActionInNativeContext(webDriver -> assertEquals(contextSwitchingDriver, webDriver));104 verify(contextSwitchingDriver).context(NATIVE_APP_CONTEXT);105 verify(contextSwitchingDriver).context(WEBVIEW_CONTEXT);106 }107 @Test108 void testPerformActionInNativeContextException()109 {110 var contextSwitchingDriver = mock(SupportsContextSwitching.class,111 withSettings().extraInterfaces(HasCapabilities.class));112 mockMobileDriverContext(contextSwitchingDriver, new HashSet<>());113 var spy = spyIsMobile(true);114 var exception = assertThrows(IllegalStateException.class,115 () -> spy.performActionInNativeContext(webDriver -> assertEquals(contextSwitchingDriver, webDriver)));116 assertEquals("MobileDriver doesn't have context: " + NATIVE_APP_CONTEXT, exception.getMessage());117 }118 @Test119 void testPerformActionInNativeContextSwitchNotNeeded()120 {121 var contextSwitchingDriver = mock(SupportsContextSwitching.class);122 when(webDriverProvider.getUnwrapped(SupportsContextSwitching.class)).thenReturn(contextSwitchingDriver);123 when(contextSwitchingDriver.getContext()).thenReturn(NATIVE_APP_CONTEXT);124 var spy = spyIsMobile(true);125 spy.performActionInNativeContext(webDriver -> assertEquals(contextSwitchingDriver, webDriver));126 verify(contextSwitchingDriver, never()).context(NATIVE_APP_CONTEXT);127 verify(contextSwitchingDriver, never()).getContextHandles();128 }129 static Stream<Arguments> mobileArguments()130 {131 // CHECKSTYLE:OFF132 return Stream.of(133 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, MobilePlatform.IOS, true),134 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, MobilePlatform.ANDROID, true),135 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, MobilePlatform.WINDOWS, false),136 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, Platform.IOS, true),137 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, Platform.ANDROID, true),138 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMobile, Platform.WINDOWS, false),139 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isIOS, MobilePlatform.IOS, true),140 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isIOS, MobilePlatform.ANDROID, false),141 arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isIOS, Platform.IOS, true),...

Full Screen

Full Screen

AndroidDriver.java

Source:AndroidDriver.java Github

copy

Full Screen

1/*2 * Licensed under the Apache License, Version 2.0 (the "License");3 * you may not use this file except in compliance with the License.4 * See the NOTICE file distributed with this work for additional5 * information regarding copyright ownership.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.appium.java_client.android;17import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;18import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;19import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;20import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;21import com.google.common.collect.ImmutableMap;22import io.appium.java_client.AppiumDriver;23import io.appium.java_client.CommandExecutionHelper;24import io.appium.java_client.ExecuteCDPCommand;25import io.appium.java_client.HasAppStrings;26import io.appium.java_client.HasDeviceTime;27import io.appium.java_client.HasOnScreenKeyboard;28import io.appium.java_client.HidesKeyboard;29import io.appium.java_client.InteractsWithApps;30import io.appium.java_client.PullsFiles;31import io.appium.java_client.LocksDevice;32import io.appium.java_client.PerformsTouchActions;33import io.appium.java_client.PushesFiles;34import io.appium.java_client.SupportsLegacyAppManagement;35import io.appium.java_client.android.connection.HasNetworkConnection;36import io.appium.java_client.android.geolocation.SupportsExtendedGeolocationCommands;37import io.appium.java_client.android.nativekey.PressesKey;38import io.appium.java_client.battery.HasBattery;39import io.appium.java_client.remote.SupportsContextSwitching;40import io.appium.java_client.remote.SupportsLocation;41import io.appium.java_client.remote.SupportsRotation;42import io.appium.java_client.screenrecording.CanRecordScreen;43import io.appium.java_client.service.local.AppiumDriverLocalService;44import io.appium.java_client.service.local.AppiumServiceBuilder;45import io.appium.java_client.ws.StringWebSocketClient;46import org.openqa.selenium.Capabilities;47import org.openqa.selenium.Platform;48import org.openqa.selenium.remote.HttpCommandExecutor;49import org.openqa.selenium.remote.html5.RemoteLocationContext;50import org.openqa.selenium.remote.http.HttpClient;51import java.net.URL;52import java.util.Collections;53import java.util.Map;54/**55 * Android driver implementation.56 */57public class AndroidDriver extends AppiumDriver implements58 PressesKey,59 SupportsRotation,60 SupportsContextSwitching,61 SupportsLocation,62 PerformsTouchActions,63 HidesKeyboard,64 HasDeviceTime,65 PullsFiles,66 InteractsWithApps,67 SupportsLegacyAppManagement,68 HasAppStrings,69 HasNetworkConnection,70 PushesFiles,71 StartsActivity,72 LocksDevice,73 HasAndroidSettings,74 HasAndroidDeviceDetails,75 HasSupportedPerformanceDataType,76 AuthenticatesByFinger,77 HasOnScreenKeyboard,78 CanRecordScreen,79 SupportsSpecialEmulatorCommands,80 SupportsNetworkStateManagement,81 ListensToLogcatMessages,82 HasAndroidClipboard,83 HasBattery<AndroidBatteryInfo>,84 ExecuteCDPCommand,85 CanReplaceElementValue,86 SupportsExtendedGeolocationCommands {87 private static final String ANDROID_PLATFORM = Platform.ANDROID.name();88 private StringWebSocketClient logcatClient;89 /**90 * Creates a new instance based on command {@code executor} and {@code capabilities}.91 *92 * @param executor is an instance of {@link HttpCommandExecutor}93 * or class that extends it. Default commands or another vendor-specific94 * commands may be specified there.95 * @param capabilities take a look at {@link Capabilities}96 */97 public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {98 super(executor, ensurePlatformName(capabilities, ANDROID_PLATFORM));99 }100 /**101 * Creates a new instance based on Appium server URL and {@code capabilities}.102 *103 * @param remoteAddress is the address of remotely/locally started Appium server104 * @param capabilities take a look at {@link Capabilities}105 */106 public AndroidDriver(URL remoteAddress, Capabilities capabilities) {107 super(remoteAddress, ensurePlatformName(capabilities, ANDROID_PLATFORM));108 }109 /**110 * Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.111 *112 * @param remoteAddress is the address of remotely/locally started Appium server113 * @param httpClientFactory take a look at {@link HttpClient.Factory}114 * @param capabilities take a look at {@link Capabilities}115 */116 public AndroidDriver(117 URL remoteAddress, HttpClient.Factory httpClientFactory, Capabilities capabilities) {118 super(remoteAddress, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));119 }120 /**121 * Creates a new instance based on Appium driver local service and {@code capabilities}.122 *123 * @param service take a look at {@link AppiumDriverLocalService}124 * @param capabilities take a look at {@link Capabilities}125 */126 public AndroidDriver(AppiumDriverLocalService service, Capabilities capabilities) {127 super(service, ensurePlatformName(capabilities, ANDROID_PLATFORM));128 }129 /**130 * Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.131 *132 * @param service take a look at {@link AppiumDriverLocalService}133 * @param httpClientFactory take a look at {@link HttpClient.Factory}134 * @param capabilities take a look at {@link Capabilities}135 */136 public AndroidDriver(137 AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, Capabilities capabilities) {138 super(service, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));139 }140 /**141 * Creates a new instance based on Appium service builder and {@code capabilities}.142 *143 * @param builder take a look at {@link AppiumServiceBuilder}144 * @param capabilities take a look at {@link Capabilities}145 */146 public AndroidDriver(AppiumServiceBuilder builder, Capabilities capabilities) {147 super(builder, ensurePlatformName(capabilities, ANDROID_PLATFORM));148 }149 /**150 * Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.151 *152 * @param builder take a look at {@link AppiumServiceBuilder}153 * @param httpClientFactory take a look at {@link HttpClient.Factory}154 * @param capabilities take a look at {@link Capabilities}155 */156 public AndroidDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,157 Capabilities capabilities) {158 super(builder, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));159 }160 /**161 * Creates a new instance based on HTTP client factory and {@code capabilities}.162 *163 * @param httpClientFactory take a look at {@link HttpClient.Factory}164 * @param capabilities take a look at {@link Capabilities}165 */166 public AndroidDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {167 super(httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));168 }169 /**170 * Creates a new instance based on {@code capabilities}.171 *172 * @param capabilities take a look at {@link Capabilities}173 */174 public AndroidDriver(Capabilities capabilities) {175 super(ensurePlatformName(capabilities, ANDROID_PLATFORM));176 }177 /**178 * Get test-coverage data.179 *180 * @param intent intent to broadcast.181 * @param path path to .ec file.182 */183 public void endTestCoverage(String intent, String path) {184 CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));185 }186 /**187 * Open the notification shade, on Android devices.188 */189 public void openNotifications() {190 CommandExecutionHelper.execute(this, openNotificationsCommand());191 }192 public void toggleLocationServices() {193 CommandExecutionHelper.execute(this, toggleLocationServicesCommand());194 }195 @SuppressWarnings("unchecked")196 @Override197 public AndroidBatteryInfo getBatteryInfo() {198 return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(199 "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue());200 }201 @Override202 public RemoteLocationContext getLocationContext() {203 return locationContext;204 }205 @Override206 public synchronized StringWebSocketClient getLogcatClient() {207 if (logcatClient == null) {208 logcatClient = new StringWebSocketClient();209 }210 return logcatClient;211 }212}...

Full Screen

Full Screen

IOSDriver.java

Source:IOSDriver.java Github

copy

Full Screen

1/*2 * Licensed under the Apache License, Version 2.0 (the "License");3 * you may not use this file except in compliance with the License.4 * See the NOTICE file distributed with this work for additional5 * information regarding copyright ownership.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.appium.java_client.ios;17import static io.appium.java_client.MobileCommand.prepareArguments;18import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;19import com.google.common.collect.ImmutableMap;20import io.appium.java_client.AppiumDriver;21import io.appium.java_client.HasAppStrings;22import io.appium.java_client.HasDeviceTime;23import io.appium.java_client.HasOnScreenKeyboard;24import io.appium.java_client.HidesKeyboard;25import io.appium.java_client.HidesKeyboardWithKeyName;26import io.appium.java_client.InteractsWithApps;27import io.appium.java_client.PullsFiles;28import io.appium.java_client.LocksDevice;29import io.appium.java_client.PerformsTouchActions;30import io.appium.java_client.PushesFiles;31import io.appium.java_client.SupportsLegacyAppManagement;32import io.appium.java_client.battery.HasBattery;33import io.appium.java_client.remote.SupportsContextSwitching;34import io.appium.java_client.remote.SupportsLocation;35import io.appium.java_client.remote.SupportsRotation;36import io.appium.java_client.screenrecording.CanRecordScreen;37import io.appium.java_client.service.local.AppiumDriverLocalService;38import io.appium.java_client.service.local.AppiumServiceBuilder;39import io.appium.java_client.ws.StringWebSocketClient;40import org.openqa.selenium.Alert;41import org.openqa.selenium.Capabilities;42import org.openqa.selenium.Platform;43import org.openqa.selenium.remote.DriverCommand;44import org.openqa.selenium.remote.HttpCommandExecutor;45import org.openqa.selenium.remote.Response;46import org.openqa.selenium.remote.html5.RemoteLocationContext;47import org.openqa.selenium.remote.http.HttpClient;48import java.net.URL;49import java.util.Collections;50import java.util.Map;51/**52 * iOS driver implementation.53 */54public class IOSDriver extends AppiumDriver implements55 SupportsContextSwitching,56 SupportsRotation,57 SupportsLocation,58 HidesKeyboard,59 HasDeviceTime,60 PullsFiles,61 InteractsWithApps,62 SupportsLegacyAppManagement,63 HasAppStrings,64 PerformsTouchActions,65 HidesKeyboardWithKeyName,66 ShakesDevice,67 HasIOSSettings,68 HasOnScreenKeyboard,69 LocksDevice,70 PerformsTouchID,71 PushesFiles,72 CanRecordScreen,73 HasIOSClipboard,74 ListensToSyslogMessages,75 HasBattery<IOSBatteryInfo> {76 private static final String PLATFORM_NAME = Platform.IOS.name();77 private StringWebSocketClient syslogClient;78 /**79 * Creates a new instance based on command {@code executor} and {@code capabilities}.80 *81 * @param executor is an instance of {@link HttpCommandExecutor}82 * or class that extends it. Default commands or another vendor-specific83 * commands may be specified there.84 * @param capabilities take a look at {@link Capabilities}85 */86 public IOSDriver(HttpCommandExecutor executor, Capabilities capabilities) {87 super(executor, ensurePlatformName(capabilities, PLATFORM_NAME));88 }89 /**90 * Creates a new instance based on Appium server URL and {@code capabilities}.91 *92 * @param remoteAddress is the address of remotely/locally started Appium server93 * @param capabilities take a look at {@link Capabilities}94 */95 public IOSDriver(URL remoteAddress, Capabilities capabilities) {96 super(remoteAddress, ensurePlatformName(capabilities, PLATFORM_NAME));97 }98 /**99 * Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.100 *101 * @param remoteAddress is the address of remotely/locally started Appium server102 * @param httpClientFactory take a look at {@link HttpClient.Factory}103 * @param capabilities take a look at {@link Capabilities}104 */105 public IOSDriver(URL remoteAddress, HttpClient.Factory httpClientFactory,106 Capabilities capabilities) {107 super(remoteAddress, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));108 }109 /**110 * Creates a new instance based on Appium driver local service and {@code capabilities}.111 *112 * @param service take a look at {@link AppiumDriverLocalService}113 * @param capabilities take a look at {@link Capabilities}114 */115 public IOSDriver(AppiumDriverLocalService service, Capabilities capabilities) {116 super(service, ensurePlatformName(capabilities, PLATFORM_NAME));117 }118 /**119 * Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.120 *121 * @param service take a look at {@link AppiumDriverLocalService}122 * @param httpClientFactory take a look at {@link HttpClient.Factory}123 * @param capabilities take a look at {@link Capabilities}124 */125 public IOSDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,126 Capabilities capabilities) {127 super(service, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));128 }129 /**130 * Creates a new instance based on Appium service builder and {@code capabilities}.131 *132 * @param builder take a look at {@link AppiumServiceBuilder}133 * @param capabilities take a look at {@link Capabilities}134 */135 public IOSDriver(AppiumServiceBuilder builder, Capabilities capabilities) {136 super(builder, ensurePlatformName(capabilities, PLATFORM_NAME));137 }138 /**139 * Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.140 *141 * @param builder take a look at {@link AppiumServiceBuilder}142 * @param httpClientFactory take a look at {@link HttpClient.Factory}143 * @param capabilities take a look at {@link Capabilities}144 */145 public IOSDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,146 Capabilities capabilities) {147 super(builder, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));148 }149 /**150 * Creates a new instance based on HTTP client factory and {@code capabilities}.151 *152 * @param httpClientFactory take a look at {@link HttpClient.Factory}153 * @param capabilities take a look at {@link Capabilities}154 */155 public IOSDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {156 super(httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));157 }158 /**159 * Creates a new instance based on {@code capabilities}.160 *161 * @param capabilities take a look at {@link Capabilities}162 */163 public IOSDriver(Capabilities capabilities) {164 super(ensurePlatformName(capabilities, PLATFORM_NAME));165 }166 @Override public TargetLocator switchTo() {167 return new InnerTargetLocator();168 }169 @SuppressWarnings("unchecked")170 @Override171 public IOSBatteryInfo getBatteryInfo() {172 return new IOSBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(173 "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue());174 }175 private class InnerTargetLocator extends RemoteTargetLocator {176 @Override public Alert alert() {177 return new IOSAlert(super.alert());178 }179 }180 class IOSAlert implements Alert {181 private final Alert alert;182 IOSAlert(Alert alert) {183 this.alert = alert;184 }185 @Override public void dismiss() {186 execute(DriverCommand.DISMISS_ALERT);187 }188 @Override public void accept() {189 execute(DriverCommand.ACCEPT_ALERT);190 }191 @Override public String getText() {192 Response response = execute(DriverCommand.GET_ALERT_TEXT);193 return response.getValue().toString();194 }195 @Override public void sendKeys(String keysToSend) {196 execute(DriverCommand.SET_ALERT_VALUE, prepareArguments("value", keysToSend));197 }198 }199 @Override200 public RemoteLocationContext getLocationContext() {201 return locationContext;202 }203 @Override204 public synchronized StringWebSocketClient getSyslogClient() {205 if (syslogClient == null) {206 syslogClient = new StringWebSocketClient();207 }208 return syslogClient;209 }210}...

Full Screen

Full Screen

GenericWebDriverManager.java

Source:GenericWebDriverManager.java Github

copy

Full Screen

...69 private <R> R runInNativeContext(Function<WebDriver, R> function)70 {71 if (isMobile())72 {73 SupportsContextSwitching contextSwitchingDriver = getUnwrappedDriver(SupportsContextSwitching.class);74 String originalContext = contextSwitchingDriver.getContext();75 if (!NATIVE_APP_CONTEXT.equals(originalContext))76 {77 switchToContext(contextSwitchingDriver, NATIVE_APP_CONTEXT);78 try79 {80 return function.apply(contextSwitchingDriver);81 }82 finally83 {84 switchToContext(contextSwitchingDriver, originalContext);85 }86 }87 return function.apply(contextSwitchingDriver);88 }89 return function.apply(webDriverProvider.get());90 }91 private void switchToContext(ContextAware contextAwareDriver, String contextName)92 {93 if (contextAwareDriver.getContextHandles().contains(contextName))94 {95 contextAwareDriver.context(contextName);96 }97 else98 {99 throw new IllegalStateException("MobileDriver doesn't have context: " + contextName);100 }101 }102 @Override103 public boolean isMobile()104 {105 Capabilities capabilities = getCapabilities();106 return isIOS(capabilities) || isAndroid(capabilities);107 }108 @Override109 public boolean isIOS()110 {111 return isIOS(getCapabilities());112 }113 public static boolean isIOS(Capabilities capabilities)...

Full Screen

Full Screen

WebDriverUtilTests.java

Source:WebDriverUtilTests.java Github

copy

Full Screen

1/*2 * Copyright 2019-2021 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * https://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.vividus.selenium;17import static org.junit.jupiter.api.Assertions.assertEquals;18import static org.junit.jupiter.api.Assertions.assertThrows;19import static org.mockito.Mockito.mock;20import static org.mockito.Mockito.withSettings;21import java.util.Objects;22import org.junit.jupiter.api.Test;23import org.openqa.selenium.WebDriver;24import org.openqa.selenium.WebElement;25import org.openqa.selenium.WrapsDriver;26import org.openqa.selenium.WrapsElement;27import org.openqa.selenium.remote.RemoteWebDriver;28import org.openqa.selenium.remote.RemoteWebElement;29import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;30import org.vividus.selenium.driver.TextFormattingWebDriver;31import org.vividus.selenium.element.DelegatingWebElement;32import org.vividus.selenium.element.TextFormattingWebElement;33import io.appium.java_client.remote.SupportsContextSwitching;34class WebDriverUtilTests35{36 @Test37 void testWrappedWebDriverUnwrap()38 {39 var remoteWebDriver = mock(RemoteWebDriver.class);40 var wrappingDriver = new TextFormattingWebDriver(remoteWebDriver);41 var actual = WebDriverUtil.unwrap(wrappingDriver, HasVirtualAuthenticator.class);42 assertEquals(remoteWebDriver, actual);43 }44 @Test45 void testNonWrappedWebDriverUnwrap()46 {47 var remoteWebDriver = mock(RemoteWebDriver.class);48 var actual = WebDriverUtil.unwrap(remoteWebDriver, HasVirtualAuthenticator.class);49 assertEquals(remoteWebDriver, actual);50 }51 @Test52 void testWrappedWebDriverUnwrapButNoCast()53 {54 var webDriver = mock(WebDriver.class);55 var wrappingDriver = new TextFormattingWebDriver(webDriver);56 assertThrows(ClassCastException.class,57 () -> WebDriverUtil.unwrap(wrappingDriver, HasVirtualAuthenticator.class));58 }59 @Test60 void testWrappedWebElementUnwrap()61 {62 var remoteWebElement = mock(RemoteWebElement.class);63 var wrappingElement = new TextFormattingWebElement(remoteWebElement);64 var actual = WebDriverUtil.unwrap(wrappingElement, WrapsDriver.class);65 assertEquals(remoteWebElement, actual);66 }67 @Test68 void testWrappedWebElementWithNewInterfaceUnwrap()69 {70 var webElement = mock(SupportsContextSwitching.class, withSettings().extraInterfaces(WebElement.class));71 var actual = WebDriverUtil.unwrap(new TestWebElement((WebElement) webElement), SupportsContextSwitching.class);72 assertEquals(webElement, actual);73 }74 @Test75 void testNonWrappedWebElementUnwrap()76 {77 var remoteWebElement = mock(RemoteWebElement.class);78 var actual = WebDriverUtil.unwrap(remoteWebElement, WrapsDriver.class);79 assertEquals(remoteWebElement, actual);80 }81 @Test82 void testWrappedWebElementUnwrapButNoCast()83 {84 var webElement = mock(WebElement.class);85 var wrappingElement = new TextFormattingWebElement(webElement);86 assertThrows(ClassCastException.class, () -> WebDriverUtil.unwrap(wrappingElement, WrapsDriver.class));87 }88 @SuppressWarnings("unchecked")89 static class TestWebElement extends DelegatingWebElement implements WrapsElement90 {91 private final WebElement wrappedElement;92 TestWebElement(WebElement wrappedElement)93 {94 super(wrappedElement);95 this.wrappedElement = wrappedElement;96 }97 @Override98 public WebElement getWrappedElement()99 {100 return wrappedElement;101 }102 @Override103 public boolean equals(Object o)104 {105 if (this == o)106 {107 return true;108 }109 if (o == null || getClass() != o.getClass())110 {111 return false;112 }113 if (!super.equals(o))114 {115 return false;116 }117 TestWebElement that = (TestWebElement) o;118 return Objects.equals(wrappedElement, that.wrappedElement);119 }120 @Override121 public int hashCode()122 {123 return Objects.hash(super.hashCode(), wrappedElement);124 }125 }126}...

Full Screen

Full Screen

SupportsContextSwitching.java

Source:SupportsContextSwitching.java Github

copy

Full Screen

...28import java.util.Set;29import static com.google.common.base.Preconditions.checkNotNull;30public interface SupportsContextSwitching extends WebDriver, ContextAware, ExecutesMethod {31 /**32 * Switches to the given context.33 *34 * @param name The name of the context to switch to.35 * @return self instance for chaining.36 */37 default WebDriver context(String name) {38 checkNotNull(name, "Must supply a context name");39 try {40 execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));41 return this;42 } catch (WebDriverException e) {43 throw new NoSuchContextException(e.getMessage(), e);44 }45 }46 /**47 * Get the names of available contexts.48 *49 * @return List list of context names.50 */51 default Set<String> getContextHandles() {52 Response response = execute(DriverCommand.GET_CONTEXT_HANDLES, ImmutableMap.of());53 Object value = response.getValue();54 try {55 //noinspection unchecked56 List<String> returnedValues = (List<String>) value;57 return new LinkedHashSet<>(returnedValues);58 } catch (ClassCastException ex) {59 throw new WebDriverException(60 "Returned value cannot be converted to List<String>: " + value, ex);61 }62 }63 /**64 * Get the name of the current context.65 *66 * @return Context name or null if it cannot be determined.67 */68 @Nullable69 default String getContext() {70 String contextName =71 String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());72 return "null".equalsIgnoreCase(contextName) ? null : contextName;73 }74}...

Full Screen

Full Screen

context

Using AI Code Generation

copy

Full Screen

1import java.net.MalformedURLException;2import java.net.URL;3import java.util.Set;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.testng.annotations.AfterClass;10import org.testng.annotations.BeforeClass;11import org.testng.annotations.Test;12import io.appium.java_client.AppiumDriver;13import io.appium.java_client.MobileElement;14import io.appium.java_client.android.AndroidDriver;15import io.appium.java_client.remote.MobileCapabilityType;16import io.appium.java_client.remote.MobilePlatform;17public class AppiumTest {18AppiumDriver driver;19public void setUp() throws MalformedURLException {20DesiredCapabilities capabilities = new DesiredCapabilities();21capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);22capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");23capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");24capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");

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.

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in SupportsContextSwitching

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful