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>
  • 相关阅读:
    HDU 6191 Query on A Tree ( 2017广西邀请赛 && 可持久化Trie )
    BZOJ 4318 OSU! ( 期望DP )
    洛谷 P2473 [SCOI2008]奖励关 ( 期望DP )
    Codeforces #499 E Border ( 裴蜀定理 )
    HDU 6444 Neko's loop ( 2018 CCPC 网络赛 && 裴蜀定理 && 线段树 )
    HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )
    Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )
    Nowcoder Playing Games ( FWT 优化 DP && 博弈论 && 线性基)
    js中的深拷贝与浅拷贝
    nrm 源管理器
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289573.html
Copyright © 2011-2022 走看看