首页 > 技术文章 > WebDriver高级应用实例(4)

z-zzz 2019-03-12 11:28 原文

  4.1操作web页面的滚动条

  被测网页的网址:

  http://v.sogou.com

  Java语言版本的API实例代码 

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import javax.swing.event.TreeWillExpandListener;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class scrolling {
    WebDriver driver;
    String url ="http://v.sogou.com";
 //priority = 1 表示测试用例的第一优先级
  @Test(priority = 1)
  public void scrollingToBottomofAPage() {
      //将页面滚动至页面最下方
      ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
     //设置3秒停顿验证滚动条是否移动至指定位置
      try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
  @Test(priority = 2)
  public void scrollingToElementofAPage(){
      //找到标签文字为电视剧的标签
      WebElement element = driver.findElement(By.xpath("//*[@id='container']//a[text()='电视剧']"));
      //使用scrollIntView()函数。将滚动条移至元素所在的位置
      ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",element);
      try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
  @Test(priority = 3)
  public void scrollingByCoordinatesofAPage(){
      //将页面滚动条向下移动800个像素
      ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,800)");
      try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      //最大化
      driver.manage().window().maximize();
      driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }

}

  4.2Robot对象操作键盘

  被测网页的网址:

  http://www.sogou.com

  Java语言版本的API实例代码 

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;

public class testRoboot {
    WebDriver driver;
    String url ="http://www.sogou.com";
  @Test
  public void testRobootOperateKeyBoard() throws InterruptedException {
      //设置显示等待10秒
      WebDriverWait wait = new WebDriverWait(driver, 10);
      //使用显示等待判断页面是否存在搜索框
      wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("query")));
      //使用ctrl+v将seleium复制到搜索框中
      setAndctrlVClipboardData("seleium");
      //使用封装的方法  按下tab键将焦点移至搜索按钮
      pressTabKey();
      //调用封装的方法按下enter搜索
      pressEnterKey();
      //等待3秒查看是否执行
      Thread.sleep(3000);
  }
  public void pressEnterKey() {
      //声明Robot对象
    Robot robot = null;
    try {
        //生成Robot对象
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    //调用KeyPress()方法实现按下Enter键
    robot.keyPress(KeyEvent.VK_ENTER);
    //调用keyRelease()方法实现释放Enter键
    robot.keyRelease(KeyEvent.VK_ENTER);
  }
  public void pressTabKey() {
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
  }
  public void setAndctrlVClipboardData(String string) {
      //声明StringSelection对象并使函数的string实例化
    StringSelection stringSelection = new StringSelection(string);
    //使用toolkit对象的setContens方法将字符串放入将字符串放入剪切板中
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (AWTException e1) {
        e1.printStackTrace();
    }
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
  }
@BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }
}

推荐阅读