码迷,mamicode.com
首页 > Web开发 > 详细

WebDriver高级应用实例(4)

时间:2019-03-12 12:33:31      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:arguments   webdriver   sleep   用例   after   url   view   find   移动   

  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();
  }

 }

WebDriver高级应用实例(4)

标签:arguments   webdriver   sleep   用例   after   url   view   find   移动   

原文地址:https://www.cnblogs.com/z-zzz/p/10515576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!