WebDriver API 实例详解(一)
一、访问某网页地址
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
方法1:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
driver.get("http://www.baidu.com");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
方法2:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
driver.navigate().to("http://www.baidu.com");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二、返回上一个访问的网页(模拟点击浏览器的后退功能)
被测试网页的网址:
http://www.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url);
driver.navigate().to(url2);
driver.navigate().back();//返回
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三、从上次访问网页前进到下一个网页(模拟单击浏览器的前进功能)
被测试网页的网址:
http://www.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url);
driver.navigate().to(url2);
driver.navigate().back();//返回
driver.navigate().forward();//前进
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
四、刷新当前网页
被测试网页的网址:
http://www.hao123.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url2);
driver.navigate().refresh();//刷新
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
五、操作浏览器窗口
被测试网页的网址:
http://www.hao123.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
//离屏幕左上角的位置
Point point = new Point(150, 150);
//浏览器窗口的长、宽
Dimension dimension = new Dimension(500, 500);
driver.manage().window().setPosition(point);
driver.manage().window().setSize(dimension);
driver.navigate().to(url2);
//窗口最大化
//driver.manage().window().maximize();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
六、获取页面的Title属性
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.baidu.com";
@Test
public void opentest() {
driver.navigate().to(url2);
String title = driver.getTitle();
System.out.println(title);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
七、获取当前页面的URL地址
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.baidu.com";
@Test
public void opentest() {
driver.navigate().to(url2);
String currenturl = driver.getCurrentUrl();
System.out.println(currenturl);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
八、在输入框中清除原有的文字内容
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="text" value="默认内容">文本框</input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement input = driver.findElement(By.id("text"));
//清除文本框中默认的内容
input.clear();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
九、在输入框中输入指定内容
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="text" value="默认内容">文本框</input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement input = driver.findElement(By.id("text"));
//清除文本框中默认的内容
input.clear();
//输入内容
input.sendKeys("输入指定内容");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十、单击按钮
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="text" value="默认内容">文本框</input>
<input type="button" id="button" value="改变文本框的文字"
onClick=document.getElementById("text").value="改变了!"></input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file2.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement button = driver.findElement(By.id("button"));
//点击
button.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
WebDriver API 实例详解(一)的更多相关文章
- WebDriver API 实例详解(四)
三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...
- WebDriver API 实例详解(三)
二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...
- WebDriver API 实例详解(二)
十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- Vue 实例详解与生命周期
Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...
- python+requests接口自动化测试框架实例详解
python+requests接口自动化测试框架实例详解 转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...
- 【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解
Eclipse插件开发6--eclipse在线翻译插件Translator开发实例详解 在上一篇文章中讲到了一个简单的eclipse插件开发实例,主要是对插件工程的基本创建步骤进行了讲解,这篇文章当中 ...
随机推荐
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
- Linux wc 命令
wc命令可以用来统计文件的行数 .单词数 .字符数,用法如下: [root@localhost ~]$ wc 1.txt # 统计文件的行数.单词数.字符数 2 4 24 1.txt [root@lo ...
- ssh通过密钥进行连接
sshd服务提供两种安全验证的方法: 基于口令的安全验证:经过验证帐号与密码即可登陆到远程主机. 基于密钥的安全验证:需要在本地生成"密钥对"后将公钥传送至服务端,进行公共密钥的比 ...
- NGUI屏幕自适应(转)
屏幕自适应 NGUI可以比较方便的实现屏幕自适应,但是它的官方教程里面针对这个问题没有详细的教程,所以可能在实现的时候会走比较多的弯路.以下是我在开发过程中找到的一个比较方便的实现方法. 主要组件 ...
- mac 安装oracle
http://www.oracle.com/technetwork/cn/database/10204macsoft-x86-64-087400-zhs.html
- Effective C++ —— 资源管理(三)
条款13 : 以对象管理资源 假设有如下代码: Investment* createInvestment(); //返回指针,指向Investment继承体系内的动态分配对象,调用者有责任删除它 vo ...
- php学习十二:其他魔术方法
__clone():克隆的时候会调用__clone方法: __cal:当类里面没有方法的时候会调用__call方法: __toString:当echo的时候会调用__toString方法: __aut ...
- 使用HttpClient以文件流的方式上传文件(非multipartFormData方式)
@Test public void testAdd() throws IOException { HttpPost post = new HttpPost("http://localhost ...
- AndroidStudio build.gradle 报错
Android Studio. I'm getting this kind of error during application run. Error:Execution failed for ta ...
- linux注意的一些地方
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行 #include <assert.h>void assert( int expr ...