- Learn Selenium
- Unmesh Gundecha Carl Cocchiaro
- 174字
- 2021-06-24 13:25:49
Registering multiple EventListeners
We can register more than one listener with EventFiringWebDriver. Once the event occurs, all of the registered listeners are notified about it. Let's modify our test script to register both our IAmTheListener.java and IAmTheListener2.java files:
public class RegisteringMultipleListeners {
public static void main(String... args){
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
WebDriver driver = new ChromeDriver();
try {
EventFiringWebDriver eventFiringDriver = new
EventFiringWebDriver(driver);
IAmTheEventListener eventListener = new IAmTheEventListener();
IAmTheEventListener2 eventListener2 = new
IAmTheEventListener2();
eventFiringDriver.register(eventListener);
eventFiringDriver.register(eventListener2);
eventFiringDriver.get("http://www.google.com");
eventFiringDriver.get("http://www.facebook.com");
eventFiringDriver.navigate().back();
} finally {
driver.close();
driver.quit();
}
}
}
Modify the listeners slightly to differentiate the log statements. Now if you execute the preceding code, you will see the following output:
Before Navigate To: http://www.google.com and Current url is: data:,
Before Navigate To http://www.google.com
After Navigate To: http://www.google.com and Current url is: https://www.google.com/?gws_rd=ssl
Before Navigate To: http://www.facebook.com and Current url is: https://www.google.com/?gws_rd=ssl
Before Navigate To http://www.facebook.com
After Navigate To: http://www.facebook.com and Current url is: https://www.facebook.com/
Before Navigate Back. Right now I'm at https://www.facebook.com/
Before Navigate Back. Right now I'm at https://www.facebook.com/
After Navigate Back. Right now I'm at https://www.google.com/?gws_rd=ssl