How to use Enum ScreenOrientation class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.Enum ScreenOrientation

Run Selenium automation tests on LambdaTest cloud grid

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

copy
1/*
2Copyright 2011 Selenium committers
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15 */
16
17package org.openqa.selenium.android;
18
19import com.google.common.collect.ImmutableMap;
20
21import org.openqa.selenium.Capabilities;
22import org.openqa.selenium.HasTouchScreen;
23import org.openqa.selenium.OutputType;
24import org.openqa.selenium.Rotatable;
25import org.openqa.selenium.ScreenOrientation;
26import org.openqa.selenium.TakesScreenshot;
27import org.openqa.selenium.TouchScreen;
28import org.openqa.selenium.WebDriverException;
29import org.openqa.selenium.html5.AppCacheStatus;
30import org.openqa.selenium.html5.ApplicationCache;
31import org.openqa.selenium.html5.BrowserConnection;
32import org.openqa.selenium.html5.LocalStorage;
33import org.openqa.selenium.html5.Location;
34import org.openqa.selenium.html5.LocationContext;
35import org.openqa.selenium.html5.SessionStorage;
36import org.openqa.selenium.html5.WebStorage;
37import org.openqa.selenium.remote.CapabilityType;
38import org.openqa.selenium.remote.DesiredCapabilities;
39import org.openqa.selenium.remote.DriverCommand;
40import org.openqa.selenium.remote.FileDetector;
41import org.openqa.selenium.remote.RemoteTouchScreen;
42import org.openqa.selenium.remote.RemoteWebDriver;
43import org.openqa.selenium.remote.html5.RemoteLocalStorage;
44import org.openqa.selenium.remote.html5.RemoteLocationContext;
45import org.openqa.selenium.remote.html5.RemoteSessionStorage;
46
47import java.net.MalformedURLException;
48import java.net.URL;
49
50/**
51 * A driver for running tests on an Android device or emulator.
52 */
53public class AndroidDriver extends RemoteWebDriver implements TakesScreenshot, Rotatable,
54    BrowserConnection, HasTouchScreen, WebStorage, LocationContext, ApplicationCache {
55
56  private TouchScreen touch;
57  private RemoteLocalStorage localStorage;
58  private RemoteSessionStorage sessionStorage;
59  private RemoteLocationContext locationContext;
60
61  /**
62   * The default constructor assumes the remote server is listening at http://localhost:8080/wd/hub
63   */
64  public AndroidDriver() {
65    this(getDefaultUrl());
66  }
67
68  public AndroidDriver(Capabilities ignored) {
69    this();
70  }
71
72  public AndroidDriver(String remoteAddress) throws MalformedURLException {
73    this(new URL(remoteAddress));
74  }
75
76  public AndroidDriver(DesiredCapabilities caps) {
77    this(getDefaultUrl(), caps);
78  }
79
80  public AndroidDriver(URL remoteAddress) {
81    super(remoteAddress, getAndroidCapabilities(null));
82    init();
83  }
84
85  public AndroidDriver(URL url, DesiredCapabilities caps) {
86    super(url, getAndroidCapabilities(caps));
87    init();
88  }
89
90  private void init() {
91    touch = new RemoteTouchScreen(getExecuteMethod());
92    localStorage = new RemoteLocalStorage(getExecuteMethod());
93    sessionStorage = new RemoteSessionStorage(getExecuteMethod());
94    locationContext = new RemoteLocationContext(getExecuteMethod());
95  }
96
97  @Override
98  public void setFileDetector(FileDetector detector) {
99    throw new WebDriverException(
100        "Setting the file detector only works on remote webdriver instances obtained " +
101        "via RemoteWebDriver");
102  }
103
104  public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
105    String base64Png = execute(DriverCommand.SCREENSHOT).getValue().toString();
106    return target.convertFromBase64Png(base64Png);
107  }
108
109  public boolean isOnline() {
110    return (Boolean) execute(DriverCommand.IS_BROWSER_ONLINE).getValue();
111  }
112
113  public void setOnline(boolean online) throws WebDriverException {
114    execute(DriverCommand.SET_BROWSER_ONLINE, ImmutableMap.of("state", online));
115  }
116
117  private static DesiredCapabilities getAndroidCapabilities(DesiredCapabilities userPrefs) {
118    DesiredCapabilities caps = DesiredCapabilities.android();
119    caps.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
120    caps.setCapability(CapabilityType.ROTATABLE, true);
121    caps.setCapability(CapabilityType.SUPPORTS_BROWSER_CONNECTION, true);
122    if (userPrefs != null) {
123      caps.merge(userPrefs);
124    }
125    return caps;
126  }
127
128  public void rotate(ScreenOrientation orientation) {
129    execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation));
130  }
131
132  public ScreenOrientation getOrientation() {
133    return ScreenOrientation.valueOf(
134        (String) execute(DriverCommand.GET_SCREEN_ORIENTATION).getValue());
135  }
136
137  private static URL getDefaultUrl() {
138    try {
139      return new URL("http://localhost:8080/wd/hub");
140    } catch (MalformedURLException e) {
141      throw new WebDriverException("Malformed default remote URL: " + e.getMessage());
142    }
143  }
144
145  public TouchScreen getTouch() {
146    return touch;
147  }
148
149  public LocalStorage getLocalStorage() {
150    return localStorage;
151  }
152
153  public SessionStorage getSessionStorage() {
154    return sessionStorage;
155  }
156
157  public Location location() {
158    return locationContext.location();
159  }
160
161  public void setLocation(Location loc) {
162    locationContext.setLocation(loc);
163  }
164
165  public AppCacheStatus getStatus() {
166    String status = (String) execute(DriverCommand.GET_APP_CACHE_STATUS).getValue();
167    return AppCacheStatus.getEnum(status);
168  }
169}
170
Full Screen
copy
1public enum ScreenOrientation {
2
3    LANDSCAPE_FIXED("LANDSCAPE_FIXED", 0);
4    // other values
5
6    private final String s;
7    private final int i;
8
9    private ScreenOrientation(String s, int i) {
10        this.s = s;
11        this.i = i;
12    }
13
14    // getters for s and i
15
16    @Override
17    public String toString() { 
18        return this.getS();
19    }
20
21    public static ScreenOrientation fromS(String s) {
22        for( ScreenOrientation so : values())
23            if(so.getS().equalsIgnoreCase(s)) {
24                return so;
25        }
26       throw new IllegalArgumentException();
27    }
28}
29
Full Screen
copy
1public enum ScreenOrientation {
2
3    LANDSCAPE_FIXED("LANDSCAPE_FIXED", 0);
4    // other values
5
6    private final String s;
7    private final int i;
8
9    private ScreenOrientation(String s, int i) {
10        this.s = s;
11        this.i = i;
12    }
13
14    // getters for s and i
15
16    @Override
17    public String toString() { 
18        return this.getS();
19    }
20
21    public static ScreenOrientation fromS(String s) {
22        for( ScreenOrientation so : values())
23            if(so.getS().equalsIgnoreCase(s)) {
24                return so;
25        }
26       throw new IllegalArgumentException();
27    }
28}
29
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on Enum-ScreenOrientation

Most used methods in Enum-ScreenOrientation

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)