How to use setApp method of io.appium.java_client.remote.MobileOptions class

Best io.appium code snippet using io.appium.java_client.remote.MobileOptions.setApp

Run io.appium automation tests on LambdaTest cloud grid

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

MobileOptionsTest.java

Source: MobileOptionsTest.java Github

copy
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 additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * 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 and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.remote;
18
19import org.junit.Test;
20import org.openqa.selenium.MutableCapabilities;
21import org.openqa.selenium.ScreenOrientation;
22
23import java.net.MalformedURLException;
24import java.net.URL;
25import java.time.Duration;
26
27import static org.junit.Assert.*;
28
29public class MobileOptionsTest {
30    private MobileOptions<?> mobileOptions = new MobileOptions<>();
31
32    @Test
33    public void acceptsExistingCapabilities() {
34        MutableCapabilities capabilities = new MutableCapabilities();
35        capabilities.setCapability("deviceName", "Pixel");
36        capabilities.setCapability("platformVersion", "10");
37        capabilities.setCapability("newCommandTimeout", 60);
38
39        mobileOptions = new MobileOptions<>(capabilities);
40
41        assertEquals("Pixel", mobileOptions.getDeviceName());
42        assertEquals("10", mobileOptions.getPlatformVersion());
43        assertEquals(Duration.ofSeconds(60), mobileOptions.getNewCommandTimeout());
44    }
45
46    @Test
47    public void acceptsMobileCapabilities() throws MalformedURLException {
48        mobileOptions.setApp(new URL("http://example.com/myapp.apk"))
49                .setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
50                .setPlatformVersion("10")
51                .setDeviceName("Pixel")
52                .setOtherApps("/path/to/app.apk")
53                .setLocale("fr_CA")
54                .setUdid("1ae203187fc012g")
55                .setOrientation(ScreenOrientation.LANDSCAPE)
56                .setNewCommandTimeout(Duration.ofSeconds(60))
57                .setLanguage("fr");
58
59        assertEquals("http://example.com/myapp.apk", mobileOptions.getApp());
60        assertEquals(AutomationName.ANDROID_UIAUTOMATOR2, mobileOptions.getAutomationName());
61        assertEquals("10", mobileOptions.getPlatformVersion());
62        assertEquals("Pixel", mobileOptions.getDeviceName());
63        assertEquals("/path/to/app.apk", mobileOptions.getOtherApps());
64        assertEquals("fr_CA", mobileOptions.getLocale());
65        assertEquals("1ae203187fc012g", mobileOptions.getUdid());
66        assertEquals(ScreenOrientation.LANDSCAPE, mobileOptions.getOrientation());
67        assertEquals(Duration.ofSeconds(60), mobileOptions.getNewCommandTimeout());
68        assertEquals("fr", mobileOptions.getLanguage());
69    }
70
71    @Test
72    public void acceptsMobileBooleanCapabilityDefaults() {
73        mobileOptions.setClearSystemFiles()
74                .setAutoWebview()
75                .setEnablePerformanceLogging()
76                .setEventTimings()
77                .setAutoWebview()
78                .setFullReset()
79                .setPrintPageSourceOnFindFailure();
80
81        assertTrue(mobileOptions.doesClearSystemFiles());
82        assertTrue(mobileOptions.doesAutoWebview());
83        assertTrue(mobileOptions.isEnablePerformanceLogging());
84        assertTrue(mobileOptions.doesEventTimings());
85        assertTrue(mobileOptions.doesAutoWebview());
86        assertTrue(mobileOptions.doesFullReset());
87        assertTrue(mobileOptions.doesPrintPageSourceOnFindFailure());
88    }
89
90    @Test
91    public void setsMobileBooleanCapabilities() {
92        mobileOptions.setClearSystemFiles(false)
93                .setAutoWebview(false)
94                .setEnablePerformanceLogging(false)
95                .setEventTimings(false)
96                .setAutoWebview(false)
97                .setFullReset(false)
98                .setPrintPageSourceOnFindFailure(false);
99
100        assertFalse(mobileOptions.doesClearSystemFiles());
101        assertFalse(mobileOptions.doesAutoWebview());
102        assertFalse(mobileOptions.isEnablePerformanceLogging());
103        assertFalse(mobileOptions.doesEventTimings());
104        assertFalse(mobileOptions.doesAutoWebview());
105        assertFalse(mobileOptions.doesFullReset());
106        assertFalse(mobileOptions.doesPrintPageSourceOnFindFailure());
107    }
108}
109
Full Screen

MobileOptions.java

Source: MobileOptions.java Github

copy
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 additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * 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 and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.remote;
18
19import org.openqa.selenium.Capabilities;
20import org.openqa.selenium.MutableCapabilities;
21import org.openqa.selenium.ScreenOrientation;
22import org.openqa.selenium.remote.CapabilityType;
23
24import java.net.URL;
25import java.time.Duration;
26
27/**
28 * Use the specific options class for your driver,
29 * for example XCUITestOptions or UiAutomator2Options.
30 *
31 * @param <T> The child class for a proper chaining.
32 */
33@Deprecated
34public class MobileOptions<T extends MobileOptions<T>> extends MutableCapabilities {
35
36    /**
37     * Creates new instance with no preset capabilities.
38     */
39    public MobileOptions() {
40    }
41
42    /**
43     * Creates new instance with provided capabilities capabilities.
44     *
45     * @param source is Capabilities instance to merge into new instance
46     */
47    public MobileOptions(Capabilities source) {
48        super(source);
49    }
50
51    /**
52     * Set the kind of mobile device or emulator to use.
53     *
54     * @param platform the kind of mobile device or emulator to use.
55     * @return this MobileOptions, for chaining.
56     * @see org.openqa.selenium.remote.CapabilityType#PLATFORM_NAME
57     */
58    public T setPlatformName(String platform) {
59        return amend(CapabilityType.PLATFORM_NAME, platform);
60    }
61
62    /**
63     * Set the absolute local path for the location of the App.
64     * The  or remote http URL to a {@code .ipa} file (IOS),
65     *
66     * @param path is a String representing the location of the App
67     * @return this MobileOptions, for chaining.
68     * @see MobileCapabilityType#APP
69     */
70    public T setApp(String path) {
71        return amend(MobileCapabilityType.APP, path);
72    }
73
74    /**
75     * Set the remote http URL for the location of the App.
76     *
77     * @param url is the URL representing the location of the App
78     * @return this MobileOptions, for chaining.
79     * @see MobileCapabilityType#APP
80     */
81    public T setApp(URL url) {
82        return setApp(url.toString());
83    }
84
85    /**
86     * Get the app location.
87     *
88     * @return String representing app location
89     * @see MobileCapabilityType#APP
90     */
91    public String getApp() {
92        return (String) getCapability(MobileCapabilityType.APP);
93    }
94
95    /**
96     * Set the automation engine to use.
97     *
98     * @param name is the name of the automation engine
99     * @return this MobileOptions, for chaining.
100     * @see MobileCapabilityType#AUTOMATION_NAME
101     */
102    public T setAutomationName(String name) {
103        return amend(MobileCapabilityType.AUTOMATION_NAME, name);
104    }
105
106    /**
107     * Get the automation engine to use.
108     *
109     * @return String representing the name of the automation engine
110     * @see MobileCapabilityType#AUTOMATION_NAME
111     */
112    public String getAutomationName() {
113        return (String) getCapability(MobileCapabilityType.AUTOMATION_NAME);
114    }
115
116    /**
117     * Set the app to move directly into Webview context.
118     *
119     * @return this MobileOptions, for chaining.
120     * @see MobileCapabilityType#AUTO_WEBVIEW
121     */
122    public T setAutoWebview() {
123        return setAutoWebview(true);
124    }
125
126    /**
127     * Set whether the app moves directly into Webview context.
128     *
129     * @param bool is whether the app moves directly into Webview context.
130     * @return this MobileOptions, for chaining.
131     * @see MobileCapabilityType#AUTO_WEBVIEW
132     */
133    public T setAutoWebview(boolean bool) {
134        return amend(MobileCapabilityType.AUTO_WEBVIEW, bool);
135    }
136
137    /**
138     * Get whether the app moves directly into Webview context.
139     *
140     * @return true if app moves directly into Webview context.
141     * @see MobileCapabilityType#AUTO_WEBVIEW
142     */
143    public boolean doesAutoWebview() {
144        return (boolean) getCapability(MobileCapabilityType.AUTO_WEBVIEW);
145    }
146
147    /**
148     * Set the app to delete any generated files at the end of a session.
149     *
150     * @return this MobileOptions, for chaining.
151     * @see MobileCapabilityType#CLEAR_SYSTEM_FILES
152     */
153    public T setClearSystemFiles() {
154        return setClearSystemFiles(true);
155    }
156
157    /**
158     * Set whether the app deletes generated files at the end of a session.
159     *
160     * @param bool is whether the app deletes generated files at the end of a session.
161     * @return this MobileOptions, for chaining.
162     * @see MobileCapabilityType#CLEAR_SYSTEM_FILES
163     */
164    public T setClearSystemFiles(boolean bool) {
165        return amend(MobileCapabilityType.CLEAR_SYSTEM_FILES, bool);
166    }
167
168    /**
169     * Get whether the app deletes generated files at the end of a session.
170     *
171     * @return true if the app deletes generated files at the end of a session.
172     * @see MobileCapabilityType#CLEAR_SYSTEM_FILES
173     */
174    public boolean doesClearSystemFiles() {
175        return (boolean) getCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES);
176    }
177
178    /**
179     * Set the name of the device.
180     *
181     * @param deviceName is the name of the device.
182     * @return this MobileOptions, for chaining.
183     * @see MobileCapabilityType#DEVICE_NAME
184     */
185    public T setDeviceName(String deviceName) {
186        return amend(MobileCapabilityType.DEVICE_NAME, deviceName);
187    }
188
189    /**
190     * Get the name of the device.
191     *
192     * @return String representing the name of the device.
193     * @see MobileCapabilityType#DEVICE_NAME
194     */
195    public String getDeviceName() {
196        return (String) getCapability(MobileCapabilityType.DEVICE_NAME);
197    }
198
199    /**
200     * Set the app to enable performance logging.
201     *
202     * @return this MobileOptions, for chaining.
203     * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING
204     */
205    public T setEnablePerformanceLogging() {
206        return setEnablePerformanceLogging(true);
207    }
208
209    /**
210     * Set whether the app logs performance.
211     *
212     * @param bool is whether the app logs performance.
213     * @return this MobileOptions, for chaining.
214     * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING
215     */
216    public T setEnablePerformanceLogging(boolean bool) {
217        return amend(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING, bool);
218    }
219
220    /**
221     * Get the app logs performance.
222     *
223     * @return true if the app logs performance.
224     * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING
225     */
226    public boolean isEnablePerformanceLogging() {
227        return (boolean) getCapability(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING);
228    }
229
230    /**
231     * Set the app to report the timings for various Appium-internal events.
232     *
233     * @return this MobileOptions, for chaining.
234     * @see MobileCapabilityType#EVENT_TIMINGS
235     */
236    public T setEventTimings() {
237        return setEventTimings(true);
238    }
239
240    /**
241     * Set whether the app reports the timings for various Appium-internal events.
242     *
243     * @param bool is whether the app enables event timings.
244     * @return this MobileOptions, for chaining.
245     * @see MobileCapabilityType#EVENT_TIMINGS
246     */
247    public T setEventTimings(boolean bool) {
248        return amend(MobileCapabilityType.EVENT_TIMINGS, bool);
249    }
250
251    /**
252     * Get whether the app reports the timings for various Appium-internal events.
253     *
254     * @return true if the app reports event timings.
255     * @see MobileCapabilityType#EVENT_TIMINGS
256     */
257    public boolean doesEventTimings() {
258        return (boolean) getCapability(MobileCapabilityType.EVENT_TIMINGS);
259    }
260
261    /**
262     * Set the app to do a full reset.
263     *
264     * @return this MobileOptions, for chaining.
265     * @see MobileCapabilityType#FULL_RESET
266     */
267    public T setFullReset() {
268        return setFullReset(true);
269    }
270
271    /**
272     * Set whether the app does a full reset.
273     *
274     * @param bool is whether the app does a full reset.
275     * @return this MobileOptions, for chaining.
276     * @see MobileCapabilityType#FULL_RESET
277     */
278    public T setFullReset(boolean bool) {
279        return amend(MobileCapabilityType.FULL_RESET, bool);
280    }
281
282    /**
283     * Get whether the app does a full reset.
284     *
285     * @return true if the app does a full reset.
286     * @see MobileCapabilityType#FULL_RESET
287     */
288    public boolean doesFullReset() {
289        return (boolean) getCapability(MobileCapabilityType.FULL_RESET);
290    }
291
292    /**
293     * Set language abbreviation for use in session.
294     *
295     * @param language is the language abbreviation.
296     * @return this MobileOptions, for chaining.
297     * @see MobileCapabilityType#LANGUAGE
298     */
299    public T setLanguage(String language) {
300        return amend(MobileCapabilityType.LANGUAGE, language);
301    }
302
303    /**
304     * Get language abbreviation for use in session.
305     *
306     * @return String representing the language abbreviation.
307     * @see MobileCapabilityType#LANGUAGE
308     */
309    public String getLanguage() {
310        return (String) getCapability(MobileCapabilityType.LANGUAGE);
311    }
312
313    /**
314     * Set locale abbreviation for use in session.
315     *
316     * @param locale is the locale abbreviation.
317     * @return this MobileOptions, for chaining.
318     * @see MobileCapabilityType#LOCALE
319     */
320    public T setLocale(String locale) {
321        return amend(MobileCapabilityType.LOCALE, locale);
322    }
323
324    /**
325     * Get locale abbreviation for use in session.
326     *
327     * @return String representing the locale abbreviation.
328     * @see MobileCapabilityType#LOCALE
329     */
330    public String getLocale() {
331        return (String) getCapability(MobileCapabilityType.LOCALE);
332    }
333
334    /**
335     * Set the timeout for new commands.
336     *
337     * @param duration is the allowed time before seeing a new command.
338     * @return this MobileOptions, for chaining.
339     * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT
340     */
341    public T setNewCommandTimeout(Duration duration) {
342        return amend(MobileCapabilityType.NEW_COMMAND_TIMEOUT, duration.getSeconds());
343    }
344
345    /**
346     * Get the timeout for new commands.
347     *
348     * @return allowed time before seeing a new command.
349     * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT
350     */
351    public Duration getNewCommandTimeout() {
352        Object duration = getCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT);
353        return Duration.ofSeconds(Long.parseLong("" + duration));
354    }
355
356    /**
357     * Set the app not to do a reset.
358     *
359     * @return this MobileOptions, for chaining.
360     * @see MobileCapabilityType#NO_RESET
361     */
362    public T setNoReset() {
363        return setNoReset(true);
364    }
365
366    /**
367     * Set whether the app does not do a reset.
368     *
369     * @param bool is whether the app does not do a reset.
370     * @return this MobileOptions, for chaining.
371     * @see MobileCapabilityType#NO_RESET
372     */
373    public T setNoReset(boolean bool) {
374        return amend(MobileCapabilityType.NO_RESET, bool);
375    }
376
377    /**
378     * Get whether the app does not do a reset.
379     *
380     * @return true if the app does not do a reset.
381     * @see MobileCapabilityType#NO_RESET
382     */
383    public boolean doesNoReset() {
384        return (boolean) getCapability(MobileCapabilityType.NO_RESET);
385    }
386
387    /**
388     * Set the orientation of the screen.
389     *
390     * @param orientation is the screen orientation.
391     * @return this MobileOptions, for chaining.
392     * @see MobileCapabilityType#ORIENTATION
393     */
394    public T setOrientation(ScreenOrientation orientation) {
395        return amend(MobileCapabilityType.ORIENTATION, orientation);
396    }
397
398    /**
399     * Get the orientation of the screen.
400     *
401     * @return ScreenOrientation of the app.
402     * @see MobileCapabilityType#ORIENTATION
403     */
404    public ScreenOrientation getOrientation() {
405        return (ScreenOrientation) getCapability(MobileCapabilityType.ORIENTATION);
406    }
407
408    /**
409     * Set the location of the app(s) to install before running a test.
410     *
411     * @param apps is the apps to install.
412     * @return this MobileOptions, for chaining.
413     * @see MobileCapabilityType#OTHER_APPS
414     */
415    public T setOtherApps(String apps) {
416        return amend(MobileCapabilityType.OTHER_APPS, apps);
417    }
418
419    /**
420     * Get the list of apps to install before running a test.
421     *
422     * @return String of apps to install.
423     * @see MobileCapabilityType#OTHER_APPS
424     */
425    public String getOtherApps() {
426        return (String) getCapability(MobileCapabilityType.OTHER_APPS);
427    }
428
429    /**
430     * Set the version of the platform.
431     *
432     * @param version is the platform version.
433     * @return this MobileOptions, for chaining.
434     * @see MobileCapabilityType#PLATFORM_VERSION
435     */
436    public T setPlatformVersion(String version) {
437        return amend(MobileCapabilityType.PLATFORM_VERSION, version);
438    }
439
440    /**
441     * Get the version of the platform.
442     *
443     * @return String representing the platform version.
444     * @see MobileCapabilityType#PLATFORM_VERSION
445     */
446    public String getPlatformVersion() {
447        return (String) getCapability(MobileCapabilityType.PLATFORM_VERSION);
448    }
449
450    /**
451     * Set the app to print page source when a find operation fails.
452     *
453     * @return this MobileOptions, for chaining.
454     * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE
455     */
456    public T setPrintPageSourceOnFindFailure() {
457        return setPrintPageSourceOnFindFailure(true);
458    }
459
460    /**
461     * Set whether the app to print page source when a find operation fails.
462     *
463     * @param bool is whether to print page source.
464     * @return this MobileOptions, for chaining.
465     * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE
466     */
467    public T setPrintPageSourceOnFindFailure(boolean bool) {
468        return amend(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE, bool);
469    }
470
471    /**
472     * Get whether the app to print page source when a find operation fails.
473     *
474     * @return true if app prints page source.
475     * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE
476     */
477    public boolean doesPrintPageSourceOnFindFailure() {
478        return (boolean) getCapability(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE);
479    }
480
481    /**
482     * Set the id of the device.
483     *
484     * @param id is the unique device identifier.
485     * @return this MobileOptions, for chaining.
486     * @see MobileCapabilityType#UDID
487     */
488    public T setUdid(String id) {
489        return amend(MobileCapabilityType.UDID, id);
490    }
491
492    /**
493     * Get the id of the device.
494     *
495     * @return String representing the unique device identifier.
496     * @see MobileCapabilityType#UDID
497     */
498    public String getUdid() {
499        return (String) getCapability(MobileCapabilityType.UDID);
500    }
501
502    @Override
503    public T merge(Capabilities extraCapabilities) {
504        super.merge(extraCapabilities);
505        return (T) this;
506    }
507
508    protected T amend(String optionName, Object value) {
509        setCapability(optionName, value);
510        return (T) this;
511    }
512}
513
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
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)