本文通过具体代码处理过程, 来展示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实例(二)的更多相关文章

  1. selenium2.0处理case实例(一)

    通过自动化脚本, 判断下拉框选项值是否按照字母顺序(忽略大小写)显示 case场景如下: 1)打开www.test.com;2)判断下拉框选项是否按照字母顺序排列(忽略大小写)3)选择其中一个任意选项 ...

  2. Python版:Selenium2.0之WebDriver学习总结_实例1

    Python版:Selenium2.0之WebDriver学习总结_实例1  快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...

  3. selenium1.0和selenium2.0页面等待处理详解

    一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...

  4. selenium2.0 处理各种窗口问题解决方法

    selenium2.0处理muti-Windows . Frames .Popup Dialogs selenium2.0处理多窗口,弹窗等,只需要调用WebDriver 嵌套类:TargetLoca ...

  5. C语言库函数大全及应用实例二

    原文:C语言库函数大全及应用实例二                                              [编程资料]C语言库函数大全及应用实例二 函数名: bioskey 功 能 ...

  6. 转:Selenium2.0之grid学习总结

    (一)介绍: Grid的功能: 并行执行 通过一个中央管理器统一控制用例在不同环境.不同浏览器下运行 灵活添加变动测试机 (二)快速开始 这个例子将介绍如何使用selenium2.0的grid,并且注 ...

  7. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十║Vue基础终篇:传值+组件+项目说明

    缘起 新的一天又开始啦,大家也应该看到我的标题了,是滴,Vue基础基本就到这里了,咱们回头看看这一路,如果你都看了,并且都会写了,那么现在你就可以自己写一个Demo了,如果再了解一点路由,ajax请求 ...

  8. selenium win7+selenium2.0+python环境搭建

    win7+selenium2.0+python环境搭建 by:授客 QQ:1033553122 步骤1:下载python 担心最新版的支持不太好,这里我下载的是python 2.7(selenium之 ...

  9. Hibernate实例二

    Hibernate实例二 一.测试openSession方法和getCurrentSession方法 hebernate中可以通过上述两种方法获取session对象以对数据库进行操作,下面的代码以及注 ...

随机推荐

  1. [Objective-c 基础 - 2.10] description方法

    A. 实例对象打印-description 1.当使用NSLog函数并且使用%@占位符的时候,会调用对象的-description方法 2.拿到-description的返回值,显示到console中 ...

  2. BAT及各大互联网公司2014前端笔试面试题:JavaScript篇

    前面几题是会很基础,越下越有深度. 初级Javascript: 1.JavaScript是一门什么样的语言,它有哪些特点? 没有标准答案. 2.JavaScript的数据类型都有什么? 基本数据类型: ...

  3. 使用Redis bitmaps进行快速、简单、实时统计

    原文:Fast, easy, realtime metrics using Redis bitmaps (http://blog.getspool.com/2011/11/29/fast-easy-r ...

  4. xcode6.4 7.2下载地址

    XCode 7.2 :ht tp://adcdownload.apple.com/Developer_Tools/Xcode_7.2/Xcode_7.2.dmgXCode7.1.1:ht tp://a ...

  5. webViewDidFinishLoad因为网页里的重定向,会调用多次,使用web view.isLoading来解决

    我编码如下,但我发现 webViewDidFinishLoad() 会发生若干次. 如何知道 webViewDidFinishLoad() 最后发生吗? iNavigate = ; - (void)w ...

  6. 【转】数据库中的join

    转自:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有 ...

  7. sql函数:汉字转换为拼音

    sql数据库自定义一个函数把下面代码写进去 功能是得到汉字拼音首字母create function fun_getPY(@str nvarchar(4000)) returns nvarchar(40 ...

  8. mysql 字符集设置查看

    1.列出MYSQL支持的所有字符集: SHOW CHARACTER SET; 2.当前MYSQL服务器字符集设置 SHOW VARIABLES LIKE 'character_set_%'; 3.当前 ...

  9. 电商ERP常见功能模块

      电商ERP是适用企业卖家的专业电子商务ERP,支持淘宝.天猫.京东.1688.当当.苏宁.拍拍.唯品会.亚马逊.独立B2C等多网络销售渠道:也包括 异地多仓..货位管理.智能配货等专业的WMS(仓 ...

  10. Java设计模式09:单例模式的强化(控制实例个数n)

    1. 单例模式的本质: 控制实例数目(目的节约资源) 2. 单例模式体现的一些思想: (1)延迟装载(Lazy Load):懒汉式 (2)缓存:饿汉式 3. 单例模式的变形使用: 控制使用实例个数为3 ...