在这里集中了我们在做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自动化练手页面的更多相关文章

  1. 使用selenium的WebDriver和ChromeDriver实现UI自动化

    下载chromedriver chromedriver与chrome的对应关系表:http://blog.csdn.net/huilan_same/article/details/51896672 下 ...

  2. UI自动化--PageObjects(页面对象)

    核心的核心:减少了重复代码的数量,减少变更涉及面:做到如果UI发生更改,则只需在一个位置应用此修复程序. PageObject:将页面作为一个对象,进行封装,包括元素定位,封装获取各元素.操作的方法: ...

  3. 推荐:一个适合于Python新手的入门练手项目

    随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...

  4. 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍

    1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...

  5. Python入门、练手、视频资源汇总,拿走别客气!

    摘要:为方便朋友,重新整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. ...

  6. 微信小程序初体验,入门练手项目--通讯录,部署上线(二)

    接上一篇<微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器>:https://www.cnblogs.com/chengxs/p/9898670.html 开发微信小程序最尴尬 ...

  7. 微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器(一)

    内容: 一.前言 二.相关概念 三.开始工作 四.启动项目起来 五.项目结构 六.设计理念 七.路由 八.部署线上后端服务 同步交流学习社区: https://www.mwcxs.top/page/4 ...

  8. Xamarin入门,开发一个简单的练手APP

    之前周末用Xamarin练手做了个简单APP,没有啥逻辑基本就是个界面架子,MVVM的简单使用,还有Binding,Command的简单使用,还有一个稍微复杂点两个界面交互处理(子页面新增后关闭,父页 ...

  9. UI自动化,你值得拥有

    去年春节联欢晚会,为了那张“敬业福”,全家都卯足了劲儿“咻一咻”,连节目都顾不上看了.当时我就想,要是能自动化该多好,不停点击屏幕,屏幕不疼手还疼呢,何况还不好分心,生怕错过了“敬业福”.玩“咻一咻” ...

随机推荐

  1. 转 :scikit-learn的GBDT工具进行特征选取。

    http://blog.csdn.net/w5310335/article/details/48972587 使用GBDT选取特征 2015-03-31 本文介绍如何使用scikit-learn的GB ...

  2. JAVA-jar包下载地址

    spring mvc常用jar包 commons-logging:http://commons.apache.org/proper/commons-logging/download_logging.c ...

  3. wifidog 源码初分析(1)-转

    wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码. 在路由器上启动 wifidog 之后,wifid ...

  4. WordPress 获取指定分类ID的分类信息

    get_term:直接从数据库中获取分类信息get_the_category:使用post_id作为参数,先根据post_id查询对应的文章然后再返回对应的分类信息,如果没有文章信息则返回Null 之 ...

  5. Logistic 回归梯度上升优化函数

    In [183]:           def loadDataSet(): dataMat = [] labelMat = [] fr = open('testSet.txt') for line ...

  6. 编译安装openssl报错:POD document had syntax errors at /usr/bin/pod2man line 69. make: *** [install_docs]

    错误如下: cms.pod around line 457: Expected text after =item, not a number cms.pod around line 461: Expe ...

  7. JavaScript:如何获得 Private、Privileged、Public 和 Static 成员(属性和方法)【翻译+整理】

    本文内容 背景 把我们的对象放在一起 添加一个私有(Private)的属性 添加一个特权(Privileged)的方法 添加一个公共(Public)的属性和方法 添加一个静态(Static)的属性 我 ...

  8. MySQL查询当天、本周,本月,上一个月的数据

    QUARTER)); ; MONTH),'%Y-%m') select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDA ...

  9. angular5中使用全局变量

    创建全局变量ts文件,然后引入 创建globals.ts文件: export const base_path = "http://localhost/api/index.php/Home&q ...

  10. Python取得系统进程列表

    一.上代码 import psutil for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name ...