码迷,mamicode.com
首页 > 其他好文 > 详细

操作 警告框、提示框、确认框

时间:2015-06-04 13:39:42      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

以如下页面为例:

http://sislands.com/coin70/week1/dialogbox.htm

示例代码:

pachage com.learningselenium.normalwebdriver;

 

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.Alert;

 

public class testDialogs{

  WebDriver driver = new FirefoxDriver();

 

  @Before

  publid void setUp() throws Exception{

    driver.get("http://sislands.com/coin70/week1/dialogbox.htm");

  }

 

  @Test

  public void testAlertDialog() throws Exception{

    WebElement alertButton = driver.findElement(By.xpath("//input[@value = ‘alert‘]"));

 

    alertButton.click();

    Alert javascriptAlert = driver.switchTo().alert();

    System.out.println(javascriptAlert.getText());

    javascriptAlert.accept();

  }

 

  @Test

  public void testPromptDialog() throws Exception{

    WebElement promptButton= driver.findElement(By.xpath("//input[@value = ‘prompt‘]"));

 

    alertButton.click();

    Alert javascriptPrompt = driver.switchTo().alert();

    javascriptPrompt.sendKeys("This is a test of prompt");

    javascriptPrompt .accept();  //确认按钮

 

    System.out.println(javascriptPrompt.getText());

 

    javascriptPrompt = driver.switchTo().alert();

    javascriptPrompt .accept();

 

    promptButton.click();

    javascriptPrompt = driver.switchTo().alert();

    System.out.println(javascriptPrompt.getText());

    javascriptPrompt.dismiss();   // 取消按钮

    

    javascriptPrompt = driver.switchTo().alert();

    System.out.println(javascriptPrompt.getText());

    javascriptPrompt.accept();

  }

 

  @Test

  public void testConfirmDialog() throws Exception{

    WebElement confirmButton= driver.findElement(By.xpath("//input[@value = ‘confirm‘]"));

 

    confirmButton.click();

    Alert javascriptConfirm= driver.switchTo().alert();

    javascriptConfirm.accept();

 

    javascriptConfirm= driver.switchTo().alert();

    System.out.println(javascriptConfirm.getText());

    javascriptConfirm.accept();    //确认按钮

 

    confrimButton.click();

    javascriptConfirm= driver.switchTo().alert();

    System.out.println(javascriptConfirm.getText());

    javascriptConfirm.dismiss();    // 取消按钮

    

    javascriptPrompt = driver.switchTo().alert();

    System.out.println(javascriptConfirm.getText());

    javascriptConfirm.accept();

  }

 

  @After

  public void tearDown() throws Exception{

    driver.quit();

  }

}

 

操作 警告框、提示框、确认框

标签:

原文地址:http://www.cnblogs.com/feifeidxl/p/4551445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!