Executing and verifying the events

Now it's time for our test script to execute events, such as navigation events. Let's first navigate to Google and then Facebook. We will use the browser back-navigation to go back to Google. The full code of the test script is as follows:

public class IAmTheDriver {
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();
eventFiringDriver.register(eventListener);
eventFiringDriver.get("http://www.google.com");
eventFiringDriver.get("http://www.facebook.com");
eventFiringDriver.navigate().back();
} finally {
driver.close();
driver.quit();
}
}
}

In the preceding code, we modify our listener class to record navigateTo and navigateBack before and after events inherited from the AbstractWebDriverEventListener class. The modified methods are as follows:

@Override
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("Before Navigate To: " + url
+ " and Current url is: " + driver.getCurrentUrl());
}

@Override
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("After Navigate To: " + url
+ " and Current url is: " + driver.getCurrentUrl());
}

@Override
public void beforeNavigateBack(WebDriver driver) {
System.out.println("Before Navigate Back. Right now I'm at " + driver.getCurrentUrl());
}

@Override
public void afterNavigateBack(WebDriver driver) {
System.out.println("After Navigate Back. Right now I'm at " + driver.getCurrentUrl());
}

Now if you execute your test script, the output will be as follows:

Before Navigate To: http://www.google.com and Current url is: data:,
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
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/
After Navigate Back. Right now I'm at https://www.google.com/?gws_rd=ssl

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.214.215