本文通过具体代码处理过程, 来展示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. web开发工具类

    1.日期工具类 import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { public sta ...

  2. 【Linux】多睡/少睡一小时!冬夏令时全解析

    多伦多2016年11月6日凌晨2点开始起时间调回一小时,时间到凌晨2点时自动跳回到1点,大家可以多睡一小时(或者多一小时写essay的时间)~ 多伦多2017年3月12日凌晨2点开始时间拨快一小时时间 ...

  3. (1/18)重学Standford_iOS7开发_iOS概述_课程笔记

    写在前面:上次学习课程对iOS还是一知半解,由于缺乏实践,看公开课的视频有时不能很好地领会知识.带着问题去学习永远是最好的方法,接触一段时间iOS开发以后再来看斯坦福iOS公开课,又会有许多新的发现, ...

  4. SQLite使用教程10 运算符

    SQLite 运算符 SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算. 运算符用于指定 SQLite 语句中的条件 ...

  5. cocos2d-x Touch

    转自:http://codingnow.cn/cocos2d-x/783.html 游戏跟视频最大的区别就是互动,玩家可以操控游戏中的角色,现在的移动设备几乎人手一台,基本上全部都是基于触屏操作的,今 ...

  6. metasploit(MSF)终端命令大全

    show exploits   列出metasploit框架中的所有渗透攻击模块. show payloads   列出metasploit框架中的所有攻击载荷. show auxiliary   列 ...

  7. [置顶] 用Wireshark保存RTP的负载码流

    这段时间工作太忙,有些日子没写文章了,今天准备了一篇Wireshark工具的一个小功能,在验证码流的时候非常好用,闲话不说,直接说步骤: 1.打开Wireshark抓取流媒体码流,然后用RTP过滤: ...

  8. 将图片序列保存为GIF文件

    http://stackoverflow.com/questions/11459568/issue-with-rebuilding-and-saving-gifs-using-tgifimage 将图 ...

  9. [VirtualBox] Install Ubuntu 14.10 error 5 Input/output error

    After you download the VirtualBox install package and install it (just defualt setting). Then you sh ...

  10. [Effective C++ --011]在operator=中处理“自我赋值”

    一.何谓“自我赋值”? 1.1.场合一 直接赋值 w = w; 1.2.场合二 同一数组         a[i] = a[j]: 1.3.场合三 指针         *px = *py: 1.4. ...