Best Selenium code snippet using org.openqa.selenium.support.events.Interface WebDriverEventListener
Source:WebEventListener.java
1package com.dataseers.qa.util;2import java.io.IOException;3/*************************************** PURPOSE **********************************4- This class implements the WebDriverEventListener, which is included under events.5The purpose of implementing this interface is to override all the methods and define certain useful Log statements 6which would be displayed/logged as the application under test is being run.7Do not call any of these methods, instead these methods will be invoked automatically8as an when the action done (click, findBy etc). 9*/10import org.openqa.selenium.By;11import org.openqa.selenium.OutputType;12import org.openqa.selenium.StaleElementReferenceException;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.WebElement;15import org.openqa.selenium.support.events.WebDriverEventListener;16import org.openqa.selenium.support.ui.ExpectedConditions;17import org.openqa.selenium.support.ui.WebDriverWait;18import org.testng.ITestNGListener;19import com.dataseers.qa.base.TestBase;20/**21 * The listener interface for receiving webEvent events.22 * The class that is interested in processing a webEvent23 * event implements this interface, and the object created24 * with that class is registered with a component using the25 * component's <code>addWebEventListener<code> method. When26 * the webEvent event occurs, that object's appropriate27 * method is invoked.28 *29 * @see WebEventEvent30 */31public class WebEventListener extends TestBase implements WebDriverEventListener,ITestNGListener32 {33 /* (non-Javadoc)34 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateTo(java.lang.String, org.openqa.selenium.WebDriver)35 */36 37 public void beforeNavigateTo(String url, WebDriver d) {38 log.info("Before navigating to: '" + url + "'");39 log.info("Before navigating to: '" + url + "'");40 }41 /* (non-Javadoc)42 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateTo(java.lang.String, org.openqa.selenium.WebDriver)43 */44 45 public void afterNavigateTo(String url, WebDriver d) {46 log.info("Navigated to:'" + url + "'");47 log.info("Navigated to:'" + url + "'");48 }49 /**50 * Before change value of.51 *52 * @param element the element53 * @param driver the driver54 */55 public void beforeChangeValueOf(WebElement element) {56 log.info("Value of the:" + element.toString() + " before any changes made");57 log.info("Value of the:" + element.toString() + " before any changes made");58 }59 /**60 * After change value of.61 *62 * @param element the element63 * @param driver the driver64 */65 public void afterChangeValueOf(WebElement element) {66 log.info("Element value changed to: " + element.toString());67 log.info("Element value changed to: " + element.toString());68 }69 /* (non-Javadoc)70 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeClickOn(org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)71 */72 73 public void beforeClickOn(WebElement element, WebDriver d) {74 log.info("Trying to click on: " + element.toString());75 log.info("Trying to click on: " + element.toString());76 }77 78 public static void clickOn(WebDriver d, WebElement locator, int timeout) {79 new WebDriverWait(d, timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(locator));80 locator.click();81 }82 83 /* (non-Javadoc)84 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterClickOn(org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)85 */86 87 public void afterClickOn(WebElement element, WebDriver d) {88 log.info("Clicked on: " + element.toString());89 log.info("Clicked on: " + element.toString());90 }91 /* (non-Javadoc)92 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateBack(org.openqa.selenium.WebDriver)93 */94 95 public void beforeNavigateBack(WebDriver d) {96 log.info("Navigating back to previous page");97 log.info("Navigating back to previous page");98 }99 /* (non-Javadoc)100 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateBack(org.openqa.selenium.WebDriver)101 */102 103 public void afterNavigateBack(WebDriver d) {104 log.info("Navigated back to previous page");105 log.info("Navigated back to previous page");106 }107 /* (non-Javadoc)108 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateForward(org.openqa.selenium.WebDriver)109 */110 111 public void beforeNavigateForward(WebDriver d) {112 log.info("Navigating forward to next page");113 log.info("Navigating forward to next page");114 }115 /* (non-Javadoc)116 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateForward(org.openqa.selenium.WebDriver)117 */118 119 public void afterNavigateForward(WebDriver d) {120 log.info("Navigated forward to next page");121 log.info("Navigated forward to next page");122 }123 /* (non-Javadoc)124 * @see org.openqa.selenium.support.events.WebDriverEventListener#onException(java.lang.Throwable, org.openqa.selenium.WebDriver)125 */126 127 public void onException(Throwable error, WebDriver d) {128 try {129 log.info("Taking Screenshot");130 TestUtil.takeScreenshotAtEndOfTest();131 } catch (IOException e) {132log.error(e);133}134 log.info("Exception occured: " + error);135 log.info("Exception occured: " + error);136 137 }138 /* (non-Javadoc)139 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeFindBy(org.openqa.selenium.By, org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)140 */141 142 public void beforeFindBy(By by, WebElement element, WebDriver d) {143 log.info("Trying to find Element By : " + by.toString());144 }145 /* (non-Javadoc)146 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterFindBy(org.openqa.selenium.By, org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)147 */148 149 public void afterFindBy(By by, WebElement element, WebDriver d) {150 log.info("Found Element By : " + by.toString());151 }152 public void beforeAlertAccept(WebDriver driver) {153 154 }155 public void afterAlertAccept(WebDriver driver) {156 157 }158 public void afterAlertDismiss(WebDriver driver) {159 160 }161 public void beforeAlertDismiss(WebDriver driver) {162 163 }164 public void beforeNavigateRefresh(WebDriver driver) {165 166 }167 public void afterNavigateRefresh(WebDriver driver) {168 169 }170 public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {171 172 }173 public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {174 175 }176 public void beforeScript(String script, WebDriver driver) {177 178 }179 public void afterScript(String script, WebDriver driver) {180 181 }182 public void beforeSwitchToWindow(String windowName, WebDriver driver) {183 184 }185 public void afterSwitchToWindow(String windowName, WebDriver driver) {186 187 }188 public <X> void beforeGetScreenshotAs(OutputType<X> target) {189 190 }191 public <X> void afterGetScreenshotAs(OutputType<X> target, X screenshot) {192 193 }194 public void beforeGetText(WebElement element, WebDriver driver) {195 196 }197 public void afterGetText(WebElement element, WebDriver driver, String text) {198 199 }200 public boolean retryingFindClick(By by) {201 boolean result = false;202 int attempts = 0;203 while(attempts < 4) {204 try {205 d.findElement(by).click();206 result = true;207 break;208 } catch(StaleElementReferenceException e) {209 }210 attempts++;211 }212 return result;213 }214 215 }...
Source:CustomWebDriverEventListener.java
1/*2 *3 * Copyright (C) 2007-2015 Licensed to the Comunes Association (CA) under4 * one or more contributor license agreements (see COPYRIGHT for details).5 * The CA licenses this file to you under the GNU Affero General Public6 * License version 3, (the "License"); you may not use this file except in7 * compliance with the License. This file is part of kune.8 *9 * This program is free software: you can redistribute it and/or modify10 * it under the terms of the GNU Affero General Public License as11 * published by the Free Software Foundation, either version 3 of the12 * License, or (at your option) any later version.13 *14 * This program is distributed in the hope that it will be useful,15 * but WITHOUT ANY WARRANTY; without even the implied warranty of16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17 * GNU Affero General Public License for more details.18 *19 * You should have received a copy of the GNU Affero General Public License20 * along with this program. If not, see <http://www.gnu.org/licenses/>.21 *22 */23package cc.kune.selenium;24import org.openqa.selenium.By;25import org.openqa.selenium.WebDriver;26import org.openqa.selenium.WebElement;27import org.openqa.selenium.chrome.ChromeDriver;28import org.openqa.selenium.support.events.WebDriverEventListener;29// TODO: Auto-generated Javadoc30/**31 * The listener interface for receiving customWebDriverEvent events. The class32 * that is interested in processing a customWebDriverEvent event implements this33 * interface, and the object created with that class is registered with a34 * component using the component's35 * <code>addCustomWebDriverEventListener<code> method. When36 * the customWebDriverEvent event occurs, that object's appropriate37 * method is invoked.38 * 39 * @see CustomWebDriverEventEvent40 */41public class CustomWebDriverEventListener implements WebDriverEventListener {42 /*43 * (non-Javadoc)44 * 45 * @see46 * org.openqa.selenium.support.events.WebDriverEventListener#afterChangeValueOf47 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)48 */49 @Override50 public void afterChangeValueOf(final WebElement element, final WebDriver driver) {51 // SeleniumUtils.showCursor(driver);52 sleep(400);53 }54 /*55 * (non-Javadoc)56 * 57 * @see58 * org.openqa.selenium.support.events.WebDriverEventListener#afterClickOn(59 * org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)60 */61 @Override62 public void afterClickOn(final WebElement element, final WebDriver driver) {63 // SeleniumUtils.showCursor(driver);64 slow();65 }66 /*67 * (non-Javadoc)68 * 69 * @see70 * org.openqa.selenium.support.events.WebDriverEventListener#afterFindBy(org71 * .openqa.selenium.By, org.openqa.selenium.WebElement,72 * org.openqa.selenium.WebDriver)73 */74 @Override75 public void afterFindBy(final By by, final WebElement element, final WebDriver driver) {76 }77 /*78 * (non-Javadoc)79 * 80 * @see81 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateBack82 * (org.openqa.selenium.WebDriver)83 */84 @Override85 public void afterNavigateBack(final WebDriver driver) {86 slow();87 }88 /*89 * (non-Javadoc)90 * 91 * @see92 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateForward93 * (org.openqa.selenium.WebDriver)94 */95 @Override96 public void afterNavigateForward(final WebDriver driver) {97 slow();98 }99 /*100 * (non-Javadoc)101 * 102 * @see103 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateTo104 * (java.lang.String, org.openqa.selenium.WebDriver)105 */106 @Override107 public void afterNavigateTo(final String url, final WebDriver driver) {108 sleep(500);109 }110 /*111 * (non-Javadoc)112 * 113 * @see114 * org.openqa.selenium.support.events.WebDriverEventListener#afterScript(java115 * .lang.String, org.openqa.selenium.WebDriver)116 */117 @Override118 public void afterScript(final String script, final WebDriver driver) {119 }120 /*121 * (non-Javadoc)122 * 123 * @see124 * org.openqa.selenium.support.events.WebDriverEventListener#beforeChangeValueOf125 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)126 */127 @Override128 public void beforeChangeValueOf(final WebElement element, final WebDriver driver) {129 SeleniumUtils.hightlight(element, driver);130 sleep(150);131 // SeleniumUtils.hideCursor(driver);132 }133 /*134 * (non-Javadoc)135 * 136 * @see137 * org.openqa.selenium.support.events.WebDriverEventListener#beforeClickOn138 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)139 */140 @Override141 public void beforeClickOn(final WebElement element, final WebDriver driver) {142 SeleniumUtils.hightlight(element, driver);143 slow();144 // SeleniumUtils.hideCursor(driver);145 }146 /*147 * (non-Javadoc)148 * 149 * @see150 * org.openqa.selenium.support.events.WebDriverEventListener#beforeFindBy(151 * org.openqa.selenium.By, org.openqa.selenium.WebElement,152 * org.openqa.selenium.WebDriver)153 */154 @Override155 public void beforeFindBy(final By by, final WebElement element, final WebDriver driver) {156 }157 /*158 * (non-Javadoc)159 * 160 * @see161 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateBack162 * (org.openqa.selenium.WebDriver)163 */164 @Override165 public void beforeNavigateBack(final WebDriver driver) {166 }167 /*168 * (non-Javadoc)169 * 170 * @see171 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateForward172 * (org.openqa.selenium.WebDriver)173 */174 @Override175 public void beforeNavigateForward(final WebDriver driver) {176 }177 /*178 * (non-Javadoc)179 * 180 * @see181 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateTo182 * (java.lang.String, org.openqa.selenium.WebDriver)183 */184 @Override185 public void beforeNavigateTo(final String url, final WebDriver driver) {186 }187 /*188 * (non-Javadoc)189 * 190 * @see191 * org.openqa.selenium.support.events.WebDriverEventListener#beforeScript(192 * java.lang.String, org.openqa.selenium.WebDriver)193 */194 @Override195 public void beforeScript(final String script, final WebDriver driver) {196 }197 /*198 * (non-Javadoc)199 * 200 * @see201 * org.openqa.selenium.support.events.WebDriverEventListener#onException(java202 * .lang.Throwable, org.openqa.selenium.WebDriver)203 */204 @Override205 public void onException(final Throwable throwable, final WebDriver driver) {206 if (driver instanceof ChromeDriver) {207 // ((ChromeDriver) driver).getScreenshotAs(OutputType.FILE);208 }209 }210 /**211 * Sleep.212 * 213 * @param milliseconds214 * the milliseconds215 */216 private void sleep(final int milliseconds) {217 SeleniumUtils.sleep(milliseconds);218 }219 /**220 * Slow.221 */222 private void slow() {223 sleep(700);224 }225}...
Source:EventListener.java
1/*2 * 3 */4package com.mifos.testing.framework.webdriver;5import org.apache.commons.io.FileUtils;6import org.openqa.selenium.*;7import org.openqa.selenium.support.events.WebDriverEventListener;8import java.io.File;9import java.io.IOException;10// TODO: Auto-generated Javadoc11/**12 * The listener interface for receiving event events. The class that is13 * interested in processing a event event implements this interface, and the14 * object created with that class is registered with a component using the15 * component's <code>addEventListener<code> method. When16 * the event event occurs, that object's appropriate17 * method is invoked.18 * 19 * @see EventEvent20 */21public class EventListener implements WebDriverEventListener {22 // @Override23 // public void onException(java.lang.Throwable throwable, WebDriver driver)24 // {25 //26 // WebDriver augmentedDriver = new Augmenter().augment(driver);27 // File source =28 // ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);29 //30 // String path = "./target/screenshots/";31 //32 // if (!(System.getProperty("screenshot.folder").equals(null)) ){33 // path = System.getProperty("screenshot.folder");34 // }35 //36 // path = path + source.getName();37 //38 // try {39 // FileUtils.copyFile(source, new File(path));40 // } catch (IOException e) {41 // // TODO Auto-generated catch block42 // e.printStackTrace();43 // }44 //45 // }46 /*47 * (non-Javadoc)48 * 49 * @see50 * org.openqa.selenium.support.events.WebDriverEventListener#afterChangeValueOf51 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)52 */53 @Override54 public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {55 // TODO Auto-generated method stub56 }57 /*58 * (non-Javadoc)59 * 60 * @see61 * org.openqa.selenium.support.events.WebDriverEventListener#afterClickOn62 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)63 */64 @Override65 public void afterClickOn(WebElement arg0, WebDriver arg1) {66 // TODO Auto-generated method stub67 }68 /*69 * (non-Javadoc)70 * 71 * @see72 * org.openqa.selenium.support.events.WebDriverEventListener#afterFindBy73 * (org.openqa.selenium.By, org.openqa.selenium.WebElement,74 * org.openqa.selenium.WebDriver)75 */76 @Override77 public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {78 arg1.sendKeys(Keys.ARROW_DOWN);79 }80 /*81 * (non-Javadoc)82 * 83 * @see84 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateBack85 * (org.openqa.selenium.WebDriver)86 */87 @Override88 public void afterNavigateBack(WebDriver arg0) {89 // TODO Auto-generated method stub90 }91 /*92 * (non-Javadoc)93 * 94 * @see org.openqa.selenium.support.events.WebDriverEventListener#95 * afterNavigateForward(org.openqa.selenium.WebDriver)96 */97 @Override98 public void afterNavigateForward(WebDriver arg0) {99 // TODO Auto-generated method stub100 }101 /*102 * (non-Javadoc)103 * 104 * @see105 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateTo106 * (java.lang.String, org.openqa.selenium.WebDriver)107 */108 @Override109 public void afterNavigateTo(String arg0, WebDriver arg1) {110 // TODO Auto-generated method stub111 }112 /*113 * (non-Javadoc)114 * 115 * @see116 * org.openqa.selenium.support.events.WebDriverEventListener#afterScript117 * (java.lang.String, org.openqa.selenium.WebDriver)118 */119 @Override120 public void afterScript(String arg0, WebDriver arg1) {121 // TODO Auto-generated method stub122 }123 /*124 * (non-Javadoc)125 * 126 * @see127 * org.openqa.selenium.support.events.WebDriverEventListener#beforeChangeValueOf128 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)129 */130 @Override131 public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {132 // TODO Auto-generated method stub133 }134 /*135 * (non-Javadoc)136 * 137 * @see138 * org.openqa.selenium.support.events.WebDriverEventListener#beforeClickOn139 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)140 */141 @Override142 public void beforeClickOn(WebElement arg0, WebDriver arg1) {143 // TODO Auto-generated method stub144 }145 /*146 * (non-Javadoc)147 * 148 * @see149 * org.openqa.selenium.support.events.WebDriverEventListener#beforeFindBy150 * (org.openqa.selenium.By, org.openqa.selenium.WebElement,151 * org.openqa.selenium.WebDriver)152 */153 @Override154 public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {155 // TODO Auto-generated method stub156 }157 /*158 * (non-Javadoc)159 * 160 * @see161 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateBack162 * (org.openqa.selenium.WebDriver)163 */164 @Override165 public void beforeNavigateBack(WebDriver arg0) {166 // TODO Auto-generated method stub167 }168 /*169 * (non-Javadoc)170 * 171 * @see org.openqa.selenium.support.events.WebDriverEventListener#172 * beforeNavigateForward(org.openqa.selenium.WebDriver)173 */174 @Override175 public void beforeNavigateForward(WebDriver arg0) {176 // TODO Auto-generated method stub177 }178 /*179 * (non-Javadoc)180 * 181 * @see182 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateTo183 * (java.lang.String, org.openqa.selenium.WebDriver)184 */185 @Override186 public void beforeNavigateTo(String arg0, WebDriver arg1) {187 // TODO Auto-generated method stub188 }189 /*190 * (non-Javadoc)191 * 192 * @see193 * org.openqa.selenium.support.events.WebDriverEventListener#beforeScript194 * (java.lang.String, org.openqa.selenium.WebDriver)195 */196 @Override197 public void beforeScript(String arg0, WebDriver arg1) {198 // TODO Auto-generated method stub199 }200 /*201 * (non-Javadoc)202 * 203 * @see204 * org.openqa.selenium.support.events.WebDriverEventListener#onException205 * (java.lang.Throwable, org.openqa.selenium.WebDriver)206 */207 @Override208 public void onException(Throwable e, WebDriver driver) {209 File source = ((TakesScreenshot) driver)210 .getScreenshotAs(OutputType.FILE);211 String path = "./target/screenshots/";212 // if (!(System.getProperty("screenshot.folder").equals(null)) ){213 // path = System.getProperty("screenshot.folder");214 // }215 path = path + source.getName();216 try {217 FileUtils.copyFile(source, new File(path));218 } catch (IOException e2) {219 // TODO Auto-generated catch block220 e.printStackTrace();221 }222 System.setProperty("screenshottaken", "true");223 }224}...
Source:_16_1_WebDriverEvenListenerImpl.java
1package com.automationfraternity;2import org.apache.logging.log4j.LogManager;3import org.apache.logging.log4j.Logger;4import org.openqa.selenium.By;5import org.openqa.selenium.OutputType;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.support.events.AbstractWebDriverEventListener;9import org.openqa.selenium.support.events.WebDriverEventListener;10/*11Steps:12* Classs to be imported13 ```java14 import org.openqa.selenium.support.events.AbstractWebDriverEventListener;15 import org.openqa.selenium.support.events.WebDriverEventListener;16 ```17* Create a Class and Implement WebDriverEventListener18* Implement either 'WebDriverEventListener' or extends 'AbstractWebDriverEventListener'19* WebDriverEventListener which is an interface that has some predefined methods for tracking the Webdriver events. It requires us to implement ALL the methods predefined in the Interface.20* AbstractWebDriverEventListener Class which provides us with the facility of implementing only those methods in which we are interested.21* For Now I am just putting just logs inside these methods.22* They are typically to be used for some advanced logging for your Framework etc.23 */24public class _16_1_WebDriverEvenListenerImpl extends AbstractWebDriverEventListener {25 private static final Logger logger = LogManager.getLogger(_16_1_WebDriverEvenListenerImpl.class);26 @Override27 public void beforeFindBy(By by, WebElement element, WebDriver driver) {28 logger.info("Logging WebDriver Event: beforeFindBy. ");29 }30 @Override31 public void afterFindBy(By by, WebElement element, WebDriver driver) {32 logger.info("Logging WebDriver Event: afterFindBy. ");33 }34 @Override35 public void beforeClickOn(WebElement element, WebDriver driver) {36 logger.info("Logging WebDriver Event: beforeClickOn");37 }38 @Override39 public void afterClickOn(WebElement element, WebDriver driver) {40 logger.info("Logging WebDriver Event: afterClickOn");41 }42 @Override43 public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {44 logger.info("Logging WebDriver Event: beforeChangeValueOf");45 }46 @Override47 public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {48 logger.info("Logging WebDriver Event: afterChangeValueOf");49 }50 @Override51 public void onException(Throwable throwable, WebDriver driver) {52 logger.info("Logging WebDriver Event: onException");53 }54 @Override55 public <X> void beforeGetScreenshotAs(OutputType<X> target) {56 logger.info("Logging WebDriver Event: beforeGetScreenshotAs");57 }58 @Override59 public <X> void afterGetScreenshotAs(OutputType<X> target, X screenshot) {60 logger.info("Logging WebDriver Event: afterGetScreenshotAs");61 }62 @Override63 public void beforeGetText(WebElement element, WebDriver driver) {64 logger.info("Logging WebDriver Event: beforeGetText");65 }66 @Override67 public void afterGetText(WebElement element, WebDriver driver, String text) {68 logger.info("Logging WebDriver Event: afterGetText");69 }70}...
Source:WebDriverContainer.java
1package com.codeborne.selenide.impl;2import com.codeborne.selenide.DownloadsFolder;3import com.codeborne.selenide.proxy.SelenideProxyServer;4import org.openqa.selenium.Proxy;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.support.events.WebDriverEventListener;7import org.openqa.selenium.support.events.WebDriverListener;8import javax.annotation.CheckReturnValue;9import javax.annotation.Nonnull;10import javax.annotation.Nullable;11import javax.annotation.ParametersAreNonnullByDefault;12@ParametersAreNonnullByDefault13public interface WebDriverContainer {14 void addListener(WebDriverEventListener listener);15 void addListener(WebDriverListener listener);16 void removeListener(WebDriverEventListener listener);17 void removeListener(WebDriverListener listener);18 void setWebDriver(WebDriver webDriver);19 void setWebDriver(WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy);20 void setWebDriver(WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy, DownloadsFolder browserDownloadsFolder);21 void resetWebDriver();22 @CheckReturnValue23 @Nonnull24 WebDriver getWebDriver();25 @CheckReturnValue26 @Nullable27 SelenideProxyServer getProxyServer();28 void setProxy(@Nullable Proxy webProxy);29 @CheckReturnValue30 @Nonnull31 WebDriver getAndCheckWebDriver();32 @CheckReturnValue33 @Nonnull34 DownloadsFolder getBrowserDownloadsFolder();35 void closeWindow();36 void closeWebDriver();37 boolean hasWebDriverStarted();38 void clearBrowserCache();39 @CheckReturnValue40 @Nonnull41 String getPageSource();42 @CheckReturnValue43 @Nonnull44 String getCurrentUrl();45 @CheckReturnValue46 @Nonnull47 String getCurrentFrameUrl();48}...
Source:AppiumWebDriverEventListener.java
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 additional5 * information regarding copyright ownership.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16package io.appium.java_client.events.api.general;17import io.appium.java_client.events.api.Listener;18import org.openqa.selenium.support.events.WebDriverEventListener;19public interface AppiumWebDriverEventListener extends Listener, WebDriverEventListener, ListensToException,20 SearchingEventListener, NavigationEventListener,21 JavaScriptEventListener, ElementEventListener {22}...
Source:ScenarioEventListener.java
1package com.helios.scriptutils;2import org.openqa.selenium.support.events.WebDriverEventListener;3public interface ScenarioEventListener extends WebDriverEventListener {4 public abstract void onScenarioStart(ScenarioState scenarioState);5 void onScenarioEnd(ScenarioState scenarioState);6 void onScenarioPass(ScenarioState scenarioState2);7 public abstract void onScenarioFailed(ScenarioState scenarioState);8 public abstract void onPageObjectChange(ScenarioState scenarioState, String previous, String current);9 public abstract void onGlobalFunctionCalled(ScenarioState scenarioState, String pageObjectName,10 String functionInfo);11}...
Source:WebDriverCloseAndQuitEventListener.java
1package org.jsystem.webdriver_so.eventlistener;2import org.openqa.selenium.support.events.WebDriverEventListener;3public interface WebDriverCloseAndQuitEventListener extends WebDriverEventListener {4 /**5 * Called before {@link org.openqa.selenium.WebDriver#quit()}.6 */7 public void beforeQuit();8 9 /**10 * Called after {@link org.openqa.selenium.WebDriver#quit()}.11 */12 public void afterQuit();13 /**14 * Called before {@link org.openqa.selenium.WebDriver#close()}.15 */16 public void beforeClose();17 /**18 * Called after {@link org.openqa.selenium.WebDriver#close()}.<br>19 */20 public void afterClose();21}...
Interface WebDriverEventListener
Using AI Code Generation
1package com.automationpractice;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.events.EventFiringWebDriver;7import org.openqa.selenium.support.events.WebDriverEventListener;8public class EventFiringWebDriverDemo {9 public static void main(String[] args) {10 System.setProperty("webdriver.chrome.driver", "C:\\Users\\soura\\Downloads\\chromedriver_win32\\chromedriver.exe");11 WebDriver driver = new ChromeDriver();12 EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver);13 HandleEvents handleEvents = new HandleEvents();14 eventFiringWebDriver.register(handleEvents);15 eventFiringWebDriver.findElement(By.name("q")).sendKeys("Selenium");16 }17}18package com.automationpractice;19import org.openqa.selenium.By;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.support.events.WebDriverEventListener;23public class HandleEvents implements WebDriverEventListener{24 public void afterAlertAccept(WebDriver arg0) {25 }26 public void afterAlertDismiss(WebDriver arg0) {27 }28 public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {29 }30 public void afterClickOn(WebElement arg0, WebDriver arg1) {31 }32 public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {33 }34 public void afterNavigateBack(WebDriver arg0) {35 }36 public void afterNavigateForward(WebDriver arg0) {37 }38 public void afterNavigateRefresh(WebDriver arg0) {39 }40 public void afterNavigateTo(String arg0, WebDriver arg1) {41 }42 public void afterScript(String arg0, WebDriver arg1)
Interface WebDriverEventListener
Using AI Code Generation
1package com.automationtesting;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.events.AbstractWebDriverEventListener;7import org.openqa.selenium.support.events.EventFiringWebDriver;8public class EventListenerExample extends AbstractWebDriverEventListener {9 public static void main(String[] args) throws InterruptedException {10 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver_win32\\chromedriver.exe");11 WebDriver driver = new ChromeDriver();12 EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);13 EventListenerExample eventListener = new EventListenerExample();14 eventDriver.register(eventListener);15 eventDriver.findElement(By.name("q")).sendKeys("Selenium");16 Thread.sleep(3000);17 eventDriver.findElement(By.name("btnK")).click();18 Thread.sleep(3000);19 eventDriver.quit();20 }21 public void beforeFindBy(By by, WebElement element, WebDriver driver) {22 System.out.println("Trying to find Element By : " + by.toString());23 }24 public void afterFindBy(By by, WebElement element, WebDriver driver) {25 System.out.println("Element Found By : " + by.toString());26 }27 public void onException(Throwable throwable, WebDriver driver) {28 System.out.println("Exception occured : " + throwable);29 }30}
Interface WebDriverEventListener
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.support.events.AbstractWebDriverEventListener;5import org.openqa.selenium.support.events.EventFiringWebDriver;6public class WebDriverEventListenerExample {7public static void main(String[] args) {8WebDriver driver = new ChromeDriver();9EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);10eventDriver.register(new EventListener());11eventDriver.findElement(By.name("q")).sendKeys("Selenium");12eventDriver.findElement(By.name("btnK")).click();13eventDriver.quit();14}15}16class EventListener extends AbstractWebDriverEventListener {17public void beforeFindBy(By by, WebElement element, WebDriver driver) {18System.out.println("Trying to find Element By : " + by.toString());19}20public void afterFindBy(By by, WebElement element, WebDriver driver) {21System.out.println("Found Element By : " + by.toString());22}23public void onException(Throwable throwable, WebDriver driver) {24System.out.println("Exception occured: " + throwable);25}26}27Exception occured: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"btnK"}28 (Session info: chrome=71.0.3578.98)
Interface WebDriverEventListener
Using AI Code Generation
1package com.automation.selenium.listeners;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.support.events.WebDriverEventListener;6public class WebDriverListener implements WebDriverEventListener{7 public void afterChangeValueOf(WebElement element, WebDriver driver) {8 }9 public void afterClickOn(WebElement element, WebDriver driver) {10 }11 public void afterFindBy(By by, WebElement element, WebDriver driver) {12 }13 public void afterNavigateBack(WebDriver driver) {14 }15 public void afterNavigateForward(WebDriver driver) {16 }17 public void afterNavigateTo(String url, WebDriver driver) {18 }19 public void afterScript(String script, WebDriver driver) {20 }21 public void beforeChangeValueOf(WebElement element, WebDriver driver) {22 }23 public void beforeClickOn(WebElement element, WebDriver driver) {24 }25 public void beforeFindBy(By by, WebElement element, WebDriver driver) {26 }27 public void beforeNavigateBack(WebDriver driver) {28 }29 public void beforeNavigateForward(WebDriver driver) {30 }31 public void beforeNavigateTo(String url, WebDriver driver) {32 }33 public void beforeScript(String script, WebDriver driver) {34 }35 public void onException(Throwable throwable, WebDriver driver) {36 }37}38package com.automation.selenium.listeners;39import org.openqa.selenium.By;40import org.openqa.selenium.WebDriver;41import org.openqa.selenium.WebElement;42import org.openqa.selenium.support.events.WebDriverEventListener;43public class WebDriverListener implements WebDriverEventListener{
Interface WebDriverEventListener
Using AI Code Generation
1package com.selenium4beginners.java.webdriver;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.events.AbstractWebDriverEventListener;7{8 public static void main (String[] args) 9 {10 WebDriver driver = new ChromeDriver ();11 driver.findElement(By.id("at-cv-lightbox-close")).click();12 driver.findElement(By.id("btn_basic_example")).click();13 driver.findElement(By.linkText("Simple Form Demo")).click();14 driver.findElement(By.id("user-message")).sendKeys("Hello World");15 driver.quit();16 }17}18{19 public void beforeFindBy(By by, WebElement element, WebDriver driver) 20 {21 System.out.println("Trying to find Element By : " + by.toString());22 }23 public void afterFindBy(By by, WebElement element, WebDriver driver) 24 {25 System.out.println("Found Element By : " + by.toString());26 }27 public void onException(Throwable error, WebDriver driver) 28 {29 System.out.println("Exception occured: " + error);30 }31}
Interface WebDriverEventListener
Using AI Code Generation
1package com.test;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.support.events.WebDriverEventListener;6public class EventListener implements WebDriverEventListener {7 public void beforeAlertAccept(WebDriver driver) {8 }9 public void afterAlertAccept(WebDriver driver) {10 }11 public void afterAlertDismiss(WebDriver driver) {12 }13 public void beforeAlertDismiss(WebDriver driver) {14 }15 public void beforeNavigateTo(String url, WebDriver driver) {16 }17 public void afterNavigateTo(String url, WebDriver driver) {18 }19 public void beforeNavigateBack(WebDriver driver) {20 }21 public void afterNavigateBack(WebDriver driver) {22 }23 public void beforeNavigateForward(WebDriver driver) {24 }25 public void afterNavigateForward(WebDriver driver) {26 }27 public void beforeNavigateRefresh(WebDriver driver) {28 }29 public void afterNavigateRefresh(WebDriver driver) {30 }31 public void beforeFindBy(By by, WebElement element, WebDriver driver) {32 }33 public void afterFindBy(By by, WebElement element, WebDriver driver) {34 }35 public void beforeClickOn(WebElement element, WebDriver driver) {36 }37 public void afterClickOn(WebElement element, WebDriver driver) {38 }39 public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {40 }41 public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!