如何使用Selenium WebDriver处理多个jQuery弹出窗口 [英] How to handle multiple jQuery popup with selenium webdriver

查看:187
本文介绍了如何使用Selenium WebDriver处理多个jQuery弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Webdriver 2.39开发Java,我们有一个应用程序,其中多个处理"弹出窗口显示2-5秒并自动关闭,这取决于数据.现在,问题是如何处理此弹出窗口,此弹出窗口是jQuery弹出窗口.只有在这三个弹出窗口都打开并处理数据并自动关闭后,我的脚本才能进一步工作.但是,我无法使用等待时间,因为此脚本用于使用JMeter进行负载测试,因此处理时间可能会超过5秒钟或少于5秒钟.我们是否有办法知道屏幕上是否存在弹出窗口?我已经使用下面给出的示例代码,但是它仅返回父窗口,并且不能识别jQuery弹出窗口,使用下面给出的代码,我可以获取是否存在弹出窗口,但仅当它不是jQuery弹出窗口时才可以.谁能帮我吗?

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);
    }
}

推荐答案

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);
    }

这篇关于如何使用Selenium WebDriver处理多个jQuery弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆