zoukankan      html  css  js  c++  java
  • selenium测试(Java)--告警框处理(十四)

    下面代码中介绍了告警框的处理方法

    package com.test.alerthandle;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.TimeoutException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class AlterHandle {
    
        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/alerthandle/alert.html");
            driver.manage().window().maximize();
    
            driver.findElement(By.cssSelector("#altertest")).click();
    
            try {
                // 先等待prompt框的出现,然后输入内容
                new WebDriverWait(driver, 5).until(ExpectedConditions.alertIsPresent());
                driver.switchTo().alert().sendKeys("处理告警框的例子");
    
                // 确认输入内容
                waitTime(3000);
                driver.switchTo().alert().accept();
    
                // 获取Alert框内text内容
                waitTime(2000);
                new WebDriverWait(driver, 5).until(ExpectedConditions.alertIsPresent());
                String inputInfo = driver.switchTo().alert().getText();
                System.out.println(inputInfo);
    
                // 关闭Alert框
                waitTime(3000);
                driver.switchTo().alert().accept();
    
                // 利用js构造一个confirm框
                waitTime(3000);
                String js = "confirm("这就是一个告警框的例子")";
                ((JavascriptExecutor) driver).executeScript(js);
    
                // 取消confirm框
                waitTime(3000);
                driver.switchTo().alert().dismiss();
    
                waitTime(3000);
                driver.quit();
    
            } catch (TimeoutException e) {
                driver.quit();
            }
    
        }
    
        static public void waitTime(int time) {
    
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    实例例子:

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Alter</title>
    <link
        href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
        rel="stylesheet" />
    </head>
    <body>
        <button id="altertest" onclick="disp_alert()">this is an alter</button>
        <script type="text/javascript">
            function disp_alert() {
                var a=prompt("请输入信息:","");            
                alert("你输入的信息是:" + a );
            }
        </script>
    </body>
    <script
        src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
    </html>
  • 相关阅读:
    下一个ajax异步请求被挂起问题
    借鉴别人的Oracle 11g安装和卸载图文教程
    Html5 实现网页截屏 页面生成图片(图文)
    Oracle修改字段类型方法小技巧
    基于轻量级ORM框架Dapper的扩展说明
    JavaScript+html5 canvas实现本地截图教程
    SkipList跳表基本原理
    Oracle日期查询:季度、月份、星期等时间信息
    设计模式之模板模式
    设计模式之解释器模式
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289573.html
Copyright © 2011-2022 走看看