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

WebDriver高级应用实例(4)的更多相关文章

  1. WebDriver高级应用实例(10)

    10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...

  2. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  3. WebDriver高级应用实例(8)

    8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...

  4. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  5. WebDriver高级应用实例(6)

    6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...

  6. WebDriver高级应用实例(5)

    5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...

  7. WebDriver高级应用实例(3)

    3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...

  8. WebDriver高级应用实例(2)

    2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...

  9. WebDriver高级应用实例(1)

    1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...

随机推荐

  1. 2018.12.12 codeforces 931E. Game with String(概率dp)

    传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...

  2. 菜品识别 SDK调用

    from aip import AipImageClassify import os """ 填入参数 """ APP_ID = 'your ...

  3. sprintf()与sscanf()

    1.sprintf() sprintf()用于向字符串中写入格式化的数据,eg: int dSrc1 = 1; int dSrc2 = 2; char str[] = "hello" ...

  4. java Concurrent包学习笔记(六):Exchanger

    一.概述 Exchanger 是一个用于线程间协作的工具类,Exchanger用于进行线程间的数据交换,它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据.这两个线程通过exchange 方法 ...

  5. java使用filter设置跨域访问

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  6. canvas打字效果

    运用fillText,写的打字效果. 唯一麻烦的地方是,换行问题, 我是把字符串转化为数组,数组一个单位完成,就换行,继续下一个单位. <!doctype html> <html&g ...

  7. property属性[Python]

    一.property解释 根据文档资料解释: property([fget[, fset[, fdel[, doc]]]]) Return a property attribute for new-s ...

  8. switch()语句

    语法: switch(expression){ case value:statement break; case value:statement break; case value:statement ...

  9. Scala界面事件处理编程实战详解.

    今天学习了一个Scala界面事件处理编程,让我们从代码出发. import scala.swing._import scala.swing.event._ object GUI_Panel exten ...

  10. 编写Shell脚本

    1.脚本的编写 Shell脚本本身是一个文本文件,这里编写一个简单的程序,在屏幕上显示一行helloworld! 脚本内容如下: #!/bin/bash #显示“Hello world!" ...