判断元素是否激活:isEnabled()
测试用例:
1. 打开“UI自动化测试”主页2. 校验 submit 文本框为不可输入状态
代码实现:
@Testpublic void isEnabledTest() {driver.get("file:///C:/selenium_html/index.html");Boolean b = driver.findElement(By.name("buttonhtml")).isEnabled();Assert.assertFalse(b);}
判断元素是否展示:isDisplayed()
之前我们讲过如果一个元素没有显示在页面,那么我们去操作往往会出现错误,所以我们一个去判断一个元素是否显示状态的方法。
测试用例:
1. 打开百度首页2. 校验百度一下按钮已经展示
代码实现:
@Testpublic void isDisplayedTest() {driver.get("http://www.baidu.com");Boolean b = driver.findElement(By.id("su")).isDisplayed();Assert.assertTrue(b, "校验百度一下按钮是否显示");}
判断单选/复选框是否选取:isSelected()
测试用例:
1. 打开 “UI自动化测试”主页2. 校验 “Volvo”单选框已经选中
代码实现:
@Testpublic void idSelectTest() {driver.get("file:///C:/selenium_html/index.html");WebElement element = driver.findElement(By.xpath("//*[@id=\"radio\"]/input[1]"));element.click();Boolean b = element.isSelected();Assert.assertTrue(b);}
截图
测试用例:
1. 打开百度主页2. 截图
代码实现:
@Testpublic void screenShot() {driver.get("https://www.baidu.com");File screenShotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try {FileUtils.copyFile(screenShotFile, new File("./test.png"));} catch (IOException e) {e.printStackTrace();}}
执行JavaScript 脚本
自动化开展过程中,有时会遇到直接通过WebDriver提供的API并无法满足我们的要求,这时我们可能需要通过执行JavaScript 的方式来处理。
测试用例
1. 打开百度首页2. 通过JS方式,往查询框插入值
代码实现:
@Testpublic void exJS() throws InterruptedException {driver.get("http://www.baidu.com");JavascriptExecutor js = (JavascriptExecutor) driver;// 执行JS语句js.executeScript("document.getElementById(\"kw\").setAttribute(\"value\",\"执行JS\")");Thread.sleep(5000);}
