seleniumポップアップボックスに関する操作(取得、クリック)

5828 ワード

    /**
     *         ,        
     * @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;
        }
    }