selenium弹出框相关操作(获取,点击)

    /**
     * 检测是否有弹出框,使用默认超时时间
     * @return
     * (1/2)
     */
    public boolean alertExists(){
        return alertExists(timeout);
    }

    /**
     * 检测是否有弹出框,可以指定超时时间
     * @param timeout
     * @return
     * (2/2)
     */
    private boolean alertExists(float timeout) {
        // TODO Auto-generated method stub
        long start = System.currentTimeMillis();
        while ((System.currentTimeMillis() -start) < timeout * 1000) {
            try {
                driver.switchTo().alert();
                return true;
            } catch (NoAlertPresentException ne) {
                LogRecorder.Info("没有检测到弹出框");
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        return false;
    }

    /**
     * 获取弹出框,使用全局超时时间
     * @return
     * (1/2)
     */
    public Alert getAlert() {
        return getAlert(timeout);
    }

    /**
     * 获取弹出框,可以指定超时时间
     * @param timeout
     * @return
     * (2/2)
     */
    private Alert getAlert(float timeout) {
        // TODO Auto-generated method stub
        long start = System.currentTimeMillis();
        while ((System.currentTimeMillis() -start) < timeout * 1000) {
            try {
                Alert alert = driver.switchTo().alert();
                return alert;
            } catch (NoAlertPresentException ne) {
                LogRecorder.Info("没有检测到弹出框");
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        return null;
    }

    /**
     * 点击弹出框内确认按钮,使用默认超时时间
     * @return
     * (1/2)
     */
    public boolean acceptAlert(){
        return acceptAlert(timeout);
    }

    /**
     * 点击弹出框内确认按钮,可以指定超时时间
     * @param timeout
     * @return
     * (2/2)
     */
    private boolean acceptAlert(float timeout) {
        // TODO Auto-generated method stub
        Alert alert = getAlert(timeout);
        if (alert == null) {
            return false;
        } else {
            alert.accept();
            return true;
        }
    }

    /**
     * 点击弹出框内的取消按钮,使用默认超时时间
     * @return
     * (1/2)
     */
    public boolean cancelAlert(){
        return cancelAlert(timeout);
    }

    /**
     * 点击弹出框内的取消按钮,可以指定超时时间
     * @param timeout2
     * @return
     * (2/2)
     */
    private boolean cancelAlert(float timeout) {
        // TODO Auto-generated method stub
        Alert alert = getAlert(timeout);
        if (alert == null) {
            return false;
        } else {
            alert.dismiss();
            return true;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_40049311/article/details/80197880
今日推荐