import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
class RadioButtonsAndcheckboxes {
/*
* 单选框和复选框操作
*/
//引入Webdriver
WebDriver driver;
//访问网址
String baseurl;
@BeforeEach
void setUp() throws Exception {
//谷歌浏览器本地驱动
System.setProperty("webdriver.chrome.driver", "/Users/lisen/webselenium/selenium/chromedriver");
//初始化谷歌浏览器
driver=new ChromeDriver();
baseurl="file:///Users/lisen/Downloads/PracticePage.html";
//设置隐性等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//窗口最大化
driver.manage().window().maximize();
}
@Test
void test() throws Exception {
//访问网址
driver.get(baseurl);
//打印log
System.out.println("打开网址完成");
//查找多选元素列表框
WebElement element =driver.findElement(By.id("multiple-select-example"));
//查找Select属性的控件
Select sel = new Select(element);
//等待3秒
Thread.sleep(3000);
//用Value选中控件元素
sel.selectByValue("orange");
//用Value取消选中元素
sel.deselectByValue("orange");
//用角标选中控件元素
sel.selectByIndex(2);
//用角标取消选中控件元素
sel.deselectByIndex(2);getClass();
//用文本选中控件元素
sel.selectByVisibleText("苹果");
//用文本取消选中控件元素
sel.deselectByVisibleText("苹果");
//打印所有选中的选项
List<WebElement> selectopions=sel.getAllSelectedOptions();
for (WebElement option:selectopions) {
//文本返回所有的选中元素
System.out.println("打印所有元素"+option.getText());
}
//等待2秒钟
Thread.sleep(2000);
//取消所有选中按钮
sel.deselectAll();
}
@AfterEach
void tearDown() throws Exception {
//等待3秒
Thread.sleep(3000);
//关闭浏览器
driver.quit();
}
}