测试页面demo
页面代码
<html> <meta charset="UTF-8"> <body> <table cellspacing="0" cellpadding="10" border="1" width="1000"> <tr color="yellow"> <td>内容</td> <td>页面元素</td> </tr> <tr> <td>文本框</td> <td> <input type="text" name="name" value="John"> </td> </tr> <tr> <td>单选框</td> <td> <input type="radio" name="sex" value="male" checked>Male <input type="radio" name="sex" value="female" disabled>Female </td> </tr> <tr> <td>复选框</td> <td> <input type="checkbox" name="vehicle" value="Bike">bike <br> <input type="checkbox" name="vehicle" value="Car">car <br> <input type="checkbox" name="vehicle" value="Car2">Apple </td> </tr> <tr> <td>alert</td> <td><input type="button" onclick="alert('alert')" value="alert" name="alert"></td> </tr> <tr> <td>prompt</td> <td><input type="button" onclick="prompt('prompt')" value="prompt"></td> </tr> <tr> <td>confirm</td> <td><input type="button" onclick="confirm('confirm')" value="confirm"></td> </tr> <tr> <td>文件上传</td> <td><input type="file" name="attach"></td> </tr> <tr> <td>下拉选择select</td> <td> <select id="selector" style="100px;"> <option value ="苹果">苹果</option> <option value ="华为">华为</option> <option value="Dell">Dell</option> </select> </td> </tr> <tr> <td>select Links</td> <td> <select name="selectMenu" style="100px;"> <option value ="http://www.baidu.com">百度</option> <option value ="https://www.zhihu.com/signin?next=%2F">知乎</option> </select> </td> </tr> <tr> <td>超链接</td> <td> <a href="https://www.cnblogs.com/gaogo/" >my blog 小新人~</a> </td> </tr> </table> </body> </html>
一、selenium的8大定位方式
selenium的核心是定位WebElement
//1、通过ID定位
By.id("");
//2、通过name定位
By.name("");
//3、通过xpath定位
By.xpath("");
//4、通过className定位
By.className("");
//5、通过cssSelector定位
By.cssSelector("");
//6、通过链接文本定位
By.linkText("");
//7、通过链接文本模糊定位
By.partialLinkText("");
//8、通过tagName定位
By.tagName("");
文本输入框
通过定位得到webElement对象,操作输入框常用方法:
element.clear(); //清除文本框中原有内容
element.sendKeys("测试文本框输入"); //输入文本
单选框radio
radio页面元素的属性
同样,要操作元素,需要先通过定位得到webElement对象,操作radio常用方法:
element.isSelected(); //判断该值是否被选中
element.isEnabled(); //判断该值是否可选,是否有效,当radio对象设置了属性disabled,则webElement对象不可选,isEnabled()返回false,否则true。
element.getAttribute("name"); //获取对象属性是name的值,<input type="radio" name="sex" value="male" checked>Male,得到值是sex
element.getAttribute("value"); //获取对象属性是value的值,<input type="radio" name="sex" value="male" checked>Male,得到的值是male
复选框checkBox
类似于radio
//checkBox List<WebElement> checkElements = driver.findElements(By.xpath("//input[@type='checkbox']")); for (WebElement element:checkElements){ System.out.println("CheckBox对象是否被选中:" + element.isSelected()); System.out.println("checkBox对象是否可选择:" + element.isEnabled()); System.out.println("CheckBox对象的值:" + element.getAttribute("value")); element.click(); Thread.sleep(2000); System.out.println("CheckBox对象是否被选中:" + element.isSelected()); }
alert、prompt,confirm对话框
先要获取alert元素对象WebElement,webElement.click()调出alert对话框
Alert alert = driver.switchTo().alert(); //获取alert对象
alert.accept(); //确认,关闭对话框
alert.getText();获取alert的内容
alert.dismiss(); //取消
alert.sendKeys("输入的内容"); //prompt对话框可以输入内容
文件上传
通常是input框,可以使用sendKeys("上传文件的地址");
select下拉选择
WebElement selectElement = driver.findElement(By.id("selector")); Select select = new Select(selectElement); select.selectByIndex(0); //通过索引选择 Thread.sleep(1000); select.selectByValue("华为"); //通过value值选择 Thread.sleep(1000); select.selectByVisibleText("Dell"); //通过展示的文本选择
选择option的三种方法
- selectByIndex(int index) 通过index
- selectByVisibleText(String text) 通过匹配到的可见字符
- selectByValue(String value) 通过匹配到标签里的value
超链接
<a href="http://www.cnblogs.com/gaogo/" >my blog 小新人~</a>
链接的操作方式有很多种,我这里是执行了js
WebElement linkElement = driver.findElement(By.linkText("my blog 小新人~")); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; String js = "window.open('"+linkElement.getAttribute("href")+"')"; javascriptExecutor.executeScript(js);
String currentWindow = driver.getWindowHandle(); //当前窗口
driver.switchTo().window(s); //跳转到指定窗口
测试代码
package com.uiAutoTest; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.Assert; import org.testng.annotations.Test; import java.util.List; import java.util.Set; /** * @author Jessie * @date 2020/9/25 20:26 */ public class study { WebDriver driver; @Test public void test() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\driver\chromedriver.exe"); driver = new ChromeDriver(); driver.get("D:\document\demo.html"); //input输入框 WebElement element = driver.findElement(By.name("name")); element.clear(); element.sendKeys("测试文本框输入"); //radio单选框 WebElement radioElement1 = driver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td[2]/input[1]")); WebElement radioElement2 = driver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td[2]/input[2]")); Assert.assertTrue(radioElement1.isSelected()); Assert.assertTrue(radioElement1.isEnabled()); Assert.assertFalse(radioElement2.isSelected()); Assert.assertFalse(radioElement2.isEnabled()); radioElement2.click(); System.out.println("radioElement1的name属性值" + radioElement1.getAttribute("name")); //checkBox多选框 List<WebElement> checkElements = driver.findElements(By.xpath("//input[@type='checkbox']")); for (WebElement one:checkElements){ System.out.println("CheckBox对象是否被选中:" + one.isSelected()); System.out.println("checkBox对象是否可选择:" + one.isEnabled()); System.out.println("CheckBox对象的值:" + one.getAttribute("value")); one.click(); Thread.sleep(1000); System.out.println("CheckBox对象是否被选中:" + one.isSelected()); } //alert对话框 WebElement alertElement = driver.findElement(By.xpath("/html/body/table/tbody/tr[5]/td[2]/input")); alertElement.click(); Thread.sleep(1000); Alert alert = driver.switchTo().alert(); Assert.assertTrue(alert.getText().contains("alert")); alert.accept(); Thread.sleep(2000); //prompt对话框 WebElement promptElement = driver.findElement(By.xpath("/html/body/table/tbody/tr[6]/td[2]/input")); promptElement.click(); Thread.sleep(2000); Alert prompt = driver.switchTo().alert(); Assert.assertTrue(prompt.getText().contains("prompt")); prompt.sendKeys("测试prompt对话框"); Thread.sleep(2000); prompt.accept(); //confirm对话框 WebElement confirmElement = driver.findElement(By.xpath("/html/body/table/tbody/tr[7]/td[2]/input")); confirmElement.click(); Thread.sleep(1000); Alert confirm = driver.switchTo().alert(); Assert.assertTrue(confirm.getText().contains("confirm")); confirm.dismiss(); Thread.sleep(2000); //文件上传 WebElement fileElement = driver.findElement(By.xpath("//td/input[@type='file']")); fileElement.sendKeys("D:\document\demo.html"); Assert.assertTrue(fileElement.getAttribute("value").contains("demo.html")); //select下拉选择 WebElement selectElement = driver.findElement(By.id("selector")); Select select = new Select(selectElement); select.selectByIndex(0); //通过索引选择 Thread.sleep(1000); select.selectByValue("华为"); //通过value值选择 Thread.sleep(1000); select.selectByVisibleText("Dell"); //通过展示的文本选择 List<WebElement> list = select.getAllSelectedOptions(); for (WebElement e:list){ System.out.println(e.getAttribute("value")); } //超链接 String currentWindow = driver.getWindowHandle(); //当前窗口 System.out.println(currentWindow); WebElement linkElement = driver.findElement(By.linkText("my blog 小新人~")); JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; String href = linkElement.getAttribute("href"); String js = "window.open('"+linkElement.getAttribute("href")+"')"; javascriptExecutor.executeScript(js); Set<String> windowHandles = driver.getWindowHandles(); //获取所有窗口 for (String s :windowHandles){ WebDriver window = driver.switchTo().window(s); if (window.getCurrentUrl().equals(href)){ System.out.println(driver.getCurrentUrl()); break; } } driver.switchTo().window(currentWindow); System.out.println(driver.getCurrentUrl()); } }