WebDriver基本操作入门及UI自动化练手页面

2024-06-23 22:48

本文主要是介绍WebDriver基本操作入门及UI自动化练手页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里集中了我们在做UI自动化时常见的一些控件操作。希望能对新手有帮助。

下载地址:http://files.cnblogs.com/zhangfei/demo.rar

复制代码
package com.test;import java.util.List;
import java.util.Set;import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;public class Demo {public WebDriver driver;public Demo() {// ProfilesIni allProfiles = new ProfilesIni();// FirefoxProfile profile = allProfiles.getProfile("default");// driver = new FirefoxDriver(profile);driver = new FirefoxDriver();}public void testBaidu() {WebDriver driver = new FirefoxDriver();driver.navigate().to("http://www.baidu.com");driver.quit();}public void testGoTo(String url) {driver.navigate().to(url);driver.manage().window().maximize();}public void testQuit() {// driver.close();
        driver.quit();}public void testInput(String value) {WebElement element = driver.findElement(By.id("user"));element.sendKeys(value);element.clear();element.sendKeys(value);String text = element.getAttribute("value");System.out.println(text);}public void testLink() {WebElement element = driver.findElement(By.className("baidu"));String href = element.getAttribute("href");System.out.println(href);String text = element.getText();System.out.println(text);element.click();driver.navigate().back();}public void testSelect(String value) {WebElement element = driver.findElement(By.name("select"));Select select = new Select(element);select.selectByValue(value);String text = select.getFirstSelectedOption().getText();System.out.println(text);}public void testRadioBox(int index) {List<WebElement> elements = driver.findElements(By.name("identity"));elements.get(index).click();boolean select = elements.get(index).isSelected();System.out.println(select);}public void testCheckBox(int index) {List<WebElement> elements = driver.findElements(By.xpath("//div[@id='checkbox']/input"));WebElement element = elements.get(index);element.click();boolean check = element.isSelected();System.out.println(check);}public void testButton() {WebElement element = driver.findElement(By.className("button"));element.click();boolean button = element.isEnabled();System.out.println(button);}public void testAlert() {WebElement element = driver.findElement(By.className("alert"));Actions action = new Actions(driver);action.click(element).perform();Alert alert = driver.switchTo().alert();String text = alert.getText();System.out.println(text);alert.accept();}public void testUpload(String filePath) {WebElement element = driver.findElement(By.id("load"));        element.sendKeys(filePath);        }public void testJavaScript(){JavascriptExecutor j = (JavascriptExecutor)driver;j.executeScript("alert('hellow rold!')");Alert alert = driver.switchTo().alert();String text = alert.getText();System.out.println(text);alert.accept();}public void testMultiWindow() {WebElement element = driver.findElement(By.className("open"));element.click();Set<String> handles = driver.getWindowHandles();String handle = driver.getWindowHandle();handles.remove(driver.getWindowHandle());WebDriver d = driver.switchTo().window(handles.iterator().next());        d.close();    driver.switchTo().window(handle);}public void testAction() {WebElement element = driver.findElement(By.className("over"));Actions action = new Actions(driver);action.moveToElement(element).perform();String text = driver.findElement(By.id("over")).getText();System.out.println(text);}public void testWait() {WebElement element = driver.findElement(By.className("wait"));element.click();
//        driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);boolean wait = new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {public Boolean apply(WebDriver d) {return d.findElement(By.className("red")).isDisplayed();}});System.out.println(wait);System.out.println(driver.findElement(By.className("red")).getText());}public static void main(String[] args) {Demo d = new Demo();d.testGoTo("http://ip/demo.html");d.testInput("hello");d.testLink();d.testRadioBox(2);d.testSelect("opel");d.testCheckBox(2);d.testButton();d.testUpload("c:\\test.txt");d.testAlert();d.testAction();d.testJavaScript();d.testWait();d.testQuit();}}
复制代码

这篇关于WebDriver基本操作入门及UI自动化练手页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088501

相关文章

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

JavaScript全屏,监听页面是否全屏

在JavaScript中,直接监听浏览器是否进入全屏模式并不直接支持,因为全屏API主要是关于请求和退出全屏模式的,而没有直接的监听器可以告知页面何时进入或退出全屏模式。但是,你可以通过在你的代码中跟踪全屏状态的改变来模拟这个功能。 以下是一个基本的示例,展示了如何使用全屏API来请求全屏模式,并在请求成功或失败时更新一个状态变量: javascriptlet isInFullscreen =

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

vue同页面多路由懒加载-及可能存在问题的解决方式

先上图,再解释 图一是多路由页面,图二是路由文件。从图一可以看出每个router-view对应的name都不一样。从图二可以看出层路由对应的组件加载方式要跟图一中的name相对应,并且图二的路由层在跟图一对应的页面中要加上components层,多一个s结尾,里面的的方法名就是图一路由的name值,里面还可以照样用懒加载的方式。 页面上其他的路由在路由文件中也跟图二是一样的写法。 附送可能存在

vue+elementui分页输入框回车与页面中@keyup.enter事件冲突解决

解决这个问题的思路只要判断事件源是哪个就好。el分页的回车触发事件是在按下时,抬起并不会再触发。而keyup.enter事件是在抬起时触发。 so,找不到分页的回车事件那就拿keyup.enter事件搞事情。只要判断这个抬起事件的$event中的锚点样式判断不等于分页特有的样式就可以了 @keyup.enter="allKeyup($event)" //页面上的//js中allKeyup(e

vue子路由回退后刷新页面方式

最近碰到一个小问题,页面中含有 <transition name="router-slid" mode="out-in"><router-view></router-view></transition> 作为子页面加载显示的地方。但是一般正常子路由通过 this.$router.go(-1) 返回到上一层原先的页面中。通过路由历史返回方式原本父页面想更新数据在created 跟mounted

LVGL快速入门笔记

目录 一、基础知识 1. 基础对象(lv_obj) 2. 基础对象的大小(size) 3. 基础对象的位置(position) 3.1 直接设置方式 3.2 参照父对象对齐 3.3 获取位置 4. 基础对象的盒子模型(border-box) 5. 基础对象的样式(styles) 5.1 样式的状态和部分 5.1.1 对象可以处于以下状态States的组合: 5.1.2 对象