潍坊网站制作公司,黄页88怎么发信息质量高,婚庆公司网站的设计与实现,网店运营推广具体内容测试|Selenium之WebDriver常见API使用 文章目录 测试|Selenium之WebDriver常见API使用1.定位对象#xff08;findElement#xff09;css定位xpath定位css选择器语法#xff1a;xpath语法:校验结果 2.操作对象鼠标点击对象在对象上模拟按键输入clear清除对象输入的文本内容su…测试|Selenium之WebDriver常见API使用 文章目录 测试|Selenium之WebDriver常见API使用1.定位对象findElementcss定位xpath定位css选择器语法xpath语法:校验结果 2.操作对象鼠标点击对象在对象上模拟按键输入clear清除对象输入的文本内容submit提交text获取元素的文本信息getAttribute获取元素属性值 3.添加等待4.打印信息5.浏览器的操作浏览器前进刷新与后退浏览器滚动条浏览器页面最大化最小化、全屏设置大小关闭浏览器 6.键盘鼠标事件**键盘事件使用sendKeys方法**鼠标事件 常见功能实现案例一组元素的定位(findElements)多层框架中元素的定位switchTo().frame切换窗口截图层级定位下拉框处理两级处理弹窗处理alert上传文件的处理(sendKeys路径) 总结 1.定位对象findElement
对象的定位是UI自动化测试的核心webdriver提供了一系列的对象定位方法这里只说css定位和xpath定位。
打开浏览器进入百度首页进入百度搜索输入框输入
css定位
以类选择器为例 public class Main {public static void main(String[] args) {ChromeOptions optionsnew ChromeOptions();//允许所有请求options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com);//找到百度搜索输入框WebElement elementwebDriver.findElement(By.cssSelector(.s_ipt));//通过css选择器//输入软件测试element.sendKeys(软件测试);}
}xpath定位 public class Main {public static void main(String[] args) {ChromeOptions optionsnew ChromeOptions();//允许所有请求options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com);//找到百度搜索输入框
// WebElement elementwebDriver.findElement(By.cssSelector(.s_ipt));//通过css选择器WebElement elementwebDriver.findElement(By.xpath(//*[id\kw\]));//通过xpath//输入软件测试element.sendKeys(软件测试);}
}css选择器语法
id选择器“#id”
类选择器“.classname”
标签选择器直接标签名 “input”
后代选择器“父级选择器 自己选择器”
xpath语法:
绝对路径/html/head/title(不常用)
相对路径双斜杠开头
相对路径索引索引默认以1开头. eg.//form/span[2]/input百度一下相对路径属性值eg.//input[classs_ipt]//input[idsu]相对路径通配符:eg,//*[*su]相对路径文本匹配:eg,//a[text()新闻] 相较于xpath选择器css选择器定位元素效率更高
校验结果 public static void test01() throws InterruptedException {ChromeOptions optionsnew ChromeOptions();//允许所有请求options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com/);//找到百度搜索输入框WebElement elementwebDriver.findElement(By.cssSelector(.s_ipt));//通过css选择器
// WebElement elementwebDriver.findElement(By.xpath(//*[id\kw\]));//通过xpath//输入软件测试element.sendKeys(软件测试);//找到百度一下按钮//点击webDriver.findElement(By.cssSelector(#su)).click();sleep(3000);//强制等待3ms//校验boolean flagtrue;//1.找到搜索结果ListWebElement elementswebDriver.findElements(By.cssSelector(a em));sleep(10000);for (int i 0; i elements.size(); i) {System.out.println(elements.get(i).getText());//2.条件if(!elements.get(i).getText().contains(测试)){flagfalse;System.out.println(测试不通过);break;}}if(flag){System.out.println(测试通过);}}2.操作对象
鼠标点击对象 在对象上模拟按键输入 clear清除对象输入的文本内容
public static void test02() throws InterruptedException {ChromeOptions optionsnew ChromeOptions();options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com);//找到搜索框WebElement elementwebDriver.findElement(By.cssSelector(.s_ipt));//输入软件测试element.sendKeys(软件测试);sleep(3000);//点击搜索按钮webDriver.findElement(By.cssSelector(#su)).click();//删除内容element.clear();//在输入Vue框架element.sendKeys(Vue框架);sleep(3000);//再次点击webDriver.findElement(By.cssSelector(#su)).click();}submit提交
如果点击的元素放在form标签中此时使用submit实现的效果和click是一样的如下 如果点击的元素放在非form标签中会报错如下所示
private static void test03() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com);//找到搜索框webDriver.findElement(By.xpath(//*[id\s-top-left\]/a[1])).submit();//输入软件测试
}所以推荐使用click()
text获取元素的文本信息
getText
getAttribute获取元素属性值
getText获取不了 private static void test04() {ChromeOptions options new ChromeOptions();options.addArguments(-remote-allow-origns*);WebDriver webDriver new ChromeDriver(options);//进入百度首页webDriver.get(https://www.baidu.com);//获取元素属性值
// String button_valuewebDriver.findElement(By.cssSelector(#su)).getText();
// System.out.println(button_value); String button_valuewebDriver.findElement(By.cssSelector(#su)).getAttribute(value);if(button_value.equals(百度一下)){System.out.println(测试通过);}else{System.out.println(测试不通过);}}3.添加等待
1.sleep强制等待sleep(xxx);单位为ms1000ms1s
2.智能等待隐式等待(使用WebDriver对象的manage方法返回值的timeouts方法的返回值的implicitlywait方法)显示等待使用WebDriverWait对象的until方法
隐式等待等待所有的元素被定位到
显示等待等待一定的条件被定位到程序员自己设定
隐式等待
显示等待 private static void test07() {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);
//判断能否点击// 显式等待设置最长等待时间为10秒并等待元素可见WebDriverWait wait new WebDriverWait(webDriver, 10);
// wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(#su))).click();//能定位到wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(#s))).click();//定位不到就会有问题}eg:driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
隐式地等待并非一个固定的等待时间当脚本执行到某个元素定位时如果元素可以定位则继续执行如果元素定位不到则它以轮询的方式不断的判断元素是否被定位到。直到超出设置的时长
4.打印信息
打印title和url
private static void test06() {// 创建Chrome浏览器的WebDriver实例WebDriver webDriver new ChromeDriver();// 导航到目标网页webDriver.get(https://www.baidu.com/);String urlwebDriver.getCurrentUrl();String titlewebDriver.getTitle();if(url.equals(https://www.baidu.com/)title.equals(百度一下你就知道)){//这里可能因为一个/就出错System.out.println(测试通过);}else {System.out.println(url);System.out.println(title);System.out.println(测试不通过);}
}5.浏览器的操作
浏览器前进刷新与后退
使用的navigate
webDriver.navigate().back();
webDriver.navigate().refresh();
webDriver.navigate().forward();private static void test08() throws InterruptedException {//打开百度首页,强制等待3秒WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);sleep(3000);//搜索儿童节,强制等待3秒webDriver.findElement(By.cssSelector(#kw)).sendKeys(儿童节);//输入框的id名webDriver.findElement(By.cssSelector(#su)).click();sleep(3000);//浏览器后退webDriver.navigate().back();sleep(3000);//强制等待3秒,前进webDriver.navigate().refresh();webDriver.navigate().forward();sleep(3000);
}浏览器滚动条
如果自动化不符合预期大部分时候都是页面渲染的问题
((JavascriptExecutor)webDriver).executeScript(document.documentElement.scrollTop10000);浏览器页面最大化最小化、全屏设置大小
使用的manage的windows
webDriver.manage().window().maximize();
sleep(3000);
webDriver.manage().window().fullscreen();
sleep(3000);
webDriver.manage().window().setSize(new Dimension(600,1000));关闭浏览器
有两种方式webDriver.quit();webDriver.close(); private static void test11() throws InterruptedException {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);webDriver.findElement(By.cssSelector(#s-top-left a:nth-child(1))).click();sleep(4000);
// webDriver.quit();webDriver.close();}两者的区别☆☆☆
quit是关闭了整个浏览器close是关闭了上一级页面quit会清空缓存cookieclose不会清空缓存
6.键盘鼠标事件
键盘事件使用sendKeys方法
功能键的选择
通过send_keys()调用按键 sendkeys(Keys.TAB) # TAB sendkeys(Keys.ENTER) # 回车 sendkeys(Keys.SPACE) #空格键 sendkeys(Keys.ESCAPE) #回退键Esc
组合键sendKeys(Keys.xxx,“xx”)…
private static void test09() throws InterruptedException {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);webDriver.findElement(By.cssSelector(#kw)).sendKeys(儿童节);//输入框的id名//ctrlawebDriver.findElement(By.cssSelector(#kw)).sendKeys(Keys.CONTROL,A);//输入框的id名sleep(3000);//ctrxwebDriver.findElement(By.cssSelector(#kw)).sendKeys(Keys.CONTROL,X);//输入框的id名sleep(3000);//ctrlvwebDriver.findElement(By.cssSelector(#kw)).sendKeys(Keys.CONTROL,V);//输入框的id名sleep(3000);}信息的输入:“xxx”直接加内容
鼠标事件
contextClick() 右击doubleClick() 双击dragAndDrop() 拖动moveToElement() 移动 private static void test10() throws InterruptedException {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);webDriver.findElement(By.cssSelector(#kw)).sendKeys(图片);webDriver.findElement(By.cssSelector(#su)).click();sleep(3000);//找到图片按钮WebElement webElement webDriver.findElement(By.xpath(//*[id\kw\]));Actions actionsnew Actions(webDriver);sleep(10000);actions.moveToElement(webElement).contextClick().perform();
}常见功能实现案例
一组元素的定位(findElements)
场景类似调查问卷的多选问题执行代码满足条件的所有选项就都选择了 多层框架中元素的定位switchTo().frame 有可能嵌套的不是框架而是窗口还有针对窗口的方法switchTo().window 用法与switchTo.frame 相同.
切换窗口
private static void test12() throws InterruptedException {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);webDriver.findElement(By.cssSelector(#s-top-left a:nth-child(1))).click();sleep(3000);// 通过getWindowHandles获取所有的窗口句柄// 通过getWindowHandle获取的get打开的页面窗口句柄System.out.println(webDriver.getWindowHandle());SetString handles webDriver.getWindowHandles();String target_handle ;for(String handle:handles) {target_handle handle;}webDriver.switchTo().window(target_handle);sleep(3000);webDriver.findElement(By.cssSelector(#ww)).sendKeys(新闻联播);webDriver.findElement(By.cssSelector(#s_btn_wr)).click();}截图
使用((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);这个方法并使用FileUtils工具类的copyFile方法复制到硬盘上。
private static void test13() throws InterruptedException, IOException {WebDriver webDriver new ChromeDriver();webDriver.get(https://www.baidu.com/);webDriver.findElement(By.cssSelector(#kw)).sendKeys(软件测试);webDriver.findElement(By.cssSelector(#su)).click();sleep(3000);File file ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(file, new File(F://typora插图//20230731jietu.png));
}这里需要引入相关依赖common-io
dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.4/version
/dependency层级定位
有时候我们需要定位的元素没有直接在页面展示而是需要对页面的元素经过一系列操作之后才展示出来这个时候我们就需要一层层去定位.
没有直接在页面中展示可能需要一些操作才能定位到
定位思路与多层框架/窗口定位思路一致。
下拉框处理两级处理
场景类似选择收货地址的省市县…
下拉框是我们最常见的一种页面元素对于一般的元素我们只需要一次就定位但下拉框里的内容需要进行两次定位先定位到下拉框定位到下拉框进行操作后再定位到下拉框里的选项。 这里除了可以通过value值进行定位还可以通过index下标默认从0开始定位等等
弹窗处理alert
页面中有一个按钮点击按钮会有弹窗弹窗中有对话框对于输入信息的处理 上传文件的处理(sendKeys路径)
上传文件一般要打开一个本地串口从窗口选择本地文件添加。
在selenium webdriver中只需要定位上传按钮通过sendKeys添加本地文件路径即可绝对路径和相对路径均可关键是上传的文件存在。 总结