Selenium(6)
一、定位页面元素
1、高级定位:层级定位
思路:先定位到祖先节点,在定位该祖先节点范围内的子节点
2、高级定位:Xpath定位(重点)
(1)Xpath定位:Xpath就是一个表达式,表示元素的路径,在XML文档中通过描述节点的路径来查找节点的技术,因为XML和HTML语法相似,所以Xpath也可以用在HTML文档中
(2)Xpath表达式有两种方式:
a.绝对路径:完整的路径,不建议使用
b.相对路径:相对于某个节点的路径,可维护性好
(3)获取Xpath表达式
a.打开被测系统,显示出要定位的元素
b.F12快捷键显示Firebug面板,切换到FirePath选项卡
c.使用第二个按钮"点击查看页面中的元素"
d.在被测系统点击要获取xpath的元素
e.在firepath的xpath表达式文本框中复制xpath表达式
说明:
a.Firebug:是火狐浏览器的一个插件,用于查看页面源代码,便于定位到关心的页面元素的源代码
b.Firepath:是Firebug的插件,用于查看和校验xpath表达式
练习1:使用xpath定位方式点击"登录"和"查看购物车"
二、查找页面元素的方法
1、findElement方法
(1)作用:获得符合定位条件的第一个元素
(2)返回值:WebElement页面元素
(3)语法:
WebElement we = driver.findElement(By.xxx("xxx"))we.click();
2、findElements方法
(1)作用:获取符合定位条件的所有页面元素
(2)返回值:由多个WebElement组成的List列表
(3)语法:
List<WebElement> allWe = driver.findElements(By.xxx("xxx"));
练习2:获取所有商品的个数
a.输入"手机"
b.点击"搜索"按钮
c.获取所有商品的个数
三、常用的对象以及常用的方法
1、WebDriver对象
(1)get方法
a.作用:获取指定URL的网页,在浏览器中打开指定的url对应的网页
b.语法:driver.get("url");
(2)quit方法:
a.作用:退出浏览器
b.语法:driver.quit();
(3)getTitle方法
a.作用:获取当前网页的标题
b.语法:String t = driver.getTitle();
2、WebElement对象:页面元素
(1)click方法:
a.单击页面元素,一般操作按钮、链接、图片
b.语法:we.click();
如:driver.findElement(By.name("submit")).click();
(2)sendKeys方法:
a.输入数据到指定的页面元素中
b.语法:we.sendKeys("testing");
如:driver.findElement(By.name("username")).sendKeys("testing");
(3)clear方法:
a.清空指定的页面元素
b.语法:we.clear();
如:driver.findElement(By.name("username")).clear();
(4)getText方法:
a.获取指定页面元素的文本
b.语法:String text = we.getText();
练习3:获取"欢迎光临本店"文本,对它进行验证
(5)getAttribute方法
a.获取指定页面元素的指定属性的值
b.语法:String v = we.getAttribute("属性名称");
练习4:
练习5:点击Ecshop首页的"注册"按钮
练习6:向Ecshop用户名输入框中输入testing
a.获取到页面中所有的输入框元素
b.在所有输入框元素遍历出用户名输入框
c.遍历出后输入testing
(6)isSelected方法
a.获得复选框或单选按钮是否处于被选中的状态
b.语法:boolean s=we.isSelected();
3、Alert对象:提示框
(1)封装Alert对象(切换到提示框,对提示框进行操作)
Alert a = driver.switchTo().alert();
(2)getText方法
a.获取提示框中的文本信息
b.语法:String t = a.getText();
(3)accept方法:
a.在提示框中点击"确认"按钮
b.语法:a.accept();
(4)dismiss方法:
a.在提示框中点击"取消"按钮
b.语法:a.dismiss();
练习7:
a.不输入任何内容点击"立即登陆"按钮,验证提示信息框是否出现和提示信息是否正确
b.输入用户名,点击"立即登陆"按钮,验证提示信息框是否出现和提示信息是否正确
4、Select对象:下拉框元素
(1)封装Select对象
Select s = new Select(driver.findElement(By.XX("XXX")));
(2)selectByVisibleText方法
a.通过选项的可见文本来选择
b.语法:s.selectByVisibleText("手机类型")
(3)selectByIndex方法
a.通过选项的下标选择选项,下标从0开始
b.语法:s.selectbyIndex(5);
(4)selectByValue方法
a.通过选项值来选择,选项值是指option标签元素中的value属性的值,是一个字符串
b.s.selectByValue("5");
(5)getFirstSelectedOption方法
a.获取选中的下拉框选项
b.语法:WebElement we = s.getFirestSelectedOption();
(6)getOptions方法
a.获取所有的下拉框选项
b.语法:List<WebElement> all = s.getOptions();
练习8:输入关键字a,点击搜索按钮,在搜索结果右上角的排序下拉框中进行操作
a.断言默认选项是否为"按上架时间排序"getFirestSelectedOption
b.选择"按价格排序"selectByVisibleText
c.选择包含"更新时间"的选项getOptions
Junit代码 package com.day06.www; import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select; public class TestECshop {
private WebDriver driver;
private String baseUrl;
//如果要点击"取消"按钮,就把true改成false
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer(); @Before
public void setUp() throws Exception { driver = new FirefoxDriver();
baseUrl = "http://localhost/";
//打开被测系统
driver.get(baseUrl+"/ws/ecshop/upload/index.php"); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} @Test
public void testUntitled() throws Exception {
//driver.get(baseUrl + "/ws/ecshop/upload/index.php");
//使用层级定位方式定位"注册"按钮,无法定位
//使用Xpath
driver.findElement(By.
xpath(".//*[@id='ECS_MEMBERZONE']/a[2]/img")).
click(); Thread.sleep(5000);
//driver.findElement(By.cssSelector("#ECS_MEMBERZONE > a > img")).click();
//使用层级定位方式定位"登录"按钮
//driver.findElement(By.id("ECS_MEMBERZONE")).
//findElement(By.tagName("img")).click();
//使用xpath登录"登录"按钮
driver.findElement(By.
xpath(".//*[@id='ECS_MEMBERZONE']/a[1]/img")).
click(); WebElement uname =
driver.findElement(By.name("username"));
uname.clear();
uname.sendKeys("testing");
//driver.findElement(By.name("username")).clear();
//driver.findElement(By.name("username")).sendKeys("testing");
WebElement passwd =
driver.findElement(By.name("password"));
passwd.clear();
passwd.sendKeys("123456"); //driver.findElement(By.name("password")).clear();
//driver.findElement(By.name("password")).sendKeys("123456");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("退出")).click();
//driver.findElement(By.linkText("查看购物车")).click();
//使用层级定位方式定位"查看购物车"
//driver.findElement(By.id("topNav")).
//findElement(By.tagName("a")).click();
//使用xpath定位方式定位"查看购物车"
driver.findElement(By.
xpath(".//*[@id='topNav']/a[1]")).
click(); Thread.sleep(5000);
} @Test
public void testSearch(){
//打开被测系统
//driver.get(baseUrl+"/ws/ecshop/upload/index.php");
//在搜索输入框中输入"手机"
driver.findElement(By.id("keyword")).
sendKeys("手机");
//点击"搜索"按钮
driver.findElement(By.name("imageField")).
click();
//获取商品的个数
List<WebElement> allItem =
driver.findElements(By.
className("goodsItem"));
System.out.println(allItem.size());
} @Test
public void testWebDriver(){
//get方法
// //http://localhost/ws/ecshop/upload/index.php
//driver.get(baseUrl+"ws/ecshop/upload/index.php");
//getTitle
String t = driver.getTitle();
assertEquals("ECSHOP演示站 - Powered by ECShop",t);
} @Test
public void testGetText() throws InterruptedException{
//打开被测系统
//driver.get(baseUrl+"/ws/ecshop/upload/index.php");
//等待一会儿
Thread.sleep(3000);
//获取页面中"欢迎光临本店"文本
String t;
t = driver.findElement(By.
id("ECS_MEMBERZONE")).
getText();
// System.out.println(t);
//验证
//assertEquals("欢迎光临本店",t);
assertTrue(t.contains("欢迎光临本店")); } @Test
public void testGetAttribute() throws InterruptedException{
//点击"登录"按钮
driver.findElement(By.
xpath(".//*[@id='ECS_MEMBERZONE']/a[1]/img")).
click();
//输入用户名:testing
driver.findElement(By.
name("username")).
sendKeys("testing");
//使用getAttribute获取testing放在uv
String uv;
uv=driver.findElement(By.
name("username")).
getAttribute("value");
//输入密码:123456
driver.findElement(By.
name("password")).
sendKeys("123456"); //点击"立即登陆"按钮
driver.findElement(By.
name("submit")).
click();
Thread.sleep(5000);
//获取登录后页面显示的testing放在tv
String tv;
tv = driver.findElement(By.
className("f4_b")).
getText();
//验证tv是否等于uv
assertTrue(tv.contains(uv));
//点击"退出"按钮
driver.findElement(By.
linkText("退出")).click(); } @Test
public void testClickReg() throws InterruptedException{
//该方法实现点击Ecshop注册按钮 //1.使用层级定位找到符合条件的所有img对象
List<WebElement> imgs = driver.
findElement(By.id("ECS_MEMBERZONE")).
findElements(By.tagName("img")); //2.从imgs中进行遍历,判断哪个元素的src属性包含reg
//遍历:for
//判断:if
//获取src属性:getAttribute方法
//包含:contains方法
//for(int i;i>10;i++){
//}
for(WebElement img:imgs){
if(img.getAttribute("src").contains("reg")){
img.click();
Thread.sleep(5000);
break;
}
} } @Test
public void testInput(){
//点击"登录"按钮
driver.findElement(By.
xpath(".//*[@id='ECS_MEMBERZONE']/a[1]/img")).
click();
//获取Input对象
List<WebElement> inputs =
driver.findElements(By.
tagName("input"));
//找到用户名输入框,输入testing
for(WebElement input:inputs){
if(input.getAttribute("name").
contains("username")){
input.sendKeys("testing");
break;
}
}
} @Test
public void testIsSelected() throws InterruptedException{
//点击"诺基亚N85"
driver.findElement(By.
linkText("诺基亚N85")).
click();
//判断黑色单选按钮是否选中
assertTrue(driver.findElement(By.
id("spec_value_163")).
isSelected());
//判断蓝牙耳机复选框是否选中,未选中,就去选中
WebElement ly = driver.findElement(By.
id("spec_value_158"));
if(!ly.isSelected()){
ly.click();
}
//判断数据线复选框是否选中,未选中,就去选中
WebElement sjx = driver.findElement(By.
id("spec_value_159"));
if(!sjx.isSelected()){
sjx.click();
}
//判断线控耳机复选框是否选中,未选中,就去选中
WebElement xkej = driver.findElement(By.
id("spec_value_157"));
if(!xkej.isSelected()){
xkej.click();
}
Thread.sleep(5000);
}
@Test
public void testAlert() throws InterruptedException{
//点击"搜索"按钮
driver.findElement(By.
className("go")).click();
Thread.sleep(7000);
//判断提示框是否出现
assertTrue(isAlertPresent());
//判断提示信息是否为:请输入搜索关键词
assertTrue(closeAlertAndGetItsText().
contains("请输入搜索关键词"));
//关闭提示框,点击确认
closeAlertAndGetItsText();//默认点击确认按钮 /*
//切换鼠标到提示框
Alert a = driver.switchTo().alert(); //断言弹框信息是否为"请输入搜索关键词"
assertTrue(a.getText().
contains("请输入搜索关键词"));
//点击弹框"确认"按钮
a.accept();//点击确认按钮
//a.dismiss();//点击取消按钮
*/
} @After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
//判断元素是否出现
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
} //判断提示框是否出现
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
} //关闭提示框并且获取提示信息
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
Selenium(6)的更多相关文章
- 面试准备——(三)Selenium(1)基础问题及自动化测试
转载:https://www.cnblogs.com/lesleysbw/p/6413880.html 面试准备——(三)Selenium(1)基础问题及自动化测试 滴滴面试: 1. 自己负责哪部 ...
- Selenium(Webdriver)自动化测试常问问题
http://blog.sina.com.cn/s/blog_c189e2590102w3bv.html Selenium(Webdriver)自动化测试常问问题 (1)selenium中如何保证操作 ...
- 初识Selenium(三)
浅谈基于Selenium的Web自动化测试框架 发表于:2011-4-25 10:58 作者:邵育亮 来源:51Testing软件测试网原创 字体:大 中 小 | 上一篇 | 下一篇 | 打印 ...
- 初识Selenium(一)
Selenium入门相关PPT参考网址:http://wenku.baidu.com/view/d1e7d90390c69ec3d5bb7565.html?from=search 内容引用网址:htt ...
- py库: Selenium (自动化测试)
http://blog.csdn.net/liujingqiu/article/details/50458553 http://www.cnblogs.com/zhaof/p/6953241.html ...
- selenium(五)伪造浏览器
简介: 这个就比较好玩了,大家还记得以前的QQ小尾巴么?还有百度贴吧的小尾巴,就是那个来自***的iphone7,这个功能. 这个功能是基于浏览器的user-agent功能实现的. 还是httpbin ...
- Selenium(Webdriver)自动化测试常问到的问题解答(转自:潜龙0318)
今天朋友问我了几个关于Selenium自动化测试的问题,我看了一下感觉还比较典型.结合我以往自动化测试的经验,给出了一些儿粗浅的答案,希望能帮大家,如果大家有什么好的看法,希望相互交流,相互学习! ( ...
- Selenium(一)自动化测试简介
1.软件开发流程 产品分析需求--架构师确认系统包含哪些模块--开发编码--开发和测试一起做单元测试--测试开展版本(集成)测试(使用手工测试,测试通过后,才开始设计脚本)--测试开展系统测试--最后 ...
- Selenium(4)
练习1:使用selenium+firefox测试ecshop登录过程 一.WebDriver 1.启动浏览器 (1)启动Firefox浏览器 a.启动默认路径下的浏览器 WebDriver drive ...
随机推荐
- lua学习笔记2--table
table是lua中的一种"数据/代码结构",可以用俩创建不同的"数据类型"lua语言中的数组其实就是table类型 array = {, , , , } pr ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- C学习笔记-字符串
对于C语言来说,字符串其实就是最后一个元素为'\0'的char数组 字符数组的初始化 字符数组常见的有两种初始化方式 char str[] = "hello"; 或者 char s ...
- pig-csm 7.9修改记录
PigCms\Lib\Action\System\UsersAction.class.php 存在页面广告跳转 bbs.go _pe.cn的问题 tpl\Home\weimob\public_head ...
- 部署开源mock平台doclever简单叙述
一.访问官网: http://doclever.cn/controller/index/index.html 二.doclever作用(重点:mock带有转发功能) DOClever是一个可视化接口管 ...
- Go基础:函数多返回值特性
之前在写Python程序的时候,惊叹于它支持多返回值的特性带来的便利性.在学习Go的时候,发现Go也拥有多返回值的特性. 1.基本用法 创建一个多返回值的方法: func getACar() (bra ...
- [loj#539][LibreOJ NOIP Round #1]旅游路线_倍增_dp
「LibreOJ NOIP Round #1」旅游路线 题目链接:https://loj.ac/problem/539 题解: 这个题就很神奇 首先大力$dp$很好想,因为可以把一维放到状态里以取消后 ...
- 【Python】【基础知识】【内置函数】【print的使用方法】
原英文帮助文档: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) Print objects to the text ...
- 嵌套泛型参数IList<IList<Object>>如何传参
在调用第三方库的时候,有这么一个泛型参数,如下图: 按照经验,使用两个List嵌套声明变量即可: IList<IList<AnnotatedPoint2D>> outImag ...
- 如何编写正确且高效的 OpenResty 应用
本文内容,由我在 OpenResty Con 2018 上的同名演讲的演讲稿整理而来. PPT 可以在 这里 下载,因为内容比较多,我就不在这里一张张贴出来了.有些内容需要结合 PPT 才能理解,请多 ...