将当前浏览器截屏

  测试网址

  http://www.baidu.com

  Java语言版本实例 

  @Test
  public void captureScreenInCurrentWindows() {
    driver.manage().window().maximize();
    driver.get(url);
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
      FileUtils.copyFile(scrFile,new File("D:\\a.png"));
    } catch (Exception e) {
      e.printStackTrace();
    }
   }

  杀掉Windows的浏览器进程

  测试前打开3个浏览器

  Java语言实例代码:

  @Test

  public void operateWindowsProcess(){

    WindowsUtils.tryToKillByName("firefox.exe");

    WindowsUtils.tryToKillByName("iexplore.exe");

    WindowsUtils.tryToKillByName("chrome.exe");

}

  将当前浏览器截屏

  测试网址

  http://www.baidu.com

  Java语言版本实例 

  @Test
  public void isElementTextPresent() {
    driver.manage().window().maximize();
    driver.get(url);
    WebElement text = driver.findElement(By.xpath("//*[@id='u1']/a[1]"));
    String contentText = text.getText();
    //判断页面文字是否匹配
    Assert.assertEquals("新闻", contentText);
    //判断页面文字是否包含
    Assert.assertTrue(contentText.contains("新"));
    //判断页面文字开头是否匹配
    Assert.assertTrue(contentText.startsWith("新"));
    //判断页面文字结尾文字是否匹配
    Assert.assertTrue(contentText.endsWith("闻"));
  }

  执行avaScript脚本

  测试网址

  http://www.baidu.com

  Java语言版本实例 

  @Test
  public void executeJavaScript() {
    driver.manage().window().maximize();
    driver.get(url);
    //声明一个JavaScript对象
    JavascriptExecutor js = (JavascriptExecutor) driver;
    //调用executeScript方法执行return document.title
    String title = (String) js.executeScript("return document.title");
    Assert.assertEquals("百度一下,你就知道", title);
    //获取按钮对象返回按钮文字
    String serachButtonText = (String) js.executeScript("var button = document.getElementById('su');return button.value");
    System.out.println(serachButtonText);
  }

  拖拽页面元素  

  测试网址

  https://jqueryui.com/draggable/

  Java语言版本实例 

  @Test
  public void dragePageElement() throws InterruptedException {
    driver.manage().window().maximize();
    driver.navigate().to(url);
    //找到需配置的对象
    Thread.sleep(6000);
    //切换至第一个iframe
    driver.switchTo().frame(0);
    WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
    //向下拖动10个像素,共5次
    for(int i=0;i<5;i++){
      //0表示坐标不变
      new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
    }
    Thread.sleep(2000);
    //向右拖动10个像素,共5次
    for(int i=0;i<5;i++){
      new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();
    }
    Thread.sleep(2000);
  }

  模拟键盘操作

  测试网址

  http://www.baidu.com

  Java语言版本实例

  @Test
  public void clickKeys() {
    driver.manage().window().maximize();
    driver.navigate().to(url);
    Actions action = new Actions(driver);
    action.keyDown(Keys.CONTROL); //按下ctrl
    action.keyDown(Keys.SHIFT); //按下shift
    action.keyDown(Keys.ALT); //按下Alt
    action.keyUp(Keys.CONTROL); //松开ctrl
    action.keyUp(Keys.SHIFT); //松开shift
    action.keyUp(Keys.ALT); //松开alt
    //模拟输入大写字符
    action.keyDown(Keys.SHIFT).sendKeys("abcdefg").perform();
  }

  模拟鼠标右键操作

  测试网址

  http://www.baidu.com

  Java语言版本实例  

  @Test
  public void rightClickMouse() {
    driver.manage().window().maximize();
    driver.get(url);
    Actions action = new Actions(driver);
    //模拟鼠标右键事件
    action.contextClick(driver.findElement(By.id("kw"))).perform();
  }

WebDriverAPI(5)的更多相关文章

  1. WebDriverAPI(7)

      查看页面元素的属性 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void getWebElementAttribute() { dri ...

  2. WebDriverAPI(10)

    操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...

  3. WebDriverAPI(9)

    操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...

  4. WebDriverAPI(4)

    单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...

  5. WebDriverAPI(2)

    操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...

  6. WebDriverAPI(8)

    判断页面元素是否存在 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void testIsElementPresent(){ driver. ...

  7. WebDriverAPI(6)

    在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...

  8. WebDriverAPI(3)

    获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...

  9. WebDriverAPI(1)

    访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...

随机推荐

  1. 2018.09.27 codeforces1045D. Interstellar battle(期望dp)

    传送门 一道有意思的期望dp. 题意是给出一棵树,每个点最开始都有一个gg的概率,有m次修改,每次修改会把某个点gg的概率更换掉,让你求出每次修改之后整个树被分成的连通块的数量的期望(gg掉的点不算) ...

  2. hdu -1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 思路:求出存钱罐装全部装满情况下硬币的最小数量,即求出硬币的最小价值.转换为最小背包的问题. # ...

  3. 31. The New Bread Earners 挣钱养家的新军

    31. The New Bread Earners 挣钱养家的新军 ① They call them the new bread earners.They are women,and they are ...

  4. Linux IPC之共享内存

    System V共享内存机制: shmget  shmat  shmdt  shmctl 原理及实现: system V IPC机制下的共享内存本质是一段特殊的内存区域,进程间需要共享的数据被放在该共 ...

  5. ExtJS+SpringMVC文件上传与下载

    说到文件上传.下载功能,网络上大多介绍的是采用JSP.SpringMVC或者Struts等开源框架的功能,通过配置达到文件上传.下载的目地.可是最近项目要用到文件上传与下载的功能,因为本项目框架采用开 ...

  6. C语言中线程和进程的区别

    线程是指进程内的一个执行单元也是进程内的可调度的实体,与进程的区别 1) 调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 2) 并发性:不仅进程之间可以并发执行,同一个进程之间的多个线 ...

  7. OpenGl 中的基本数据类型

    OpenGl 中的基本数据类型 为了便于 OpenGL在各种平台上移植,OpenGL定义了自己的数据类型. 如果你愿意也可用这些数据类型对应的标准C的数据类型来替代.如OpenGL也定义 GLvoid ...

  8. spring mvc的例子

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  9. Unidac连接出错:命名管道提供程序:管道的另一端上无任何进程.

    环境: DELPHI XE ,UniDAC 6.2.8, (Options->Provider=prNativeClient) 程序编译后,放在Windows Server 2003 (sp2 ...

  10. asp.net mvc 在JS中跳转到其它controller/action

    平时在ASP.NET 中经常这样写, $('#loginOut').click(function() {           $.messager.confirm('系统提示', '您确定要退出本次登 ...