0

I am using Selenide to test a website. I need some event listeners set up on every page. To do so I need some kind of page load event that tells me whenever Selenium is loading a new page.

A possible (but ugly) solution would be checking the current url in a loop every x seconds.


Solution:

WebDriverRunner.addListener(new WebDriverEventListener() {
    private String urlBeforeClick;

    @Override
    public void afterNavigateTo(String s, WebDriver webDriver) {
        // code here
    }

    @Override
    public void beforeClickOn(WebElement webElement, WebDriver webDriver) {
        this.urlBeforeClick = webDriver.getCurrentUrl();
    }

    @Override
    public void afterClickOn(WebElement webElement, WebDriver webDriver) {
        // if the url is different after a click a new page was loaded.
        if (!Objects.equals(this.urlBeforeClick, webDriver.getCurrentUrl()))
            // code here
    }
});

Note: I dont think this is should be marked as a duplicate.

Error404
  • 719
  • 9
  • 30
  • No, I dont want to know when a page has finished loading. I want to be notified whenever a page is being loaded. – Error404 Aug 13 '19 at 16:39
  • Then you probably want to use the `ready` event instead of the `onload` event. See https://stackoverflow.com/questions/3698200/window-onload-vs-document-ready. – tarik Aug 13 '19 at 17:37
  • No, I want to be notified whenever selenium loads starts to load a new page, not when a particular page is fully loaded – Error404 Aug 14 '19 at 07:49

2 Answers2

2

I'm coming from C# so I don't know the exact code but you still should be able to wrap your WebDriver in EventFiringWebDriver and use beforeNavigateTo. Which according to that link is available in java as well.

Edit: As pointed out in the comments beforeNavigateTo only fires when directly sending the navigate command to the driver. Instead listen to EventClicking & ElementClicked events, cache the current URL on the clicking event and then check if it has changed on the clicked event.

J.D. Cain
  • 639
  • 4
  • 16
  • This works for page navigation I do from java, but not when a link is clicked on the page – Error404 Aug 14 '19 at 10:55
  • That's right, I forgot that does not get triggered. I pulled up some old code and found what I did was listen to the EventClicking & ElementClicked events, cache the current URL on the clicking event and then check if it has changed on the clicked event. – J.D. Cain Aug 14 '19 at 11:17
1

This page talks about an "unload" event. Could this work for you?

Javascript event that runs before page changes

I also found that an anonymous function in the page will run before the onLoad event. https://jsfiddle.net/pawqhdjy/

function codeAddress() {
    alert('second');
}

window.onload = codeAddress;

(function () {
    alert("first");
})();
shanecandoit
  • 581
  • 1
  • 3
  • 11
  • THe unload event does no help me as I need to execute js on the new page. The load event also does not work for me as cant know when to attach the listener – Error404 Aug 14 '19 at 07:44