在这里集中了我们在做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. 使用tensorflow的lstm网络进行时间序列预测

    https://blog.csdn.net/flying_sfeng/article/details/78852816 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...

  2. TensorFlow实战12:Bidirectional LSTM Classifier

    https://blog.csdn.net/felaim/article/details/70300362 1.双向递归神经网络简介 双向递归神经网络(Bidirectional Recurrent ...

  3. 转:Parameter Server 详解

    Parameter Server 详解   本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 更多相关博客请猛戳:http://blog.csdn.net/c ...

  4. Flask学习笔记

    1.路由用"/"结尾. 比如@app.route("/about/"),可以匹配/about和/about/,而@app.route("/about& ...

  5. iis 防火墙防止恶意ip攻击

    今天发现服务器里,一个IP不停的占用我的网络资源,然后在防火墙里配置,将其禁止访问,网络很快降了下来. 这个恶意的IP是 115.171.60.62

  6. chrome插件编写基本入门

    chrome插件编写基本入门  http://igeekbar.com/igeekbar/post/331.htm #精选JAVASCRIPTCHROME 作为一名程序猿,怎么能不会写chrome插件 ...

  7. 2014年10月底/终于/HTML5定稿……/技术从来不会成为发展的绝对瓶颈/反而商业成了无法逾越的鸿沟【转载+整理】

    原文地址 本文内容 一.HTML5 诞生 二.HTML5 第一阶段: Web 增强与打破垄断 三.HTML5 第二阶段: 移动互联网 四.HTML5 这回真的来了 五.颠覆原生 App 六.还有什么会 ...

  8. SpringBoot开发详解(六)-- 异常统一管理以及AOP的使用

    https://blog.csdn.net/qq_31001665/article/details/71357825

  9. url文件的格式

    [DEFAULT]BASEURL= [InternetShortcut]URL=WorkingDirectory=ShowCommand=IconIndex=IconFile=Modified=Hot ...

  10. Android 在闹钟开机时,如何解决开机动画没有播完就进入Launcher M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...