Karim Narsindani
Karim Narsindani

Reputation: 454

How to handle multiple jQuery popup with selenium webdriver

I am working on java with selenium webdriver 2.39, we have application where multiple 'processing' popup is display for 2-5 sec and closed automatically, that is depend on data. Now, the question is how to handle this popup, this popup are jQuery popup. My script can only work further once all this three popup gets open and process data and get closed automatically. However, I can not use wait time as this script is used for load testing using JMeter, hence the process time may take more or less than 5 sec., Is there any way we can know if the popup exist or not on screen? I have used below given sample code but it returns only parent window and it does not identify jQuery popup, using below given code I can get if popup exist or not, but only if it is not jQuery popup. Can anyone help me?

public void FocusOnWindow() throws Exception{

    int i=0;

    do {
        handles=driver.getWindowHandles();//get all windows
        iterator = handles.iterator();
        if(iterator.hasNext()){
            subWindowHandler = iterator.next();
            if(subWindowHandler==null){
                i=0;
            }else if(subWindowHandler!=null){
                if(subWindowHandler!=parentWindowHandler){
                    popup = true;
                    i=2;
                }
            }
        }
    }while(i<2);
    if(popup){
        do{
            handles=driver.getWindowHandles();
            iterator = handles.iterator();
            Thread.sleep(500);
            if(iterator.hasNext()){
                subWindowHandler = iterator.next();
                if(subWindowHandler!=parentWindowHandler){
                    if(subWindowHandler==null){
                        String source = driver.getPageSource();
                        if(source==null){
                            i=2;
                        }
                    }
                }else {
                    i=0;
                }
                //System.out.println("No any other popup.");
            }

        }while (i<2);
    }
}

Upvotes: 0

Views: 2411

Answers (2)

Karim Narsindani
Karim Narsindani

Reputation: 454

public boolean runScript(){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        return  (Boolean) js.executeScript("return jQuery.active==0;");
        }


    public void FocusOnWindow() throws Exception{
        int i=0;
        do {
            if(!runScript()){
                System.out.println("Popup exists");
                i++;
            }else{
                i=5000;
                System.out.println("Popup does not exists");
            }
        }while(i<5000);
    }

Upvotes: 0

Vabz
Vabz

Reputation: 11

First of all, I'll strongly suggest not to put hard wait at all.

If you are aware of any of the element which is unique and part of post processing pop up screen (i.e. resulted user screen) then make use of selenium waitForElement() API function which intelligently wait for element to appear and once appeared performs further actions.

Take a look at this link which explains the advantages of using it.

And with Java bindings for selenium in place, You can use something like this -

WebDriverWait wait = new WebDriverWait(driver, /*seconds=*/3);
elementOfPage = wait.until(presenceOfElementLocated(By.id("id_of_element")));

Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
return new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(locator);
    }
};
}

Upvotes: 1

Related Questions