下面列出了怎么用org.openqa.selenium.Rotatable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean isOrientation(ScreenOrientation orientation)
{
if (!isMobile())
{
return false;
}
ScreenOrientation screenOrientation =
webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION);
if (screenOrientation == null)
{
screenOrientation = webDriverProvider.getUnwrapped(Rotatable.class).getOrientation();
webDriverManagerContext.putParameter(WebDriverManagerParameter.ORIENTATION, screenOrientation);
}
return orientation == screenOrientation;
}
@ParameterizedTest
@MethodSource("nativeApplicationViewportProvider")
void testGetScreenSizeForPortraitOrientation(ScreenOrientation orientation, Dimension dimension,
Dimension viewport)
{
lenient().when(webDriverProvider.getUnwrapped(MobileDriver.class)).thenReturn(mobileDriver);
lenient().when(webDriverProvider.getUnwrapped(Rotatable.class)).thenReturn(mobileDriver);
when(mobileDriver.getOrientation()).thenReturn(orientation);
when(mobileDriver.getContext()).thenReturn(NATIVE_APP_CONTEXT);
WebDriverManager spy = spyIsMobile(true);
Options options = mock(Options.class);
when(mobileDriver.manage()).thenReturn(options);
Window window = mock(Window.class);
when(options.window()).thenReturn(window);
when(window.getSize()).thenReturn(dimension);
assertEquals(viewport, spy.getSize());
verify(webDriverManagerContext).putParameter(WebDriverManagerParameter.SCREEN_SIZE, dimension);
}
private Map<String, Object> getDescription(WebDriver instance, Capabilities capabilities) {
DesiredCapabilities caps = new DesiredCapabilities(capabilities.asMap());
caps.setJavascriptEnabled(instance instanceof JavascriptExecutor);
if (instance instanceof TakesScreenshot) {
caps.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
}
if (instance instanceof LocationContext) {
caps.setCapability(CapabilityType.SUPPORTS_LOCATION_CONTEXT, true);
}
if (instance instanceof ApplicationCache) {
caps.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, true);
}
if (instance instanceof NetworkConnection) {
caps.setCapability(CapabilityType.SUPPORTS_NETWORK_CONNECTION, true);
}
if (instance instanceof WebStorage) {
caps.setCapability(CapabilityType.SUPPORTS_WEB_STORAGE, true);
}
if (instance instanceof Rotatable) {
caps.setCapability(CapabilityType.ROTATABLE, true);
}
if (instance instanceof HasTouchScreen) {
caps.setCapability(CapabilityType.HAS_TOUCHSCREEN, true);
}
return caps.asMap();
}
@ParameterizedTest
@MethodSource("orientationProvider")
void testIsOrientation(ScreenOrientation actualOrientation, ScreenOrientation orientationToCheck, String platform)
{
when(webDriverProvider.getUnwrapped(Rotatable.class)).thenReturn(mobileDriver);
when(webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION)).thenReturn(null);
when(mobileDriver.getOrientation()).thenReturn(actualOrientation);
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, platform));
assertEquals(actualOrientation == orientationToCheck, webDriverManager.isOrientation(orientationToCheck));
verify(webDriverManagerContext).putParameter(WebDriverManagerParameter.ORIENTATION, actualOrientation);
}
@Test
void testGetNativeApplicationViewportCached()
{
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, MobilePlatform.IOS));
when(webDriverProvider.getUnwrapped(Rotatable.class)).thenReturn(mobileDriver);
lenient().when(webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION)).thenReturn(null);
when(mobileDriver.getOrientation()).thenReturn(ScreenOrientation.PORTRAIT);
Dimension dimension = new Dimension(375, 667);
lenient().when(webDriverManagerContext.getParameter(WebDriverManagerParameter.SCREEN_SIZE))
.thenReturn(dimension);
Dimension actualDimension = webDriverManager.getSize();
assertEquals(dimension.getHeight(), actualDimension.getHeight());
assertEquals(dimension.getWidth(), actualDimension.getWidth());
verify(this.mobileDriver, never()).manage();
}
@Test
public void shouldBeAbleToAugmentMultipleTimes() {
Capabilities caps = new ImmutableCapabilities("canRotate", true, "magic.numbers", true);
StubExecutor stubExecutor = new StubExecutor(caps);
stubExecutor.expect(DriverCommand.GET_SCREEN_ORIENTATION,
Collections.emptyMap(),
ScreenOrientation.PORTRAIT.name());
RemoteWebDriver driver = new RemoteWebDriver(stubExecutor, caps);
WebDriver augmented = getAugmenter()
.addDriverAugmentation(
"canRotate",
Rotatable.class,
(c, exe) -> new RemoteRotatable(exe))
.augment(driver);
assertThat(driver).isNotSameAs(augmented);
assertThat(augmented).isInstanceOf(Rotatable.class);
assertThat(augmented).isNotInstanceOf(HasMagicNumbers.class);
WebDriver augmentedAgain = getAugmenter()
.addDriverAugmentation(
"magic.numbers",
HasMagicNumbers.class,
(c, exe) -> () -> 42)
.augment(augmented);
assertThat(augmented).isNotSameAs(augmentedAgain);
assertThat(augmentedAgain).isInstanceOf(Rotatable.class);
assertThat(augmentedAgain).isInstanceOf(HasMagicNumbers.class);
((Rotatable) augmentedAgain).getOrientation(); // Should not throw.
assertThat(((HasCapabilities) augmentedAgain).getCapabilities())
.isSameAs(driver.getCapabilities());
}
@Override
public ScreenOrientation getOrientation() {
return ((Rotatable) super.getWrappedDriver()).getOrientation();
}
@Override
public void rotate(DeviceRotation deviceRotation) {
((Rotatable) super.getWrappedDriver()).rotate(deviceRotation);
}
@Override
public DeviceRotation rotation() {
return ((Rotatable) super.getWrappedDriver()).rotation();
}
@Override
public void rotate(ScreenOrientation screenOrientation) {
((Rotatable) super.getWrappedDriver()).rotate(screenOrientation);
}
@Override
public Class<Rotatable> getDescribedInterface() {
return Rotatable.class;
}
@Override
public Rotatable getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new RemoteRotatable(executeMethod);
}
@Override
public Void call() {
((Rotatable) getUnwrappedDriver()).rotate(orientation);
return null;
}
@Override
public ScreenOrientation call() {
return ((Rotatable) getUnwrappedDriver()).getOrientation();
}