本文通过具体代码处理过程, 来展示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. ServletContextListener 启动SPRING加载数据到缓存的应用

    java 代码 public class LoadTreeForXML implements ServletContextListener {    public void contextInitia ...

  2. The maximum number of cell styles was exceeded. You can define up to 4000 styles

    POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 ...

  3. php throw new Excpetion()之后,程序还往下继续运行吗?

    经过测试是不会往下执行的,直接catch抛出异常了.

  4. 7za 解压文件

    7za.exe x -aoa 完美国际补丁.7z 7za.exe x -aoa 完美国际补丁.7z -o"C:\Users\Admin\AppData\Local\Temp\完美国际补丁&q ...

  5. [置顶] 【玩转cocos2d-x之七】场景类CCScene和布景类CCLayer

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12708811 场景类CCScene和布景类CCLayer都是作为一个容器来使 ...

  6. Xcode中的Info.plist字段列表详解

    Info.plist用于向iOS提供关于app,bundle或者framework的一些重要信息.它指定了比如一个应用应该怎样启动,它如何被本地化,应用的名称,要显示的图标,还有更多.Info.pli ...

  7. [018]C++ explicit构造函数

    explicit [英][ɪkˈsplɪsɪt][美][ɪkˈsplɪsɪt] adj.明确的,清楚的; 直言的; 详述的; 不隐瞒的; 看到上面的英文解释,我们应该就知道explicit构造函数是什 ...

  8. js jquery 等的地址

    jquery在线地址(jquery地址):http://code.jquery.com/jquery-latest.js js人脉图(关系图)插件: http://js.cytoscape.org/

  9. php输出错误信息

    error_reporting(E_ALL);  ini_set('display_errors','on'); header("Content-Type:text/html;charset ...

  10. Java ZIP File Example---refernce

    In this tutorial we are going to see how to ZIP a file in Java. ZIP is an archive file format that e ...