操作JavaScript的Alert窗口

  测试网址代码 

  <html>
    <head>
      <title>你喜欢的水果</title>
    </head>
    <body>
      <input id='button' type='button' onclick="alert('这是一个alert弹窗');" value='单击此按钮,弹出alert弹出窗'/></input>
    </body>
  </html>

  Java语言版本API实例 

  @Test
  public void testHandleAlert() {
    driver.manage().window().maximize();
    driver.get(url);
    WebElement button = driver.findElement(By.xpath("//input"));
    button.click();
    try {
      //使用driver.switchTo.alert()方法获取Alert对象
      Alert alert = driver.switchTo().alert();
      //断言判断
      Assert.assertEquals("这是一个alert弹窗", alert.getText());
      alert.accept();
    } catch (NoAlertPresentException exception) {
      Assert.fail("尝试操作的alert框未被找到");
      exception.printStackTrace();
    }
  }

  操作JavaScript的confirm窗口

  测试网址代码 

  <html>
    <head>
      <title>你喜欢的水果</title>
    </head>
    <body>
      <input id='button' type='button' onclick="confirm('这是一个confirm弹窗');" value='单击此按钮,弹出confirm弹出窗'/></input>
    </body>
  </html>

  Java语言版本API实例 

  @Test
  public void testHandleconfirm() {
    driver.manage().window().maximize();
    driver.get(url);
    WebElement button = driver.findElement(By.xpath("//input"));
    button.click();
    try {
      Alert alert = driver.switchTo().alert();
      Assert.assertEquals("这是一个confirm弹窗", alert.getText());
      alert.accept();
    } catch (NoAlertPresentException exception) {
      Assert.fail("尝试操作confirm未被找到");
      exception.printStackTrace();
    }
  }

  操作JavaScript的prompt窗口

  测试网址代码 

  <html>
    <head>
      <title>你喜欢的水果</title>
    </head>
    <body>
      <input id='button' type='button' onclick="prompt('这是一个prompt弹窗');" value='单击此按钮,弹出prompt弹出窗'/></input>
    </body>
  </html>

  Java语言版本API实例 

  @Test
  public void testHandlePrompt() {
    driver.manage().window().maximize();
    driver.get(url);
    WebElement button = driver.findElement(By.xpath("//input"));
    button.click();
    try {
      Alert alert = driver.switchTo().alert();
      Assert.assertEquals("这是一个prompt弹窗", alert.getText());
      //在prompt弹窗上输入内容
      alert.sendKeys("selemiun");
      //点击prompt上的确定按钮
      alert.accept();
      //alert.dismiss(); 取消按钮
    } catch (NoAlertPresentException exception) {
      Assert.fail("未找到prompt窗口");
      exception.printStackTrace();
    }
  }

WebDriverAPI(9)的更多相关文章

  1. WebDriverAPI(7)

      查看页面元素的属性 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void getWebElementAttribute() { dri ...

  2. WebDriverAPI(10)

    操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...

  3. WebDriverAPI(4)

    单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...

  4. WebDriverAPI(2)

    操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...

  5. WebDriverAPI(8)

    判断页面元素是否存在 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void testIsElementPresent(){ driver. ...

  6. WebDriverAPI(6)

    在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...

  7. WebDriverAPI(5)

    将当前浏览器截屏 测试网址 http://www.baidu.com Java语言版本实例 @Test public void captureScreenInCurrentWindows() { dr ...

  8. WebDriverAPI(3)

    获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...

  9. WebDriverAPI(1)

    访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...

随机推荐

  1. Notepad++ 中如何将代码格式化

    Notepad++ 中如何将代码格式化   在阅读别人的代码时偶尔会遇到格式很乱,阅读起来很费劲的情况,若手动改,很容易出错且很费时间,这时可以借助一些专业的编辑器来格式化代码,NotePad++是一 ...

  2. 学习Java的方法

    许多人在刚开始学习Java时,会因为学习方法的不正确,而丧失信心,从而半途而废.所以,今天,巩固就要教教大家学习Java的方法. 1.多练习 编程其实是一个非常抽象的东西,要想学好它,就不能只是看看书 ...

  3. 17)maven-surefire-plugin

    http://maven.apache.org/surefire/maven-surefire-plugin/ Goals Overview The Surefire Plugin has only ...

  4. iPhone 物理尺寸与分辨率

    //    iPhone  物理尺寸(pt:Point)   分辨率(px) //    4S        320*480(3.5英寸)     640*960 //    5,5c,5S   32 ...

  5. 在 Java 中使用 protobuf

    在 Java 中使用 protobuf 从 https://github.com/google/protobuf/releases 下载编译器,并设置环境变量. 创建java项目添加protobuf- ...

  6. Java中的I/O 线程 网络

    Java学习总结--I/O,线程,网络题目整理 I/O 1.有什么理由必须要用字符流? 答:处理字符数据的语法更方便.自动化字符编码 2.插入哪些代码可以让下面的代码正确编译? Console con ...

  7. linux cpu过高原因及代码定位

    1. top命令查看CPU和内存占用率 top回车,然后按1 发现进程PID 35163 CPU和内存占用率都很高 top - 06:13:47 up  5:31,  1 user,  load av ...

  8. (线段树) Count the Colors --ZOJ --1610

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F http://acm.zju.edu.cn/onli ...

  9. test命令详解

    test命令格式: test condition 通常,在if-then-else语句中,用[]代替,即[ condition ].注意:方括号两边都要用空格.   1.数值比较 ========== ...

  10. LeetCode138:Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...