import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Alert;
/*
*seleniumJavaScript弹窗处理
*
*/
class JavaScriptbounced {
//定义Webdriver
WebDriver driver;
//定义访问网址
String baseurl;
@BeforeEach
void setUp() throws Exception {
//谷歌浏览器的本地驱动
System.setProperty("webdriver.chrome.driver", "/Users/lisen/webselenium/selenium/chromedriver");
//new一个谷歌浏览器对象
driver =new ChromeDriver();
//定义访问网址,访问的是去哪网
baseurl="file:///Users/lisen/Downloads/PracticePage.html";
//打开网址过后,设置隐性等待,等待10秒
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//打开网址后,窗口最大化
driver.manage().window().maximize();
//打开网址,调用driver.get方法
driver.get(baseurl);
}
@Test
void test1() throws Exception {
//打印log信息
System.out.println("打开网址完成");
//点击去哪网登陆按钮,用id查找
driver.findElement(By.id("name")).sendKeys("list");
driver.findElement(By.id("alertbtn")).click();
Thread.sleep(3000);
//返回一个Alert(弹窗)对象。
Alert alert=driver.switchTo().alert();
//操作ok按钮
alert.accept();
System.out.println("点击确认按钮完成");
Thread.sleep(3000);
}
@Test
void Test2() throws Exception {
//点击去哪网登陆按钮,用id查找
driver.findElement(By.id("name")).sendKeys("list");
driver.findElement(By.id("confirmbtn")).click();
Thread.sleep(3000);
//返回一个Alert(弹窗)对象。
Alert alert=driver.switchTo().alert();
//操作ok按钮
alert.accept();
System.out.println("点击ok按钮完成");
//点击取消按钮
//alert.dismiss();
}
@AfterEach
void tearDown() throws Exception {
//设置等待时间,看清楚操作
Thread.sleep(2000);
//关闭浏览器
driver.quit();
}
}