Best io.appium code snippet using io.appium.java_client.events.api.mobile.ContextEventListener
DefaultListener.java
Source:DefaultListener.java
...22import io.appium.java_client.events.api.general.ListensToException;23import io.appium.java_client.events.api.general.NavigationEventListener;24import io.appium.java_client.events.api.general.SearchingEventListener;25import io.appium.java_client.events.api.general.WindowEventListener;26import io.appium.java_client.events.api.mobile.ContextEventListener;27import io.appium.java_client.events.api.mobile.RotationEventListener;28import org.openqa.selenium.Alert;29import org.openqa.selenium.By;30import org.openqa.selenium.Dimension;31import org.openqa.selenium.Point;32import org.openqa.selenium.ScreenOrientation;33import org.openqa.selenium.WebDriver;34import org.openqa.selenium.WebElement;35import org.openqa.selenium.security.Credentials;36import org.openqa.selenium.support.events.WebDriverEventListener;37import java.lang.reflect.Proxy;38import java.util.ArrayList;39import java.util.Collection;40import java.util.List;41class DefaultListener42 implements Listener, AppiumWebDriverEventListener, ListensToException, SearchingEventListener,43 NavigationEventListener, JavaScriptEventListener, ElementEventListener, AlertEventListener,44 WindowEventListener, ContextEventListener, RotationEventListener {45 private final List<Listener> listeners = new ArrayList<>();46 private Object dispatcher = Proxy.newProxyInstance(Listener.class.getClassLoader(),47 new Class[] {AlertEventListener.class, ContextEventListener.class,48 ElementEventListener.class, JavaScriptEventListener.class, ListensToException.class,49 NavigationEventListener.class, RotationEventListener.class,50 SearchingEventListener.class, WindowEventListener.class, WebDriverEventListener.class},51 new ListenerInvocationHandler(listeners));52 @Override public void beforeNavigateTo(String url, WebDriver driver) {53 ((NavigationEventListener) dispatcher).beforeNavigateTo(url, driver);54 }55 @Override public void afterNavigateTo(String url, WebDriver driver) {56 ((NavigationEventListener) dispatcher).afterNavigateTo(url, driver);57 }58 @Override public void beforeNavigateBack(WebDriver driver) {59 ((NavigationEventListener) dispatcher).beforeNavigateBack(driver);60 }61 @Override public void afterNavigateBack(WebDriver driver) {62 ((NavigationEventListener) dispatcher).afterNavigateBack(driver);63 }64 @Override public void beforeNavigateForward(WebDriver driver) {65 ((NavigationEventListener) dispatcher).beforeNavigateForward(driver);66 }67 @Override public void afterNavigateForward(WebDriver driver) {68 ((NavigationEventListener) dispatcher).afterNavigateForward(driver);69 }70 @Override public void beforeNavigateRefresh(WebDriver driver) {71 ((NavigationEventListener) dispatcher).beforeNavigateRefresh(driver);72 }73 @Override public void afterNavigateRefresh(WebDriver driver) {74 ((NavigationEventListener) dispatcher).afterNavigateRefresh(driver);75 }76 @Override public void beforeFindBy(By by, WebElement element, WebDriver driver) {77 ((SearchingEventListener) dispatcher).beforeFindBy(by, element, driver);78 }79 @Override public void afterFindBy(By by, WebElement element, WebDriver driver) {80 ((SearchingEventListener) dispatcher).afterFindBy(by, element, driver);81 }82 @Override public void beforeClickOn(WebElement element, WebDriver driver) {83 ((ElementEventListener) dispatcher).beforeClickOn(element, driver);84 }85 @Override public void afterClickOn(WebElement element, WebDriver driver) {86 ((ElementEventListener) dispatcher).afterClickOn(element, driver);87 }88 @Override public void beforeChangeValueOf(WebElement element, WebDriver driver) {89 ((ElementEventListener) dispatcher).beforeChangeValueOf(element, driver);90 }91 @Override public void beforeChangeValueOf(WebElement element, WebDriver driver,92 CharSequence[] keysToSend) {93 ((ElementEventListener) dispatcher).beforeChangeValueOf(element, driver, keysToSend);94 }95 @Override public void afterChangeValueOf(WebElement element, WebDriver driver) {96 ((ElementEventListener) dispatcher).afterChangeValueOf(element, driver);97 }98 @Override public void afterChangeValueOf(WebElement element, WebDriver driver,99 CharSequence[] keysToSend) {100 ((ElementEventListener) dispatcher).afterChangeValueOf(element, driver, keysToSend);101 }102 @Override public void beforeScript(String script, WebDriver driver) {103 ((JavaScriptEventListener) dispatcher).beforeScript(script, driver);104 }105 @Override public void afterScript(String script, WebDriver driver) {106 ((JavaScriptEventListener) dispatcher).afterScript(script, driver);107 }108 @Override public void onException(Throwable throwable, WebDriver driver) {109 ((ListensToException) dispatcher).onException(throwable, driver);110 }111 public void add(Collection<Listener> listeners) {112 this.listeners.addAll(listeners);113 }114 @Override public void beforeAlertAccept(WebDriver driver, Alert alert) {115 ((AlertEventListener) dispatcher).beforeAlertAccept(driver, alert);116 }117 @Override118 public void beforeAlertAccept(WebDriver driver) {119 ((WebDriverEventListener) dispatcher).beforeAlertAccept(driver);120 }121 @Override public void afterAlertAccept(WebDriver driver, Alert alert) {122 ((AlertEventListener) dispatcher).afterAlertAccept(driver, alert);123 }124 @Override125 public void afterAlertAccept(WebDriver driver) {126 ((WebDriverEventListener) dispatcher).afterAlertAccept(driver);127 }128 @Override public void afterAlertDismiss(WebDriver driver, Alert alert) {129 ((AlertEventListener) dispatcher).afterAlertDismiss(driver, alert);130 }131 @Override132 public void afterAlertDismiss(WebDriver driver) {133 ((WebDriverEventListener) dispatcher).afterAlertDismiss(driver);134 }135 @Override public void beforeAlertDismiss(WebDriver driver, Alert alert) {136 ((AlertEventListener) dispatcher).beforeAlertDismiss(driver, alert);137 }138 @Override139 public void beforeAlertDismiss(WebDriver driver) {140 ((WebDriverEventListener) dispatcher).beforeAlertDismiss(driver);141 }142 @Override public void beforeAlertSendKeys(WebDriver driver, Alert alert, String keys) {143 ((AlertEventListener) dispatcher).beforeAlertSendKeys(driver, alert, keys);144 }145 @Override public void afterAlertSendKeys(WebDriver driver, Alert alert, String keys) {146 ((AlertEventListener) dispatcher).afterAlertSendKeys(driver, alert, keys);147 }148 @Override149 public void beforeAuthentication(WebDriver driver, Alert alert, Credentials credentials) {150 ((AlertEventListener) dispatcher).beforeAuthentication(driver, alert, credentials);151 }152 @Override153 public void afterAuthentication(WebDriver driver, Alert alert, Credentials credentials) {154 ((AlertEventListener) dispatcher).afterAuthentication(driver, alert, credentials);155 }156 @Override public void beforeWindowChangeSize(WebDriver driver, WebDriver.Window window,157 Dimension targetSize) {158 ((WindowEventListener) dispatcher).beforeWindowChangeSize(driver, window, targetSize);159 }160 @Override public void afterWindowChangeSize(WebDriver driver, WebDriver.Window window,161 Dimension targetSize) {162 ((WindowEventListener) dispatcher).afterWindowChangeSize(driver, window, targetSize);163 }164 @Override165 public void beforeWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) {166 ((WindowEventListener) dispatcher).beforeWindowIsMoved(driver, window, targetPoint);167 }168 @Override169 public void afterWindowIsMoved(WebDriver driver, WebDriver.Window window, Point targetPoint) {170 ((WindowEventListener) dispatcher).afterWindowIsMoved(driver, window, targetPoint);171 }172 @Override public void beforeWindowIsMaximized(WebDriver driver, WebDriver.Window window) {173 ((WindowEventListener) dispatcher).beforeWindowIsMaximized(driver, window);174 }175 @Override public void afterWindowIsMaximized(WebDriver driver, WebDriver.Window window) {176 ((WindowEventListener) dispatcher).afterWindowIsMaximized(driver, window);177 }178 @Override public void beforeSwitchingToContext(WebDriver driver, String context) {179 ((ContextEventListener) dispatcher).beforeSwitchingToContext(driver, context);180 }181 @Override public void afterSwitchingToContext(WebDriver driver, String context) {182 ((ContextEventListener) dispatcher).afterSwitchingToContext(driver, context);183 }184 @Override public void beforeRotation(WebDriver driver, ScreenOrientation orientation) {185 ((RotationEventListener) dispatcher).beforeRotation(driver, orientation);186 }187 @Override public void afterRotation(WebDriver driver, ScreenOrientation orientation) {188 ((RotationEventListener) dispatcher).afterRotation(driver, orientation);189 }190}...
WechatBase.java
Source:WechatBase.java
2import com.aventstack.extentreports.testng.listener.ExtentITestListenerClassAdapter;3import com.tuners.mobile.BasePages.WechatNativePage;4import com.tuners.mobile.util.Functions.FunctionUtil;5import com.tuners.mobile.util.PageObject;6import com.tuners.mobile.util.listeners.ContextEventListener;7import com.tuners.mobile.util.listeners.ElementListener;8import com.tuners.mobile.util.listeners.LogAppiumWebDriverEventListener;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.events.EventFiringWebDriverFactory;11import io.appium.java_client.events.api.Listener;12import io.appium.java_client.remote.AndroidMobileCapabilityType;13import io.appium.java_client.remote.MobileCapabilityType;14import org.apache.log4j.Logger;15import org.apache.log4j.PropertyConfigurator;16import org.openqa.selenium.WebElement;17import org.openqa.selenium.chrome.ChromeOptions;18import org.openqa.selenium.remote.DesiredCapabilities;19import org.openqa.selenium.remote.LocalFileDetector;20import org.testng.annotations.*;21import java.io.IOException;22import java.net.URL;23import java.util.ArrayList;24import java.util.List;25import java.util.Properties;26import java.util.concurrent.TimeUnit;27/**28 * @Author Shaine29 * @Description30 * @Date 2020/6/16 17:4831 **/32@Listeners(ExtentITestListenerClassAdapter.class)33public class WechatBase {34 public static AndroidDriver androidDriver;35 static Logger logger = Logger.getLogger(WechatBase.class);36 public int ENVFLAG=0;37 @Parameters({"env"})38 @BeforeTest39 public void config(@Optional("0") String env) throws Exception {40 //ä¸appium建ç«è¿æ¥41 configAppium();42 //æ·»å ã注åçå¬43 addRegisterListeners();44 // åå§åæ¥å¿45 initLog();46 //æ件æ¢æµ47 androidDriver.setFileDetector(new LocalFileDetector());48 //设置å
¨å±çå¾
49 androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);50 ENVFLAG=Integer.parseInt(env);51 logger.info("ENVFLAG is:" + ENVFLAG);52 }53public void configAppium() throws IOException {54 FunctionUtil functionUtil=new FunctionUtil(androidDriver);55Properties prop=functionUtil.loadProperties("android");56 //1. å建é
置对象57 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();58 //2.æ·»å é
ç½®59 desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, prop.getProperty(MobileCapabilityType.PLATFORM_NAME));60 desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,prop.getProperty(MobileCapabilityType.AUTOMATION_NAME));61 desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, prop.getProperty(MobileCapabilityType.PLATFORM_VERSION));62 desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, prop.getProperty(MobileCapabilityType.DEVICE_NAME));63 desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);64 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,prop.getProperty(AndroidMobileCapabilityType.APP_PACKAGE));65 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, prop.getProperty(AndroidMobileCapabilityType.APP_ACTIVITY));66 desiredCapabilities.setCapability(AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE, prop.getProperty(AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE));67 desiredCapabilities. setCapability("setWebContentsDebuggingEnabled",true);68 ChromeOptions options=new ChromeOptions();69 options.setExperimentalOption("androidProcess",prop.getProperty("androidProcess"));70 options.setCapability("browserName","");71 desiredCapabilities.setCapability(ChromeOptions.CAPABILITY,options);72 //åå»ºé©±å¨ ä¼ å
¥ä¸¤ä¸ªåæ° 1.appiumé讯å°å 2.desiredCapabilities73 androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);74}75public void addRegisterListeners(){76 //æ·»å çå¬77 List<Listener> listeners = new ArrayList<>();78 listeners.add(new LogAppiumWebDriverEventListener());79 listeners.add(new ContextEventListener());80 listeners.add(new ElementListener());81 //注åçå¬82 androidDriver = EventFiringWebDriverFactory.getEventFiringWebDriver(androidDriver,listeners);83}84 public void initLog(){85 PropertyConfigurator.configure("src/test/Resources/log4j.properties");86 }87@AfterMethod88public void afterEachTestMethod() throws Exception {89 WechatNativePage wechatHomePage=new WechatNativePage(androidDriver);90 FunctionUtil functionUtil=new FunctionUtil(androidDriver);91 functionUtil.switchToNative();92 wechatHomePage.iconOfReturnAndClose.click();93 wechatHomePage.iconOfReturn.click();...
ContextListener2.java
Source:ContextListener2.java
1package io.appium.java_client.events.listeners;2import io.appium.java_client.events.api.mobile.ContextEventListener;3import org.openqa.selenium.WebDriver;4public class ContextListener2 extends TestListener implements ContextEventListener {5 @Override public void beforeSwitchingToContext(WebDriver driver, String context) {6 messages.add("Externally defined listener: Attempt to change current context to " + context);7 }8 @Override public void afterSwitchingToContext(WebDriver driver, String context) {9 messages.add("Externally defined listener: The previous context has been changed to " + context);10 }11 @Override protected void add() {12 SingleListeners.listeners.put(ContextListener2.class, this);13 }14}...
ContextListener.java
Source:ContextListener.java
1package io.appium.java_client.events.listeners;2import io.appium.java_client.events.api.mobile.ContextEventListener;3import org.openqa.selenium.WebDriver;4public class ContextListener extends TestListener implements ContextEventListener {5 @Override public void beforeSwitchingToContext(WebDriver driver, String context) {6 messages.add("Attempt to change current context to " + context);7 }8 @Override public void afterSwitchingToContext(WebDriver driver, String context) {9 messages.add("The previous context has been changed to " + context);10 }11 @Override protected void add() {12 SingleListeners.listeners.put(ContextListener.class, this);13 }14}...
ContextEventListener.java
Source:ContextEventListener.java
...4 * @Author Mason5 * @Description6 * @Date 2020/7/9 10:077 **/8public class ContextEventListener implements io.appium.java_client.events.api.mobile.ContextEventListener {9 @Override10 public void beforeSwitchingToContext(WebDriver webDriver, String s) {11 }12 @Override13 public void afterSwitchingToContext(WebDriver webDriver, String s) {14 }15}...
ContextEventListener
Using AI Code Generation
1package appium.java;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.remote.DesiredCapabilities;6import io.appium.java_client.AppiumDriver;7import io.appium.java_client.MobileElement;8import io.appium.java_client.android.AndroidDriver;9import io.appium.java_client.events.api.mobile.ContextEventListener;10import io.appium.java_client.remote.MobileCapabilityType;11public class AppiumContextEventListener {12 public static AppiumDriver<MobileElement> driver;13 public static void main(String[] args) throws MalformedURLException, InterruptedException {14 DesiredCapabilities cap = new DesiredCapabilities();15 cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");16 cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");17 cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10.0");18 cap.setCapability(MobileCapabilityType.UDID, "emulator-5554");19 cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");20 cap.setCapability("appPackage", "com.androidsample.generalstore");21 cap.setCapability("appActivity", ".SplashActivity");
ContextEventListener
Using AI Code Generation
1package appium.java;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.testng.annotations.AfterTest;8import org.testng.annotations.BeforeTest;9import org.testng.annotations.Test;10import io.appium.java_client.AppiumDriver;11import io.appium.java_client.MobileElement;12import io.appium.java_client.android.AndroidDriver;13import io.appium.java_client.events.api.mobile.ContextEventListener;14import io.appium.java_client.remote.MobileCapabilityType;15public class ContextChange {16 AppiumDriver<MobileElement> driver;17 public void setup() throws MalformedURLException {18 DesiredCapabilities caps = new DesiredCapabilities();19 caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");20 caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");21 caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.0");22 caps.setCapability(MobileCapabilityType.UDID, "emulator-5554");23 caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);24 caps.setCapability("appPackage", "com.androidsample.generalstore");25 caps.setCapability("appActivity", ".SplashActivity");26 caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
ContextEventListener
Using AI Code Generation
1package com.appium.test;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.testng.annotations.AfterTest;7import org.testng.annotations.BeforeTest;8import org.testng.annotations.Test;9import io.appium.java_client.AppiumDriver;10import io.appium.java_client.MobileElement;11import io.appium.java_client.android.AndroidDriver;12import io.appium.java_client.events.api.mobile.ContextEventListener;13import io.appium.java_client.events.api.mobile.ContextListener;14import io.appium.java_client.events.api.mobile.ContextSwitchListener;15public class ContextSwitching {16 AppiumDriver<MobileElement> driver;17 ContextListener contextListener;18 ContextSwitchListener contextSwitchListener;19 ContextEventListener contextEventListener;20 DesiredCapabilities dc = new DesiredCapabilities();21 public void setup() throws MalformedURLException {22 dc.setCapability("deviceName", "Pixel 3a API 28");23 dc.setCapability("udid", "emulator-5554");24 dc.setCapability("platformName", "Android");25 dc.setCapability("platformVersion", "9");26 dc.setCapability("appPackage", "com.androidsample.generalstore");27 dc.setCapability("appActivity", ".SplashActivity");28 dc.setCapability("noReset", "true");
ContextEventListener
Using AI Code Generation
1package appium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.remote.DesiredCapabilities;5import java.net.URL;6import io.appium.java_client.AppiumDriver;7import io.appium.java_client.android.AndroidDriver;8import io.appium.java_client.events.api.mobile.ContextEvent
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!