How to use deleteC method of io.appium.java_client.MobileCommand class

Best io.appium code snippet using io.appium.java_client.MobileCommand.deleteC

Run io.appium automation tests on LambdaTest cloud grid

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

AppiumDriver.java

Source: AppiumDriver.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;
18
19import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
20import static io.appium.java_client.remote.MobileCapabilityType.AUTOMATION_NAME;
21import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME;
22import static org.apache.commons.lang3.StringUtils.isBlank;
23
24import io.appium.java_client.internal.CapabilityHelpers;
25import io.appium.java_client.remote.AppiumCommandExecutor;
26import io.appium.java_client.remote.AppiumNewSessionCommandPayload;
27import io.appium.java_client.remote.MobileCapabilityType;
28import io.appium.java_client.remote.options.BaseOptions;
29import io.appium.java_client.service.local.AppiumDriverLocalService;
30import io.appium.java_client.service.local.AppiumServiceBuilder;
31import org.openqa.selenium.Capabilities;
32import org.openqa.selenium.ImmutableCapabilities;
33import org.openqa.selenium.MutableCapabilities;
34import org.openqa.selenium.SessionNotCreatedException;
35import org.openqa.selenium.WebDriverException;
36import org.openqa.selenium.remote.CapabilityType;
37import org.openqa.selenium.remote.DriverCommand;
38import org.openqa.selenium.remote.ErrorHandler;
39import org.openqa.selenium.remote.ExecuteMethod;
40import org.openqa.selenium.remote.HttpCommandExecutor;
41import org.openqa.selenium.remote.RemoteWebDriver;
42import org.openqa.selenium.remote.Response;
43import org.openqa.selenium.remote.html5.RemoteLocationContext;
44import org.openqa.selenium.remote.http.HttpClient;
45import org.openqa.selenium.remote.http.HttpMethod;
46
47import java.lang.reflect.Field;
48import java.net.URL;
49import java.util.Arrays;
50import java.util.Collections;
51import java.util.Map;
52
53/**
54 * Default Appium driver implementation.
55 */
56public class AppiumDriver extends RemoteWebDriver implements
57        ExecutesMethod,
58        ComparesImages,
59        ExecutesDriverScript,
60        LogsEvents,
61        HasBrowserCheck,
62        HasSettings {
63
64    private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
65    // frequently used command parameters
66    private final URL remoteAddress;
67    protected final RemoteLocationContext locationContext;
68    private final ExecuteMethod executeMethod;
69
70    /**
71     * Creates a new instance based on command {@code executor} and {@code capabilities}.
72     *
73     * @param executor     is an instance of {@link HttpCommandExecutor}
74     *                     or class that extends it. Default commands or another vendor-specific
75     *                     commands may be specified there.
76     * @param capabilities take a look at {@link Capabilities}
77     */
78    public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
79        super(executor, capabilities);
80        this.executeMethod = new AppiumExecutionMethod(this);
81        locationContext = new RemoteLocationContext(executeMethod);
82        super.setErrorHandler(errorHandler);
83        this.remoteAddress = executor.getAddressOfRemoteServer();
84    }
85
86    public AppiumDriver(URL remoteAddress, Capabilities capabilities) {
87        this(new AppiumCommandExecutor(MobileCommand.commandRepository, remoteAddress),
88                capabilities);
89    }
90
91    public AppiumDriver(URL remoteAddress, HttpClient.Factory httpClientFactory,
92                        Capabilities capabilities) {
93        this(new AppiumCommandExecutor(MobileCommand.commandRepository, remoteAddress,
94                httpClientFactory), capabilities);
95    }
96
97    public AppiumDriver(AppiumDriverLocalService service, Capabilities capabilities) {
98        this(new AppiumCommandExecutor(MobileCommand.commandRepository, service),
99                capabilities);
100    }
101
102    public AppiumDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,
103                        Capabilities capabilities) {
104        this(new AppiumCommandExecutor(MobileCommand.commandRepository, service, httpClientFactory),
105                capabilities);
106    }
107
108    public AppiumDriver(AppiumServiceBuilder builder, Capabilities capabilities) {
109        this(builder.build(), capabilities);
110    }
111
112    public AppiumDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
113                        Capabilities capabilities) {
114        this(builder.build(), httpClientFactory, capabilities);
115    }
116
117    public AppiumDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {
118        this(AppiumDriverLocalService.buildDefaultService(), httpClientFactory,
119                capabilities);
120    }
121
122    public AppiumDriver(Capabilities capabilities) {
123        this(AppiumDriverLocalService.buildDefaultService(), capabilities);
124    }
125
126    /**
127     * Changes platform name if it is not set and returns merged capabilities.
128     *
129     * @param originalCapabilities the given {@link Capabilities}.
130     * @param defaultName          a {@link MobileCapabilityType#PLATFORM_NAME} value which has
131     *                             to be set up
132     * @return {@link Capabilities} with changed platform name value or the original capabilities
133     */
134    protected static Capabilities ensurePlatformName(
135            Capabilities originalCapabilities, String defaultName) {
136        String currentName = (String) originalCapabilities.getCapability(PLATFORM_NAME);
137        return isBlank(currentName)
138                ? originalCapabilities.merge(new ImmutableCapabilities(PLATFORM_NAME, defaultName))
139                : originalCapabilities;
140    }
141
142    /**
143     * Changes automation name if it is not set and returns merged capabilities.
144     *
145     * @param originalCapabilities the given {@link Capabilities}.
146     * @param defaultName          a {@link MobileCapabilityType#AUTOMATION_NAME} value which has
147     *                             to be set up
148     * @return {@link Capabilities} with changed mobile automation name value or the original capabilities
149     */
150    protected static Capabilities ensureAutomationName(
151            Capabilities originalCapabilities, String defaultName) {
152        String currentAutomationName = CapabilityHelpers.getCapability(
153                originalCapabilities, AUTOMATION_NAME, String.class);
154        if (isBlank(currentAutomationName)) {
155            String capabilityName = originalCapabilities.getCapabilityNames()
156                    .contains(AUTOMATION_NAME) ? AUTOMATION_NAME : APPIUM_PREFIX + AUTOMATION_NAME;
157            return originalCapabilities.merge(new ImmutableCapabilities(capabilityName, defaultName));
158        }
159        return originalCapabilities;
160    }
161
162    /**
163     * Changes platform and automation names if they are not set
164     * and returns merged capabilities.
165     *
166     * @param originalCapabilities the given {@link Capabilities}.
167     * @param defaultPlatformName  a {@link MobileCapabilityType#PLATFORM_NAME} value which has
168     *                             to be set up
169     * @param defaultAutomationName The default automation name to set up for this class
170     * @return {@link Capabilities} with changed platform/automation name value or the original capabilities
171     */
172    protected static Capabilities ensurePlatformAndAutomationNames(
173            Capabilities originalCapabilities, String defaultPlatformName, String defaultAutomationName) {
174        Capabilities capsWithPlatformFixed = ensurePlatformName(originalCapabilities, defaultPlatformName);
175        return ensureAutomationName(capsWithPlatformFixed, defaultAutomationName);
176    }
177
178    @Override
179    public ExecuteMethod getExecuteMethod() {
180        return executeMethod;
181    }
182
183    /**
184     * This method is used to get build version status of running Appium server.
185     *
186     * @return map containing version details
187     */
188    public Map<String, Object> getStatus() {
189        //noinspection unchecked
190        return (Map<String, Object>) execute(DriverCommand.STATUS).getValue();
191    }
192
193    /**
194     * This method is used to add custom appium commands in Appium 2.0.
195     *
196     * @param httpMethod the available {@link HttpMethod}.
197     * @param url        The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
198     * @param methodName The name of custom appium command.
199     */
200    public void addCommand(HttpMethod httpMethod, String url, String methodName) {
201        switch (httpMethod) {
202            case GET:
203                MobileCommand.commandRepository.put(methodName, MobileCommand.getC(url));
204                break;
205            case POST:
206                MobileCommand.commandRepository.put(methodName, MobileCommand.postC(url));
207                break;
208            case DELETE:
209                MobileCommand.commandRepository.put(methodName, MobileCommand.deleteC(url));
210                break;
211            default:
212                throw new WebDriverException(String.format("Unsupported HTTP Method: %s. Only %s methods are supported",
213                        httpMethod,
214                        Arrays.toString(HttpMethod.values())));
215        }
216        ((AppiumCommandExecutor) getCommandExecutor()).refreshAdditionalCommands();
217    }
218
219    public URL getRemoteAddress() {
220        return remoteAddress;
221    }
222
223    @Override
224    protected void startSession(Capabilities capabilities) {
225        Response response = execute(new AppiumNewSessionCommandPayload(capabilities));
226        if (response == null) {
227            throw new SessionNotCreatedException(
228                    "The underlying command executor returned a null response.");
229        }
230
231        Object responseValue = response.getValue();
232        if (responseValue == null) {
233            throw new SessionNotCreatedException(
234                    "The underlying command executor returned a response without payload: "
235                            + response);
236        }
237        if (!(responseValue instanceof Map)) {
238            throw new SessionNotCreatedException(
239                    "The underlying command executor returned a response with a non well formed payload: "
240                            + response);
241        }
242
243        @SuppressWarnings("unchecked") Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue;
244        // A workaround for Selenium API enforcing some legacy capability values
245        rawCapabilities.remove(CapabilityType.PLATFORM);
246        if (rawCapabilities.containsKey(CapabilityType.BROWSER_NAME)
247                && isBlank((String) rawCapabilities.get(CapabilityType.BROWSER_NAME))) {
248            rawCapabilities.remove(CapabilityType.BROWSER_NAME);
249        }
250        MutableCapabilities returnedCapabilities = new BaseOptions<>(rawCapabilities);
251        try {
252            Field capsField = RemoteWebDriver.class.getDeclaredField("capabilities");
253            capsField.setAccessible(true);
254            capsField.set(this, returnedCapabilities);
255        } catch (NoSuchFieldException | IllegalAccessException e) {
256            throw new WebDriverException(e);
257        }
258        setSessionId(response.getSessionId());
259    }
260
261    @Override
262    public Response execute(String driverCommand, Map<String, ?> parameters) {
263        return super.execute(driverCommand, parameters);
264    }
265
266    @Override
267    public Response execute(String command) {
268        return super.execute(command, Collections.emptyMap());
269    }
270}
271
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

Trigger deleteC code on LambdaTest Cloud Grid

Execute automation tests with deleteC on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)