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

相关文章

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.