Selenium2 操作Web常见对话框

一、Web中三种常见对话框:alert、prompt、confirm


二、在做web自动化测试中需要经常和对话框交互,这里就利用Selenium Webdriver 来操作对话框,废话少说,贴上代码(eclipse中的Junit写法):

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)//设置Test的执行顺序,这里表示按首字母顺序执行
public class TestDialogs {
	
	WebDriver driver;
	
	@Before
	public void setUp(){
		System.setProperty("webdriver.chrome.driver", "/Learning/Selenium/ChromeDriver/chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://sislands.com/coin70/week1/dialogbox.htm");//打开浏览器并转到对于地址
	}
	
	@Test
	public void testAlert() throws InterruptedException{
		WebElement alertButton = driver.findElement(By.xpath("/html/body/div/center/table/tbody/tr/td/form[1]/p/input"));
		if(alertButton.isDisplayed()){
			alertButton.click();
			Thread.sleep(2000);//必须设置等待时间,否则后面的对话框对象来不及获取
			
			Alert jsAlert = driver.switchTo().alert();//获取对话框对象
			System.out.println(jsAlert.getText());//打印对话框文本内容
			jsAlert.accept();//点击确认按钮
		} else {
			System.out.println("Can not find this button.");
		}
	}
	/**
	 * @date 2015-10-18
	 * @author Alan_y
	 */
	
	@Test
	public void testPrompt() throws InterruptedException{
		WebElement promptButton = driver.findElement(By.xpath("//input[@value='prompt']"));
		///html/body/div/center/table/tbody/tr/td/form[2]/p/input
		if(promptButton.isDisplayed()){
			promptButton.click();
			Thread.sleep(2000);
			Alert jsPrompt = driver.switchTo().alert();
			System.out.println(jsPrompt.getText());
			jsPrompt.sendKeys("Blue");
			jsPrompt.accept();
			Thread.sleep(2000);
			
			//new dialog, need rename
			jsPrompt = driver.switchTo().alert();
			System.out.println(jsPrompt.getText());
			jsPrompt.accept();
			
			//test cancel
			promptButton.click();
			Thread.sleep(2000);
			jsPrompt = driver.switchTo().alert();
			System.out.println(jsPrompt.getText());
			jsPrompt.dismiss();//点击取消按钮
			
		} else {
			System.out.println("Can not find this button.");
		}
	}
	
	@Test
	public void testComfirm() throws InterruptedException{
		WebElement confirmButton = driver.findElement(By.xpath("//input[@value='confirm']"));
		///html/body/div/center/table/tbody/tr/td/form[3]/p/input
		if(confirmButton.isDisplayed()){
			confirmButton.click();
			
			Thread.sleep(2000);
			Alert jsConfirm = driver.switchTo().alert();
			System.out.println(jsConfirm.getText());
			jsConfirm.accept();
			Thread.sleep(2000);
			//new dialog
			jsConfirm = driver.switchTo().alert();
			System.out.println(jsConfirm.getText());
			jsConfirm.accept();
			
			//test cancel
			confirmButton.click();
			Thread.sleep(2000);
			jsConfirm = driver.switchTo().alert();
			System.out.println(jsConfirm.getText());
			jsConfirm.dismiss();
			Thread.sleep(2000);
			
			//new dialog
			jsConfirm = driver.switchTo().alert();
			System.out.println(jsConfirm.getText());
			jsConfirm.accept();		
		} else {
			System.out.println("Can not find this button.");
		}
	}
	
	@After
	public void tearDown(){
		driver.quit();//关闭浏览器
		System.out.println("----end----");
	}
	
}

三、运行打印结果:

Starting ChromeDriver (v2.7.236900) on port 18063

This is an alert!!

----end----

Starting ChromeDriver (v2.7.236900) on port 25770

Confirm Test: Continue?

Your response was OK!

Confirm Test: Continue?

Your response was Cancel!

----end----

Starting ChromeDriver (v2.7.236900) on port 24391

What is your favorite color?

Your favorite color is: Blue

What is your favorite color?

----end----


本文出自 “Alan_Y (Upspringing)” 博客,转载请与作者联系!

你可能感兴趣的:(对话框,selenium;)