selenium2.0处理case实例(二)
本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法
1、javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用executeScript()就可执行javascript;
2、TakesScreenshot,自动化测试中,截图可以帮助我们直观的定位错误、记录测试步骤;webdriver的截图功能十分方便,只需要driver.get到当前要截图的页面, 再将driver强转成TakesScreenshot 类型,调用getScreenshotAs(getScreenshotAs)方法得到截图文件
3、DragAndDrop,模拟拖放操作, 使用actions.dragAndDrop(source, target).perform();来完成。source是要拖拉操作的对象,比如一张图片等, target是要放置的位置
4、Select类,下拉框类型的控件,可以通过Select select=new Select(driver.findElement(By.id("select1"))); 来得到Select对象, 就可以对选项进行操作,比如,提供了“清空选择项”,“通过text/index/value来选择选项”,“获得所有的选项”,“获得当前选择的选项”等方法,具体可参见api,用法可参见博客“selenium2.0处理case实例(一)”
下面代码按照顺序,列出上述类具体使用过程和场景:
package mavenSelenium;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import com.google.common.io.Files; public class TestBlog extends Assert {
WebDriver driver; @Before
public void init(){
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
} @Test
public void test()throws Exception{
//1、driver execute JavaScript
driver.get("http://www.baidu.com");
JavascriptExecutor js=(JavascriptExecutor)driver;
String title=(String)js.executeScript("return document.title");
System.out.println(title); //String script="function timeStamp(){ var d = new Date(); var m=d.getMonth()+1;var m=(m>9)?m:\"0\"+m; var dd=(d.getDate()>9)?d.getDate():\"0\"+d.getDate();var hh=(d.getHours()>9)?d.getHours():\"0\"+d.getHours();var mm=(d.getMinutes()>9)?d.getMinutes():\"0\"+d.getMinutes(); var ss=(d.getSeconds()>9)?d.getSeconds():\"0\"+d.getSeconds();var timeStamp=\"\"+d.getFullYear()+m+dd+hh+mm+ss; return timeStamp; }var rs;rs=timeStamp();";
String script="var d = new Date(); var m=d.getMonth()+1;var m=(m>9)?m:\"0\"+m; var dd=(d.getDate()>9)?d.getDate():\"0\"+d.getDate(); var hh=(d.getHours()>9)?d.getHours():\"0\"+d.getHours(); var mm=(d.getMinutes()>9)?d.getMinutes():\"0\"+d.getMinutes(); var ss=(d.getSeconds()>9)?d.getSeconds():\"0\"+d.getSeconds();var timeStamp=\"\"+d.getFullYear()+m+dd+hh+mm+ss; return timeStamp;";
System.out.println(script);
String timestamps=(String)js.executeScript(script);
System.out.println(timestamps); //2、TakesScreenshot
File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String descFilePath="d:\\test2.jpeg";
Files.copy(srcFile, new File(descFilePath));
File descFile=new File(descFilePath);
assertTrue(descFile.exists()); //3、Drag And Drop
driver.get("https://www.casa.com/login.qs?returnurl=/StyleBoard/Detail.qs?StyleBoardId=9468");
driver.findElement(By.id("emailTextBox")).sendKeys("jennifer.huang@suryani.cn");
driver.findElement(By.id("PwdTextBox_Security")).sendKeys("abc123");
driver.findElement(By.id("SignInButton")).click();
Thread.sleep(2000);
WebElement source1=driver.findElement(By.xpath("//div[@id='SBProductBox1']/div/a/img"));
WebElement target1=driver.findElement(By.xpath("//div[contains(@class,'SBDrag') and child::div[@id='SBProductBox2']]"));
Actions actions=new Actions(driver);
actions.dragAndDrop(source1, target1).perform();
Thread.sleep(2000);
WebElement source2=driver.findElement(By.xpath("//div[@id='SBProductBox2']/div/a/img"));
WebElement target2=driver.findElement(By.xpath("//div[contains(@class,'SBDrag') and child::div[@id='SBProductBox1']]"));
actions.dragAndDrop(source2, target2).perform();
}
public void tearDown(){
driver.quit();
} }
dragAndDropToObject(locatorOfObjectToBeDragged, locatorOfDragDestinationObject)
dragAndDrop(locator, movementsString) //movementsString - offset in pixels from the current location to which the element should be moved, e.g., "+70,-300"
dragAndDrop(Point, Point) // Point point = driver.findElement(locator).getLocation(); get (x,y)
selenium2.0处理case实例(二)的更多相关文章
- selenium2.0处理case实例(一)
通过自动化脚本, 判断下拉框选项值是否按照字母顺序(忽略大小写)显示 case场景如下: 1)打开www.test.com;2)判断下拉框选项是否按照字母顺序排列(忽略大小写)3)选择其中一个任意选项 ...
- Python版:Selenium2.0之WebDriver学习总结_实例1
Python版:Selenium2.0之WebDriver学习总结_实例1 快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...
- selenium1.0和selenium2.0页面等待处理详解
一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...
- selenium2.0 处理各种窗口问题解决方法
selenium2.0处理muti-Windows . Frames .Popup Dialogs selenium2.0处理多窗口,弹窗等,只需要调用WebDriver 嵌套类:TargetLoca ...
- C语言库函数大全及应用实例二
原文:C语言库函数大全及应用实例二 [编程资料]C语言库函数大全及应用实例二 函数名: bioskey 功 能 ...
- 转:Selenium2.0之grid学习总结
(一)介绍: Grid的功能: 并行执行 通过一个中央管理器统一控制用例在不同环境.不同浏览器下运行 灵活添加变动测试机 (二)快速开始 这个例子将介绍如何使用selenium2.0的grid,并且注 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十║Vue基础终篇:传值+组件+项目说明
缘起 新的一天又开始啦,大家也应该看到我的标题了,是滴,Vue基础基本就到这里了,咱们回头看看这一路,如果你都看了,并且都会写了,那么现在你就可以自己写一个Demo了,如果再了解一点路由,ajax请求 ...
- selenium win7+selenium2.0+python环境搭建
win7+selenium2.0+python环境搭建 by:授客 QQ:1033553122 步骤1:下载python 担心最新版的支持不太好,这里我下载的是python 2.7(selenium之 ...
- Hibernate实例二
Hibernate实例二 一.测试openSession方法和getCurrentSession方法 hebernate中可以通过上述两种方法获取session对象以对数据库进行操作,下面的代码以及注 ...
随机推荐
- Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性. 懒加载属性 Swift在语言层面上提供了类中懒加载属性的支持,使用lazy作为关键字: class Renderer { lazy v ...
- Delphi- 一些H8记录
CheckOrder方法写在uDataModConn类里.
- C#公历转农历算法
C#公历转农历算法,高手们也可以改写一下让其更加有效率! Code/// <summary> /// LunDay 的摘要说明. /// 用法说明 /// 直接调用即可,比较简单 /// ...
- 磐石加密狗NT88管理层API
磐石加密狗NT88管理层API 直接贴代码了 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 ...
- Indesign多媒体富交互插件【MagBuilder】与iOS app 【MagViewer】介绍
[写在前面]进园子有一段时间了,从来都是看别人的文章,自己的一点东西都记在本地笔记里,现在想把一些东西拿来出分享,希望能够认识一些志同道合的朋友和老师. 学习Adobe插件开发的初衷是为了给PS做插件 ...
- solr-1.4.1 环境配置
solr-1.4.1 环境配置: Solr是一个apache名下非常好用的开源索引.搜索工具,网上的资料虽多但非常杂,笔者花了一天的时间对Solr进行了较为初步的研究,对Solr的基础应用做了一定的总 ...
- forward_list例子
9.28 编写函数,接受一个forward_list<string>和两个string共三个参数.函数应在链表中查找第一个string,并将第二个string插入到紧接着第一个string ...
- js之parentElement属性
<html> <head> </head> <body> <form name="a "> <table name ...
- 如何查找局域网的外网ip
方法一:一个简单的方法 用你电脑打开www.ip138.com 就可以看到自己的公网IP地址 方法二:如果一定要从路由器里面看 那就打开路由的配置页面 一般在系统状态里面 会有个WAN口IP 那就是你 ...
- Spring在Web项目中的三种启动加载的配置
在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...