0

My code works as far as opening a browser window and browsing to a URL. No errors are caught in my code. I need to handle events generated by the browser, particularly when the user closes the browser window. None of the messages are caught in my OverrideClass.

I have this:

try {
    System.setProperty("webdriver.gecko.driver", "./geckodriver.exe");
    driver = new FirefoxDriver();

    EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
    OverrideClass oc = new OverrideClass();
    eventDriver.register(oc);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (Exception ex) {
    Log.logError("Browser.initBrowser(): " + ex.getLocalizedMessage());
}

The message handler looks like this:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class OverrideClass implements WebDriverEventListener{
//  @Override
//  public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
//
//  }
    @Override
    public void afterClickOn(WebElement arg0, WebDriver arg1) {
        System.out.println("OverrideClass.afterClickOn(): " + arg0.toString());
    }
    @Override
    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        System.out.println("OverrideClass.afterFindBy(): " + arg0.toString());  }
    @Override
    public void afterNavigateBack(WebDriver driver) {
        System.out.println("After clicking back  "+ driver.getCurrentUrl());
    }
    @Override
    public void afterNavigateForward(WebDriver arg0) {

    }
    @Override
    public void afterNavigateTo(String arg0, WebDriver arg1) {

    }
    @Override
    public void afterScript(String arg0, WebDriver arg1) {

    }
//  @Override
//  public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
//
//  }
    @Override
    public void beforeClickOn(WebElement arg0, WebDriver arg1) {

    }
    @Override
    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {

    }
    @Override
    public void beforeNavigateBack(WebDriver driver) {
        System.out.println("OverrideClass.beforeNavigateBack() Before clicking back: " + driver.getCurrentUrl());
    }
    @Override
    public void beforeNavigateForward(WebDriver arg0) {
        System.out.println("OverrideClass.beforeNavigateForward() Before clicking back: " + arg0.getCurrentUrl());
    }
    @Override
    public void beforeNavigateTo(String arg0, WebDriver arg1) {
        System.out.println("OverrideClass.beforeNavigateTo(): " + arg0);
    }
    @Override
    public void beforeScript(String arg0, WebDriver arg1) {

    }
    @Override
    public void onException(Throwable arg0, WebDriver arg1) {
        System.out.println("OverrideClass.onException(): " + arg0.getLocalizedMessage());
    }
    @Override
    public void afterAlertAccept(WebDriver arg0) {


    }
    @Override
    public void afterAlertDismiss(WebDriver arg0) {


    }
    @Override
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {


    }
    @Override
    public void afterNavigateRefresh(WebDriver arg0) {


    }
    @Override
    public void beforeAlertAccept(WebDriver arg0) {


    }
    @Override
    public void beforeAlertDismiss(WebDriver arg0) {


    }
    @Override
    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {


    }
    @Override
    public void beforeNavigateRefresh(WebDriver arg0) {


    }
}
nicomp
  • 4,344
  • 4
  • 27
  • 60
  • 2
    Are you loading the page via the `eventDriver`? It seems you still use the original `driver` (`driver.manage().timeouts()`) rather than the `eventDriver` wrapper. – Vince Aug 13 '17 at 14:45
  • 2
    EventFiringWebDriver is for listening to events that are initiated through code on that driver and not for the user generated events. If user changes url of the browser, no event would be fired on your code. So this events will only be fired when you take action on the `eventDriver` object – Tarun Lalwani Aug 13 '17 at 15:19
  • @VinceEmigh Thanks! Good catch. – nicomp Aug 13 '17 at 15:50
  • @TarunLalwani Bummer. Thanks for clarifying that. :) – nicomp Aug 13 '17 at 15:51
  • Please read [ask], especially the part about [mcve] (MCVE), and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) This will help you debug your own programs and solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and the execution result including any error messages so we can better help you. Also provide a link to the page and/or the relevant HTML. – JeffC Aug 13 '17 at 21:33
  • Is your query solved? Let me know the status and if you need further help. – undetected Selenium Aug 14 '17 at 03:53
  • @DebanjanB My query is not solved because I need to handle events generated by the user interacting with the browser, particularly when the user closes the browser window. The WebDriverEventListener does not do that, I have learned. – nicomp Aug 15 '17 at 10:22

2 Answers2

0

So selenium works with a series of commands emulating user interaction, passing those commands to the driver. Firefox I believe has a plugin allowing you to create the commands by browsing manually, which you can then pump back into the driver for automated testing.

I don't see any "click" or other commands being used in sample code above. See this for an example of programmatically issuing commands for driver.

ZagNut
  • 1,431
  • 15
  • 20
  • I can already issue commands for the driver, no problem. I can't get the driver to tell me when the browser window closes. – nicomp Aug 13 '17 at 15:42
  • stopping the driver is the equivalent of browser window closing. – ZagNut Aug 14 '17 at 01:25
  • I need to know when the user closes the browser window. – nicomp Aug 15 '17 at 13:06
  • The instance of the webdriver IS the "browser window". You can (1) [try this](https://stackoverflow.com/questions/27616470/webdriver-how-to-check-if-browser-still-exists-or-still-open) or (2) [try this](https://stackoverflow.com/questions/31048777/get-browser-closing-event-in-selenium-web-browser) – ZagNut Aug 16 '17 at 12:25
0

You have to take care a couple of stuffs here as follows:

  1. To see some browser events being handled you need to access some url like "https://google.com"
  2. To see some more browser events as per the event handlers you need to access some WebElement.
  3. So, your minimal code to see some events being fired will be:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;
    
    public class my_program 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
            OverrideClass oc = new OverrideClass();
            eventDriver.register(oc);
            eventDriver.get("https://google.com");
            WebElement element = eventDriver.findElement(By.linkText("Gmail"));
            element.click();
        }
    
    }
    
  4. Now, you have already created some Sysouts to see the browser events being handled which remains unchanged.

  5. Finally when you execute this program, you will see the following output on your console:

    OverrideClass.beforeNavigateTo(): https://google.com
    OverrideClass.afterFindBy(): By.linkText: Gmail
    OverrideClass.afterClickOn(): [[FirefoxDriver: firefox on XP (b1412059-9499-45ff-b49a-2bc69a3ace30)] -> link text: Gmail]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352