WebDriver学习笔记(八)windows弹窗(警告弹窗)处理

测试时会遇到三种警告窗口: alert confirm 以及prompt。三种警告窗口的处理都很容易。
先新建个测试页面,html代码如下:
alert.html


 Alert



        

        

        


1.alert 
     alert警告弹窗只有一个确定按钮
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertTest {
        @Test
        public void alertTest() throws InterruptedException {
               // 设置chromedriver的路径,根据你具体存放位置来设置路径
              System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
               // 启动Chrome浏览器
              WebDriver driver = new ChromeDriver();
               // get方式打开测试页面
              driver.get( "C:\\alert.html" );
               // 为了看效果等待3S
              Thread. sleep(3000);
               // 点击alert按钮
              driver.findElement(By. id( "alert")).click();
               // 为了看效果等待3S
              Thread. sleep(3000);
               //     选取警告弹窗
              Alert alert=driver.switchTo().alert();
               // 点击取消按钮
              alert.accept();
               // 为了看效果等待3S
              Thread. sleep(3000);
               // 结束测试
              driver.quit();
       }
}
2.confirm
     这是那种提示是否要某个操作的,点击确定(accept())就执行,取消(dismiss())则不做任何操作的警告窗。
     @Test
        public void confirmTest() throws InterruptedException{
               // 设置chromedriver的路径,根据你具体存放位置来设置路径
              System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
               // 启动Chrome浏览器
              WebDriver driver = new ChromeDriver();
               // get方式打开测试页面
              driver.get( "C:\\alert.html" );
               // 为了看效果等待3S
              Thread. sleep(3000);
               // 点击alert按钮
              driver.findElement(By. id( "confirm")).click();
               // 为了看效果等待3S
              Thread. sleep(3000);
               //     选取警告弹窗
              Alert alert=driver.switchTo().alert();
               // 输出弹窗内容getText()
              System. out .println(driver.switchTo().alert().getText());
               // 点击确定按钮
              alert.accept();
               // 点击取消按钮
//            alert.dismiss();
               // 为了看效果等待3S
              Thread. sleep(3000);
               // 结束测试
              driver.quit();
       }
}
3.prompt
             @Test
        public void promptTest() throws InterruptedException{
               // 设置chromedriver的路径,根据你具体存放位置来设置路径
              System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
               // 启动Chrome浏览器
              WebDriver driver = new ChromeDriver();
               // get方式打开测试页面
              driver.get( "C:\\alert.html" );
               // 为了看效果等待2S
              Thread. sleep(2000);
               // 点击alert按钮
              driver.findElement(By. id( "prompt")).click();
               // 为了看效果等待2S
              Thread. sleep(2000);
               //     选取警告弹窗
              Alert prompt=driver.switchTo().alert();
               // 输出弹窗内容
              prompt.sendKeys( "输入值。。。。" );
               // 为了看效果等待2S
              Thread. sleep(2000);
               // 点击确定按钮
              prompt.accept();
               // 为了看效果等待2S
              Thread. sleep(2000);
               // 结束测试
              driver.quit();
       } 

你可能感兴趣的:(selenium,2.0)