How to use CustomEvent class of io.appium.java_client.serverevents package

Best io.appium code snippet using io.appium.java_client.serverevents.CustomEvent

Run io.appium automation tests on LambdaTest cloud grid

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

iOSInAppAuthenticationTest.java

Source: iOSInAppAuthenticationTest.java Github

copy
1package com.appium.mobile;
2
3import com.google.common.collect.ImmutableMap;
4import io.appium.java_client.MobileBy;
5import io.appium.java_client.ios.IOSDriver;
6import io.appium.java_client.remote.AutomationName;
7import io.appium.java_client.remote.IOSMobileCapabilityType;
8import io.appium.java_client.remote.MobileCapabilityType;
9import io.appium.java_client.serverevents.CustomEvent;
10import io.appium.java_client.serverevents.ServerEvents;
11import lombok.SneakyThrows;
12import org.openqa.selenium.By;
13import org.openqa.selenium.remote.DesiredCapabilities;
14import org.openqa.selenium.support.ui.ExpectedConditions;
15import org.openqa.selenium.support.ui.WebDriverWait;
16import org.testng.annotations.AfterClass;
17import org.testng.annotations.BeforeClass;
18import org.testng.annotations.Test;
19
20import java.io.File;
21import java.io.IOException;
22import java.net.URL;
23
24public class iOSInAppAuthenticationTest {
25
26    private IOSDriver driver;
27    private WebDriverWait wait;
28
29    private By loginButton = MobileBy.AccessibilityId("Log In");
30    private By logoutButton = MobileBy.AccessibilityId("Log Out");
31
32    @BeforeClass
33    public void setUp() throws IOException {
34        DesiredCapabilities capabilities = new DesiredCapabilities();
35        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "13.3");
36        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 11 Pro Max");
37        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.IOS_XCUI_TEST);
38        capabilities.setCapability(IOSMobileCapabilityType.LAUNCH_TIMEOUT, 700000);
39        capabilities.setCapability(IOSMobileCapabilityType.USE_PREBUILT_WDA, true);
40        capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir") + "/apps/BiometricLogin.app");
41        driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
42        wait  = new WebDriverWait(driver, 20);
43    }
44
45    @Test
46    public void inAppAuthenticationTest() {
47        CustomEvent logEvent = new CustomEvent();
48
49        driver.executeScript("mobile:enrollBiometric", ImmutableMap.of("isEnabled", true));
50        logEvent.setVendor("VodQA");
51        logEvent.setEventName("onLoginScreen");
52        driver.logEvent(logEvent);
53        wait.until(ExpectedConditions.presenceOfElementLocated(loginButton)).click();
54        logEvent.setEventName("loggedIn");
55        driver.switchTo().alert().accept();
56        driver.logEvent(logEvent);
57
58        driver.executeScript("mobile:sendBiometricMatch", ImmutableMap.of("type", "faceId", "match", true));
59
60        wait.until(ExpectedConditions.presenceOfElementLocated(logoutButton)).click();
61        wait.until(ExpectedConditions.presenceOfElementLocated(loginButton)).click();
62
63        driver.executeScript("mobile:sendBiometricMatch", ImmutableMap.of("type", "faceId", "match", false));
64
65        wait.until(ExpectedConditions.alertIsPresent());
66    }
67
68    @SneakyThrows
69    @AfterClass
70    public void afterClass() {
71        if (driver != null) {
72            CustomEvent customEvent = new CustomEvent();
73            customEvent.setVendor("VodQA");
74            customEvent.setEventName("event ends here");
75            driver.logEvent(customEvent);
76            ServerEvents events = driver.getEvents();
77            events.save(new File(System.getProperty("user.dir") + "/eventFlow.json").toPath());
78            driver.quit();
79        }
80    }
81}
82
Full Screen

LogEventTest.java

Source: LogEventTest.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.android;
18
19import static org.junit.Assert.assertThat;
20import static org.junit.Assert.assertTrue;
21
22import io.appium.java_client.serverevents.CommandEvent;
23import io.appium.java_client.serverevents.CustomEvent;
24import io.appium.java_client.serverevents.TimedEvent;
25import io.appium.java_client.serverevents.ServerEvents;
26import org.hamcrest.Matchers;
27import org.junit.Test;
28
29public class LogEventTest extends BaseAndroidTest {
30
31    @Test
32    public void verifyLoggingCustomEvents() {
33        CustomEvent evt = new CustomEvent();
34        evt.setEventName("funEvent");
35        evt.setVendor("appium");
36        driver.logEvent(evt);
37        ServerEvents events = driver.getEvents();
38        boolean hasCustomEvent = events.events.stream().anyMatch((TimedEvent event) ->
39            event.name.equals("appium:funEvent") &&
40            event.occurrences.get(0).intValue() > 0
41        );
42        boolean hasCommandName = events.commands.stream().anyMatch((CommandEvent event) ->
43            event.name.equals("logCustomEvent")
44        );
45        assertTrue(hasCustomEvent);
46        assertTrue(hasCommandName);
47        assertThat(events.jsonData, Matchers.containsString("\"appium:funEvent\""));
48    }
49}
50
Full Screen

LogsEvents.java

Source: LogsEvents.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.MobileCommand.GET_EVENTS;
20import static io.appium.java_client.MobileCommand.LOG_EVENT;
21
22import com.google.common.collect.ImmutableMap;
23import io.appium.java_client.serverevents.CommandEvent;
24import io.appium.java_client.serverevents.CustomEvent;
25import io.appium.java_client.serverevents.TimedEvent;
26import io.appium.java_client.serverevents.ServerEvents;
27import java.util.List;
28import java.util.Map;
29import java.util.stream.Collectors;
30import org.openqa.selenium.json.Json;
31import org.openqa.selenium.remote.Response;
32
33public interface LogsEvents extends ExecutesMethod {
34
35    /**
36     * Log a custom event on the Appium server.
37     *
38     * @since Appium 1.16
39     * @param event the event to log
40     * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script
41     */
42    default void logEvent(CustomEvent event) {
43        execute(LOG_EVENT, ImmutableMap.of("vendor", event.getVendor(), "event", event.getEventName()));
44    }
45
46    /**
47     * Log a custom event on the Appium server.
48     *
49     * @since Appium 1.16
50     * @return ServerEvents object wrapping up the various command and event timestamps
51     * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script
52     */
53    default ServerEvents getEvents() {
54        Response response = execute(GET_EVENTS);
55        String jsonData = new Json().toJson(response.getValue());
56
57        //noinspection unchecked
58        Map<String, Object> value = (Map<String, Object>) response.getValue();
59
60        //noinspection unchecked
61        List<CommandEvent> commands = ((List<Map<String, Object>>) value.get("commands"))
62            .stream()
63            .map((Map<String, Object> cmd) -> new CommandEvent(
64                (String) cmd.get("cmd"),
65                ((Long) cmd.get("startTime")),
66                ((Long) cmd.get("endTime"))
67            ))
68            .collect(Collectors.toList());
69
70        List<TimedEvent> events = value.keySet().stream()
71            .filter((String name) -> !name.equals("commands"))
72            .map((String name) -> {
73                //noinspection unchecked
74                return new TimedEvent(name, (List<Long>) value.get(name));
75            })
76            .collect(Collectors.toList());
77
78        return new ServerEvents(commands, events, jsonData);
79    }
80}
81
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 methods in CustomEvent

    No methods found for this class 😞

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)