下面列出了怎么用org.openqa.selenium.ScreenOrientation的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Dimension getSize()
{
if (isMobile())
{
Dimension dimension =
webDriverManagerContext.getParameter(WebDriverManagerParameter.SCREEN_SIZE);
if (dimension == null)
{
dimension = runInNativeContext(this::getSize);
webDriverManagerContext.putParameter(WebDriverManagerParameter.SCREEN_SIZE, dimension);
}
return isOrientation(ScreenOrientation.LANDSCAPE) ? new Dimension(dimension.height, dimension.width)
: dimension;
}
return getSize(getWebDriver());
}
@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;
}
@Test
void testGetSize()
{
MobileDriver<?> mobileDriver = mock(MobileDriver.class, withSettings().extraInterfaces(HasCapabilities.class));
Set<String> contexts = new HashSet<>();
contexts.add(WEBVIEW_CONTEXT);
contexts.add(NATIVE_APP_CONTEXT);
mockMobileDriverContext(mobileDriver, WEBVIEW_CONTEXT, contexts);
lenient().when(webDriverManagerContext.getParameter(WebDriverManagerParameter.SCREEN_SIZE)).thenReturn(null);
lenient().when(webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION))
.thenReturn(ScreenOrientation.PORTRAIT);
WebDriverManager spy = spyIsMobile(true);
Window window = mock(Window.class);
when(mockOptions(mobileDriver).window()).thenReturn(window);
when(window.getSize()).thenReturn(mock(Dimension.class));
assertNotNull(spy.getSize());
}
@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);
}
@Test
public void acceptsMobileCapabilities() throws MalformedURLException {
androidOptions.setApp(new URL("http://example.com/myapp.apk"))
.setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
.setPlatformVersion("10")
.setDeviceName("Pixel")
.setOtherApps("/path/to/app.apk")
.setLocale("fr_CA")
.setUdid("1ae203187fc012g")
.setOrientation(ScreenOrientation.LANDSCAPE)
.setNewCommandTimeout(Duration.ofSeconds(60))
.setLanguage("fr");
assertEquals("http://example.com/myapp.apk", androidOptions.getApp());
assertEquals(AutomationName.ANDROID_UIAUTOMATOR2, androidOptions.getAutomationName());
assertEquals("10", androidOptions.getPlatformVersion());
assertEquals("Pixel", androidOptions.getDeviceName());
assertEquals("/path/to/app.apk", androidOptions.getOtherApps());
assertEquals("fr_CA", androidOptions.getLocale());
assertEquals("1ae203187fc012g", androidOptions.getUdid());
assertEquals(ScreenOrientation.LANDSCAPE, androidOptions.getOrientation());
assertEquals(Duration.ofSeconds(60), androidOptions.getNewCommandTimeout());
assertEquals("fr", androidOptions.getLanguage());
}
protected boolean assertThatRotationListenerWorks(EmptyWebDriver driver, TestListener listener,
String prefix) {
try {
driver.rotate(ScreenOrientation.LANDSCAPE);
driver.rotate(ScreenOrientation.PORTRAIT);
assertThat(listener.messages,
contains(prefix + "Attempt to change screen orientation. The new screen orientation is "
+ ScreenOrientation.LANDSCAPE.toString(),
prefix + "The screen orientation has been changed to "
+ ScreenOrientation.LANDSCAPE.toString(),
prefix + "Attempt to change screen orientation. The new screen orientation is "
+ ScreenOrientation.PORTRAIT.toString(),
prefix + "The screen orientation has been changed to "
+ ScreenOrientation.PORTRAIT.toString()));
return true;
} finally {
listener.messages.clear();
}
}
@Test
public void acceptsMobileCapabilities() throws MalformedURLException {
iosOptions.setApp(new URL("http://example.com/myapp.apk"))
.setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
.setPlatformVersion("10")
.setDeviceName("Pixel")
.setOtherApps("/path/to/app.apk")
.setLocale("fr_CA")
.setUdid("1ae203187fc012g")
.setOrientation(ScreenOrientation.LANDSCAPE)
.setNewCommandTimeout(Duration.ofSeconds(60))
.setLanguage("fr");
assertEquals("http://example.com/myapp.apk", iosOptions.getApp());
assertEquals(AutomationName.ANDROID_UIAUTOMATOR2, iosOptions.getAutomationName());
assertEquals("10", iosOptions.getPlatformVersion());
assertEquals("Pixel", iosOptions.getDeviceName());
assertEquals("/path/to/app.apk", iosOptions.getOtherApps());
assertEquals("fr_CA", iosOptions.getLocale());
assertEquals("1ae203187fc012g", iosOptions.getUdid());
assertEquals(ScreenOrientation.LANDSCAPE, iosOptions.getOrientation());
assertEquals(Duration.ofSeconds(60), iosOptions.getNewCommandTimeout());
assertEquals("fr", iosOptions.getLanguage());
}
@Test
public void acceptsMobileCapabilities() throws MalformedURLException {
mobileOptions.setApp(new URL("http://example.com/myapp.apk"))
.setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
.setPlatformVersion("10")
.setDeviceName("Pixel")
.setOtherApps("/path/to/app.apk")
.setLocale("fr_CA")
.setUdid("1ae203187fc012g")
.setOrientation(ScreenOrientation.LANDSCAPE)
.setNewCommandTimeout(Duration.ofSeconds(60))
.setLanguage("fr");
assertEquals("http://example.com/myapp.apk", mobileOptions.getApp());
assertEquals(AutomationName.ANDROID_UIAUTOMATOR2, mobileOptions.getAutomationName());
assertEquals("10", mobileOptions.getPlatformVersion());
assertEquals("Pixel", mobileOptions.getDeviceName());
assertEquals("/path/to/app.apk", mobileOptions.getOtherApps());
assertEquals("fr_CA", mobileOptions.getLocale());
assertEquals("1ae203187fc012g", mobileOptions.getUdid());
assertEquals(ScreenOrientation.LANDSCAPE, mobileOptions.getOrientation());
assertEquals(Duration.ofSeconds(60), mobileOptions.getNewCommandTimeout());
assertEquals("fr", mobileOptions.getLanguage());
}
private AShot createAShot(ShootingStrategy baseShootingStrategy,
ScreenshotShootingStrategy screenshotShootingStrategy, boolean viewportScreenshot)
{
String deviceName = webDriverFactory.getCapability(SauceLabsCapabilityType.DEVICE_NAME, false);
boolean landscapeOrientation = webDriverManager.isOrientation(ScreenOrientation.LANDSCAPE);
ShootingStrategy shootingStrategy = screenshotShootingStrategy.getDecoratedShootingStrategy(
baseShootingStrategy, viewportScreenshot, landscapeOrientation, deviceName);
return new AShot().shootingStrategy(shootingStrategy)
.coordsProvider(screenshotShootingStrategy == ScreenshotShootingStrategy.SIMPLE
? CeilingJsCoordsProvider.getSimple(javascriptActions)
: CeilingJsCoordsProvider.getScrollAdjusted(javascriptActions));
}
@ParameterizedTest
@EnumSource(ScreenOrientation.class)
void shouldNotDetectOrientationForDesktop(ScreenOrientation orientation)
{
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, Platform.WIN10.toString()));
assertFalse(webDriverManager.isOrientation(orientation));
}
static Stream<Arguments> orientationProvider()
{
return Stream.of(
Arguments.of(ScreenOrientation.LANDSCAPE, ScreenOrientation.PORTRAIT, MobilePlatform.ANDROID),
Arguments.of(ScreenOrientation.PORTRAIT, ScreenOrientation.PORTRAIT, MobilePlatform.ANDROID),
Arguments.of(ScreenOrientation.LANDSCAPE, ScreenOrientation.PORTRAIT, MobilePlatform.IOS),
Arguments.of(ScreenOrientation.PORTRAIT, ScreenOrientation.PORTRAIT, MobilePlatform.IOS),
Arguments.of(ScreenOrientation.LANDSCAPE, ScreenOrientation.LANDSCAPE, MobilePlatform.ANDROID),
Arguments.of(ScreenOrientation.PORTRAIT, ScreenOrientation.LANDSCAPE, MobilePlatform.ANDROID),
Arguments.of(ScreenOrientation.LANDSCAPE, ScreenOrientation.LANDSCAPE, MobilePlatform.IOS),
Arguments.of(ScreenOrientation.PORTRAIT, ScreenOrientation.LANDSCAPE, MobilePlatform.IOS)
);
}
@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 testOrientationCached()
{
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, MobilePlatform.IOS));
when(webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION))
.thenReturn(ScreenOrientation.PORTRAIT);
assertTrue(webDriverManager.isOrientation(ScreenOrientation.PORTRAIT));
verifyNoInteractions(mobileDriver);
}
static Stream<Arguments> nativeApplicationViewportProvider()
{
return Stream.of(
Arguments.of(ScreenOrientation.LANDSCAPE, new Dimension(375, 667), new Dimension(667, 375)),
Arguments.of(ScreenOrientation.PORTRAIT, new Dimension(375, 667), new Dimension(375, 667))
);
}
@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 testScreenshots() throws Exception {
Thread.sleep(2000);
String desktop = System.getenv("HOME") + "/Desktop";
driver.rotate(ScreenOrientation.LANDSCAPE);
File regularScreenshot = driver.getScreenshotAs(OutputType.FILE);
driver.setSetting("screenshotOrientation", "landscapeRight");
File adjustedScreenshot = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(regularScreenshot, new File(desktop + "/screen1.png"));
FileUtils.copyFile(adjustedScreenshot, new File(desktop + "/screen2.png"));
}
/**
* @return orientation list
* @author wasiqb
* @since Oct 21, 2018
*/
@DataProvider
public static Iterator<Object[]> getOrientation() {
final List<Object[]> data = new ArrayList<>();
data.add(new Object[] { ScreenOrientation.LANDSCAPE });
data.add(new Object[] { ScreenOrientation.PORTRAIT });
return data.iterator();
}
/**
* @param orientation
* @author wasiqb
* @since Oct 21, 2018
*/
@Test(dataProvider = "getOrientation")
public void testRotation(final ScreenOrientation orientation) {
this.main.onDevice()
.rotate(orientation);
assertThat(this.main.onDevice()
.rotation()
.name()).isEqualTo(orientation.name());
}
/**
* Get screen orientation settings.
* <p>
* * @return ScreenOrientation.
*/
private ScreenOrientation getScreenOrientation() {
String orientation = this.properties.getProperty("orientation", "none");
if (orientation.toLowerCase().contains("port")) {
return ScreenOrientation.PORTRAIT;
} else if (orientation.toLowerCase().contains("land")) {
return ScreenOrientation.LANDSCAPE;
} else {
return null;
}
}
@Override
public ScreenOrientation getOrientation() {
Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION);
String orientation = response.getValue().toString().toLowerCase();
if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) {
return ScreenOrientation.LANDSCAPE;
} else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) {
return ScreenOrientation.PORTRAIT;
} else {
throw new WebDriverException("Unexpected orientation returned: " + orientation);
}
}
@Test
public void orientationTest() {
assertEquals(ScreenOrientation.PORTRAIT, driver.getOrientation());
driver.rotate(ScreenOrientation.LANDSCAPE);
assertEquals(ScreenOrientation.LANDSCAPE, driver.getOrientation());
driver.rotate(ScreenOrientation.PORTRAIT);
}
@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());
}
private void mockDeviceAndOrientation()
{
String deviceName = "Google pixel 3";
when(webDriverFactory.getCapability(SauceLabsCapabilityType.DEVICE_NAME, false)).thenReturn(deviceName);
when(webDriverManager.isOrientation(ScreenOrientation.LANDSCAPE)).thenReturn(true);
}
@Override
public ScreenOrientation getOrientation() {
return ((Rotatable) super.getWrappedDriver()).getOrientation();
}
@Override
public void rotate(ScreenOrientation screenOrientation) {
((Rotatable) super.getWrappedDriver()).rotate(screenOrientation);
}
public void setParameters( Object[] parameterArray )
{
setOrientation( (ScreenOrientation) parameterArray[ 0 ] );
}
@Override
public void rotate(ScreenOrientation orientation) {
execute(DriverCommand.SET_SCREEN_ORIENTATION,
ImmutableMap.of("orientation", orientation.value().toUpperCase()));
}
@Override public void beforeRotation(WebDriver driver, ScreenOrientation orientation) {
((RotationEventListener) dispatcher).beforeRotation(driver, orientation);
}
@Override public void afterRotation(WebDriver driver, ScreenOrientation orientation) {
((RotationEventListener) dispatcher).afterRotation(driver, orientation);
}
@Override public void rotate(ScreenOrientation orientation) {
//The rotation does nothing there
}