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自动化练手页面的更多相关文章
- 使用selenium的WebDriver和ChromeDriver实现UI自动化
下载chromedriver chromedriver与chrome的对应关系表:http://blog.csdn.net/huilan_same/article/details/51896672 下 ...
- UI自动化--PageObjects(页面对象)
核心的核心:减少了重复代码的数量,减少变更涉及面:做到如果UI发生更改,则只需在一个位置应用此修复程序. PageObject:将页面作为一个对象,进行封装,包括元素定位,封装获取各元素.操作的方法: ...
- 推荐:一个适合于Python新手的入门练手项目
随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...
- 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...
- Python入门、练手、视频资源汇总,拿走别客气!
摘要:为方便朋友,重新整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. ...
- 微信小程序初体验,入门练手项目--通讯录,部署上线(二)
接上一篇<微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器>:https://www.cnblogs.com/chengxs/p/9898670.html 开发微信小程序最尴尬 ...
- 微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器(一)
内容: 一.前言 二.相关概念 三.开始工作 四.启动项目起来 五.项目结构 六.设计理念 七.路由 八.部署线上后端服务 同步交流学习社区: https://www.mwcxs.top/page/4 ...
- Xamarin入门,开发一个简单的练手APP
之前周末用Xamarin练手做了个简单APP,没有啥逻辑基本就是个界面架子,MVVM的简单使用,还有Binding,Command的简单使用,还有一个稍微复杂点两个界面交互处理(子页面新增后关闭,父页 ...
- UI自动化,你值得拥有
去年春节联欢晚会,为了那张“敬业福”,全家都卯足了劲儿“咻一咻”,连节目都顾不上看了.当时我就想,要是能自动化该多好,不停点击屏幕,屏幕不疼手还疼呢,何况还不好分心,生怕错过了“敬业福”.玩“咻一咻” ...
随机推荐
- PowerShell获取当前用户的权限
function Get-CurrentUserRoles { $SecurityPrinciple = New-Object -TypeName System.Security.Princi ...
- mac切换root
方法一: sudo -i sudo su或是su. 转自:http://blog.csdn.net/duanyipeng/article/details/8621967
- servlet种下cookie后如何携带cookie继续往下走
事情是这样的,今天我在应用1里面手动种下了一个cookie,然后它会发接着访问应用2,因为是我手动setCookie,所以它还没来得及携带cookie继续前往下一站,于是,apple pen,炸了. ...
- sonarqube 5.6
转载:https://www.jianshu.com/p/402987500bfd 一. 简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量.通过插件形式,可以支持包括java,C ...
- css调整图片位置布局
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE wml PUBLIC "-//WAP ...
- eclipse maven scm
http://my.oschina.net/OutOfMemory/blog/178512 1.安装eclipse的maven插件 m2e(http://wiki.eclipse.org/M2E_u ...
- android中使用SharedPreferences存储数据
使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...
- Unity3D for Mac 破解
每次版本更新又忘记怎么搞了.干脆记下来! 1安装Unity 2安装成功后打开Applecation>Unity>Unity(右击显示包内容)>contents>MacOS 3用 ...
- iOS DES ECB 模式加密
//iOS DES ECB 模式加密 #import <CommonCrypto/CommonCryptor.h> ,,,,,,,}; +(NSString *) encryptUseDE ...
- Android开发点滴 - 如何使按钮水平垂直居中且始终占据屏幕宽度一半
问题描述: 如何使按钮水平垂直居中且始终占据屏幕宽度一半 效果如下: 竖屏: 横屏: 解决方案: 使用线性布局,指定线性布局的总权重(weightSum)为1, 指定按钮的权重为其一半即0.5 布局代 ...