Selenium学习8--截图,拖拽页面元素,键盘操作,鼠标右键,悬停,鼠标双击

1. 当前窗口截图

 @Test
  public void captureScreenInCurrentWindow(){
      driver.get("http:www.sogou.com");
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      try{
          FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
      }catch(Exception e){}
  }

2. 拖拽页面元素

  @Ignore
  public void dragPageElement(){
      driver.get("http://jqueryui.com/resources/demos/draggable/scroll.html");
      WebElement draggable = driver.findElement(By.id("draggable"));

      //向下移动10个像素
      for(int i = 0; i<5; i++){
          new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
      }
      //向右移动10个像素
      for(int i=0; i<5;i++){
          new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();
      }
  }

3. 模拟键盘点击

  @Test
  public void keyDown(){
      driver.get("http://www.sogou.com");
      Actions action = new Actions(driver);
      //按下按钮
      action.keyDown(Keys.CONTROL);
      action.keyDown(Keys.SHIFT);
      //释放按钮
      action.keyUp(Keys.CONTROL);
      action.keyUp(Keys.SHIFT);

      //模拟键盘输入
      action.keyDown(Keys.SHIFT).sendKeys("abdcsdf").perform();
  }

4.鼠标右键

Actions action = new Actions(driver);
//调用Action对象的contextClick方法
action.contextClick(driver.findElement(By.id("query"))).perform();

5. 鼠标悬停

action.moveToElement(link).perform();

6. 鼠标单击左键hold住, 然后释放

action.clickAndHold(div).perform();
action.release(div).perform();

7. 鼠标双击

@Test
public void doubleClick(){
    Actions action = new Actions(driver);
    action.doubleClick(findElement(By.id("inputBox")).build().perform();
}

你可能感兴趣的:(Selenium,学习笔记)