当前位置: 首页> 娱乐> 八卦 > UI界面自动化测试-Selenium

UI界面自动化测试-Selenium

时间:2025/7/12 13:05:46来源:https://blog.csdn.net/paperjie/article/details/140851398 浏览次数:0次

Selenium工作原理

image.png
image.png

SeleniumAPI

定位元素

image.png

Selenium操作对象

**send_keys 在对象上模拟按键输入 **
image.png
clear 清除对象输入的文本内容 **
image.png
click 点击对象(无限制)
image.png
submit 提交(用于form表单) **
image.png
getText() 用于获取元素的文本信息
image.png
g
etAttribute() 用于获取属性的值

image.png
quit 关闭游览器(quit关闭会将token cookie等删除 推荐)
close 关闭游览器(不推荐)
image.png
隐式等待 selenium.webdriver.remote.webdriver.implicitly_wait(time_to_wait)
image.png
使用案例:

public static void main(String[] args) throws InterruptedException {//创建一个游览器驱动WebDriver webDriver = new ChromeDriver();//打开百度网页webDriver.get("https://www.baidu.com");//找到搜索框WebElement cearch_input = webDriver.findElement(By.cssSelector("#kw"));//输入软件测试cearch_input.sendKeys("软件测试");//清空搜索框中内容cearch_input.clear();//输入前端cearch_input.sendKeys("前端");//找到百度一下,点击WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));baidu_button.click();//隐式等待webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显示等待//WebDriverWait wait = new WebDriverWait(webDriver, 3);//wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));//查看是否有前端相关内容List<WebElement> search_result = webDriver.findElements(By.xpath("//font[@color=\"#CC0000\"]"));for (int i = 0; i < search_result.size(); i++) {if(search_result.get(i).getText().equals("前端")) {System.out.println("成功");}else{System.out.println("失败");}}//关闭游览器 quit关闭会将token cookie等删除 推荐webDriver.quit();}

打印信息

获取title getTitle()
获取url getCurrentUrl()
image.png

游览器操作

游览器最大化 webDriver.manage.windows.maximize()
image.png
设置游览器大小 webDriver.manage().window().setSize(new Dimension(x, y))
image.png
游览器前进后退 webDriver.navigate().back() / forward()
image.png
操作游览器滚动 ((JavascriptExecutor)webDriver).executeScrpt(“document.documentElement.scrollTop=x”)
image.png

键盘事件

键盘按键用法和键盘组合用法:
image.png

鼠标事件

移动和右键:

public static void main(String[] args) {WebDriver webDriver = new ChromeDriver();webDriver.get("https://baidu.com/");//先创建一个ActionsActions actions = new Actions(webDriver);WebElement element = webDriver.findElement(By.xpath("//a[@target=\"_blank\"]"));//鼠标移动到这个按钮上且右键actions.moveToElement(element).contextClick().perform();}

特殊操作

切换窗口

image.png

//切换窗口句柄public static void main10(String[] args) throws InterruptedException {WebDriver webDriver = new ChromeDriver();webDriver.get("https://baidu.com/");webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();sleep(3000);//获取当前窗口句柄String cur_handle = webDriver.getWindowHandle();System.out.println(cur_handle);//获取游览器全部窗口的句柄Set<String> all_handle = webDriver.getWindowHandles();for (String str : all_handle) {if (!str.equals(cur_handle)) {//将句柄切换到当前窗口句柄webDriver.switchTo().window(str);}}webDriver.findElement(By.cssSelector("#ww")).sendKeys("测试");webDriver.findElement(By.cssSelector("#s_btn_wr")).click();sleep(3000);List<WebElement> webElements = webDriver.findElements(By.cssSelector("#\\32  > div > h3 > a > em"));System.out.println(webElements.toString());for (int i = 0; i < webElements.size(); i++) {if(webElements.get(i).getText().equals("测试")) {System.out.println("测试成功");} else {System.out.println("测试失败");}}

截图

引入依赖:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.16.0</version>
</dependency>

image.png

public static void main(String[] args) throws IOException {WebDriver webDriver = new ChromeDriver();webDriver.get("https://baidu.com/");//截图 把截下来的图放到File临时变量中File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);//把File保存在磁盘中FileUtils.copyFile(file, new File("D:/测试.png"));
}

选择一组元素

public static void main(String[] args) {WebDriver webDriver = new ChromeDriver();webDriver.get("");List<WebElement> webElements = webDriver.findElements(By.cssSelector(""));for(int i = 0; i < webElements.size(); i++) {if (webElements.get(i).getAttribute("").equals("")) {webElements.get(i).click();}}}

定位iframe下标签

image.png

 private static void Page02() throws InterruptedException {WebDriver webDriver = new ChromeDriver();webDriver.get("http://localhost:63342/Test01/Page/test02.html?_ijt=hsrkct8hrvk081crvik4rmae0k&_ij_reload=RELOAD_ON_SAVE");webDriver.switchTo().frame("f1");sleep(3000);webDriver.findElement(By.cssSelector("body > div > div > a")).click();}

定位下拉框选项

image.png

//定位下拉框选项public static void main3(String[] args) throws InterruptedException {WebDriver webDriver = new ChromeDriver();webDriver.get("http://localhost:63342/measurement/page/a.html?_ijt=fpqtishuhhavpgqi7lpf6mti4p&_ij_reload=RELOAD_ON_SAVE");WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));//创建一个Select对象Select select = new Select(webElement);//第一种方式 通过序号选中选项, 下标从0开始select.selectByIndex(2);sleep(3000);select.selectByValue("12.51");}

定位弹窗

image.png

//定位弹窗public static void main4(String[] args) throws InterruptedException {WebDriver webDriver = new ChromeDriver();webDriver.get("file:///D:/code/java-ee/code/measurement/src/main/java/page/b.html");webDriver.findElement(By.cssSelector("body > button")).click();//弹框里输入啊哈哈哈哈哈哈webDriver.switchTo().alert().sendKeys("啊哈哈哈哈哈哈");sleep(3000);//弹框确定webDriver.switchTo().alert().accept();/*弹框取消webDriver.switchTo().alert().dismiss();*/String txt = webDriver.findElement(By.cssSelector("body > div")).getText();if(txt.equals("啊哈哈哈哈哈哈")) {System.out.println("测试成功");}else {System.out.println("测试失败");}}

上传文件

image.png

//上传文件public static void main(String[] args) throws InterruptedException {WebDriver webDriver = new ChromeDriver();webDriver.get("file:///D:/code/java-ee/code/measurement/src/main/java/page/c.html");sleep(3000);//上传文件webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("D:\\brower\\网络原理2.png");}
关键字:UI界面自动化测试-Selenium

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: