How to use alias method of io.appium.java_client.remote.AppiumW3CHttpCommandCodec class

Best io.appium code snippet using io.appium.java_client.remote.AppiumW3CHttpCommandCodec.alias

Run io.appium automation tests on LambdaTest cloud grid

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

AppiumW3CHttpCommandCodec.java

Source: AppiumW3CHttpCommandCodec.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 static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ATTRIBUTE;
20import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION;
21import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW;
22import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_SIZE;
23import static org.openqa.selenium.remote.DriverCommand.GET_PAGE_SOURCE;
24import static org.openqa.selenium.remote.DriverCommand.IS_ELEMENT_DISPLAYED;
25import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT;
26import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ELEMENT;
27import static org.openqa.selenium.remote.DriverCommand.SET_TIMEOUT;
28import static org.openqa.selenium.remote.DriverCommand.SUBMIT_ELEMENT;
29
30import com.google.common.collect.ImmutableList;
31import com.google.common.collect.ImmutableMap;
32
33import org.openqa.selenium.interactions.KeyInput;
34import org.openqa.selenium.interactions.Sequence;
35import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
36
37import java.util.Collection;
38import java.util.Map;
39import java.util.stream.Collectors;
40import java.util.stream.Stream;
41
42public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec {
43    /**
44     * This class overrides the built-in Selenium W3C commands codec,
45     * since the latter hardcodes many commands in Javascript,
46     * which does not work with Appium.
47     * Check https://www.w3.org/TR/webdriver/ to see all available W3C
48     * endpoints.
49     */
50    public AppiumW3CHttpCommandCodec() {
51        defineCommand(GET_ELEMENT_ATTRIBUTE, get("/session/:sessionId/element/:id/attribute/:name"));
52        defineCommand(IS_ELEMENT_DISPLAYED, get("/session/:sessionId/element/:id/displayed"));
53        defineCommand(GET_PAGE_SOURCE, get("/session/:sessionId/source"));
54        defineCommand(SEND_KEYS_TO_ACTIVE_ELEMENT, post("/session/:sessionId/actions"));
55    }
56
57    @Override
58    public void alias(String commandName, String isAnAliasFor) {
59        // This blocks parent constructor from undesirable aliases assigning
60        switch (commandName) {
61            case GET_ELEMENT_ATTRIBUTE:
62            case GET_ELEMENT_LOCATION:
63            case GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW:
64            case GET_ELEMENT_SIZE:
65            case IS_ELEMENT_DISPLAYED:
66            case SUBMIT_ELEMENT:
67            case GET_PAGE_SOURCE:
68                return;
69            default:
70                super.alias(commandName, isAnAliasFor);
71                break;
72        }
73    }
74
75    @Override
76    protected Map<String, ?> amendParameters(String name, Map<String, ?> parameters) {
77        // This blocks parent constructor from undesirable parameters amending
78        switch (name) {
79            case SEND_KEYS_TO_ACTIVE_ELEMENT:
80                Object rawValue = parameters.get("value");
81                //noinspection unchecked
82                Stream<CharSequence> source = (rawValue instanceof Collection)
83                        ? ((Collection<CharSequence>) rawValue).stream()
84                        : Stream.of((CharSequence[]) rawValue);
85                String text = source
86                        .flatMap(Stream::of)
87                        .collect(Collectors.joining());
88
89                final KeyInput keyboard = new KeyInput("keyboard");
90                Sequence sequence = new Sequence(keyboard, 0);
91                for (int i = 0; i < text.length(); ++i) {
92                    sequence.addAction(keyboard.createKeyDown(text.charAt(i)))
93                            .addAction(keyboard.createKeyUp(text.charAt(i)));
94                }
95                return ImmutableMap.<String, Object>builder()
96                        .put("actions", ImmutableList.of(sequence.toJson()))
97                        .build();
98            case SEND_KEYS_TO_ELEMENT:
99            case SET_TIMEOUT:
100                return super.amendParameters(name, parameters);
101            default:
102                return parameters;
103        }
104    }
105}
106
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 used method in AppiumW3CHttpCommandCodec

Trigger alias code on LambdaTest Cloud Grid

Execute automation tests with alias 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)