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>
  • 相关阅读:
    HDOJ 5414 CRB and String 模拟
    Python标准库:内置函数all(iterable)
    Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.
    关于TabLayout+ViewPager组合实现多页面滑动
    互联网产品经理应该具备的技能(需求篇)
    【Android】利用自己定义View的重绘实现拖动移动,获取组件的尺寸
    mybatis自己主动生成mapper,dao,映射文件
    Java解析注解
    如日中天的Uber到底是用什么开发语言做到的?
    [Swift]LeetCode1002. 查找常用字符 | Find Common Characters
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289573.html
Copyright © 2011-2022 走看看