1)操作页面元素之单选按钮:“性别”字段一般有俩个选项。如果不允许用户选择多个选项,可以使用表单元素的单选按钮对象。单选按钮用于一组互相排斥的值,也就是用户只能从选项表中选择一项。单选按钮中所有按钮共享同一个名称,所以浏览器知道将按钮组合在一起,通过选中其中一个按钮,其他按钮自动变为为选中状态。
package com.glp.selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestOption {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver",
"C:\\Driver\\chromedriver.exe");
driver = new ChromeDriver(); // 浏览器初始化
baseUrl = "file:///C:/Users/Lipin/workspace/guoliping_selenium/content/autotest.html";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@Test
public void testOptions() {
driver.get(baseUrl);
WebElement femaleRadioButton = driver
.findElement(By.cssSelector("#sexID2"));
if (!femaleRadioButton.isSelected()) {
femaleRadioButton.click();
}
Assert.assertTrue(femaleRadioButton.isSelected());
// Assert.assertEquals(femaleRadioButton.isSelected(), true);
}
@AfterMethod // 执行完测试用例关闭浏览器
public void tearDown() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
2)操作页面元素之多选按钮,Checkbox.下图为例:
我们来分析下,一般人的做法是会一个个定位元素,不管是用 id/还是CSSselector 还是 XPath。但是为了是代码更简洁,我把它写成了一个for循环。
package com.glp.selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestSelect {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver",
"C:\\Driver\\chromedriver.exe");
driver = new ChromeDriver(); // 浏览器初始化
baseUrl = "file:///C:/Users/Lipin/workspace/guoliping_selenium/content/autotest.html";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@Test
public void testOptions() {
driver.get(baseUrl);
for (int i = 1; i < 5; i++) {
WebElement JijieCheckBox = driver
.findElement(By.cssSelector("#u" + i));
if (!JijieCheckBox.isSelected()) {
JijieCheckBox.click();
}
Assert.assertTrue(JijieCheckBox.isSelected());
}
}
@AfterMethod // 执行完测试用例关闭浏览器
public void tearDown() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
-
幼儿园安全工作总结:https://www.nanss.com/gongzuo/3363.html 现在完成时的句子:https://www.nanss.com/xuexi/3788.html 月度总结:https://www.nanss.com/gongzuo/2984.html 喜欢一个人的表现:https://www.nanss.com/shenghuo/3881.html 清洁工作文:https://www.nanss.com/xuexi/2940.html 爱国征文:https://www.nanss.com/xuexi/2740.html 护理工作总结:https://www.nanss.com/gongzuo/3513.html 形势与政策心得体会:https://www.nanss.com/xuexi/3265.html 有诗意的名字:https://www.nanss.com/mingcheng/3610.html 一二三木头人作文300字:https://www.nanss.com/xuexi/2097.html 气候变化与健康为主题的征文:https://www.nanss.com/xuexi/2263.html 面对困难的励志的句子:https://www.nanss.com/yulu/2222.html 工作汇报:https://www.nanss.com/gongzuo/3138.html 思绪混乱:https://www.nanss.com/wenan/3536.html 事业越来越好的祝福语:https://www.nanss.com/gongzuo/3970.html 大学生入党思想汇报:https://www.nanss.com/xuexi/2622.html 酷的名字:https://www.nanss.com/mingcheng/3946.html 十岁成长仪式说说发朋友圈:https://www.nanss.com/wenan/2035.html 关于感谢的作文:https://www.nanss.com/xuexi/2220.html 生活录:https://www.nanss.com/yulu/3728.html 小学毕业感言:https://www.nanss.com/xuexi/3300.html 新员工转正申请:https://www.nanss.com/gongzuo/3533.html 我的乐园作文350字:https://www.nanss.com/xuexi/3705.html 纪律作风整顿心得体会:https://www.nanss.com/gongzuo/2104.html 祝女儿生日短句霸气:https://www.nanss.com/yulu/3824.html 闺蜜愚人节玩笑:https://www.nanss.com/shenghuo/2390.html 开心的网名:https://www.nanss.com/mingcheng/3779.html 处罚通知书:https://www.nanss.com/gongzuo/3384.html 桂花的作文:https://www.nanss.com/xuexi/2164.html 爱国故事:https://www.nanss.com/yuedu/2909.html(0) 回复 (0)
-
讨债公司/蓝月传奇辅助/蓝月辅助(0) 回复 (0)