本文通过具体代码处理过程, 来展示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. shell 检测ip的合法性与检测网络掩码的合法性

    有时我们需要检测IP输入的正确性与网络掩码的正确性,用shell脚本写的: #验证ip地址的正确性 check_ip_format() { echo $1 | grep "^[0-9]\{1 ...

  2. android最快的模拟器

    https://www.genymotion.com/ genymotion Genymotion是一套完整的工具,它提供了Android虚拟环境.它简直就是开发者.测试人员.推销者甚至是游戏玩家的福 ...

  3. 详解Windows平台搭建Androiod开发环境

    http://blog.csdn.net/lyq8479/article/details/6348330 1.安装JDK 2.安装SDK管理器,安装SDK(在线.离线) 3.下载安装Eclipse 4 ...

  4. JavaScript要点(十三) HTML DOM EventListener

    addEventListener() 方法 <body> <p>该实例使用 addEventListener() 方法在按钮中添加点击事件. </p> <bu ...

  5. NSThead

    每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...

  6. linux设置tomcat开机自动启动

    1.修改/etc/rc.d/rc.local,使用vi /etc/rc.d/rc.local 命令2.在/etc/rc.d/rc.local文件最后添加下面两行脚本 export JAVA_HOME= ...

  7. 给定表达式[x/2] + y + x * y, 其中x,y都是正整数。

    改进了一下,不过还是要十多秒吧. package com.boco.study; import java.math.BigDecimal; import java.util.Calendar; imp ...

  8. PDF转换成二进制字符串写入 HTTP 输出流

    最近项目需要做电子签章,需要网页打开PDF签章后保存:正好复习哈二进制和流的转换: 文件转换成二进制字符串写入HTTP输出流 protected void Page_Load(object sende ...

  9. qt-vs-addin-版本支持

    qt-vs-addin-1.2.0-opensource.exe         VS200X qt-vs-addin-1.2.1-opensource.exe         VS200X qt-v ...

  10. IIS 之 添加MIME扩展类型及常用的MIME类型列表

    经常用IIS作为下载服务器的时候有时传上去的文件比如 example.mp4 文件名上传后,但是用http打开的时候确显示为 404 文件不存在.其实是IIS对文件的一种保护,不在IIS指定的MIME ...