==========================================================================================================

写在前面:

最近项目一直很忙,没有时间来整理自动化测试方面的知识。

由于项目忙,自动化测试一直未使用到项目中来,希望接下来在项目之余,能够抽出时间来将自动化测试投入到实际的项目中来。

==========================================================================================================

参考链接:http://www.cnblogs.com/itliucheng/p/5578788.html

1、Selenium定位器

下表给出了定位selenium 元素的webdriver的Java语法。

使用Jquery查找元素

2、常用命令

下表列出了webdriver的最常用的命令以及它的语法,这将有助于我们开发webdriver脚本。

3、文本框的相互作用

String result = driver.findElement(By.id("cpar1")).getAttribute("value");
driver.findElement(By.id("cpar1")).sendKeys("10");
driver.navigate().to("http://www.baidu.com");
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("魔兽");
driver.findElement(By.id("su")).click();

4、单选按钮互动

// Click on Radio Button
driver.findElement(By.id("cpayoff1")).click();
   WebElement radioOption = driver.findElement(By.xpath("//input[@value='orange']"));
if(!radioOption.isSelected()){
radioOption.click();
}
List<WebElement> fruits = driver.findElements(By.name("fruit"));
for(WebElement fruit : fruits){
if(fruit.getAttribute("value").equals("watermelon")){
if(!fruit.isSelected()){
fruit.click();
Assert.assertTrue(fruit.isSelected());
break;
}
}
}

5、复选框交互

// Click on check Box
driver.findElement(By.id("caddoptional")).click();

6、下拉交互

用“selectByVisibleText'或'selectByIndex'或'selectByValue'的方法选择一个选项。
Select dropdown = new Select(driver.findElement(By.id("ccompound")));
dropdown.selectByVisibleText("continuously");
   Select dropSelect = new Select(driver.findElement(By.name("fruit")));
//判断页面是否进行多选
Assert.assertTrue(dropSelect.isMultiple());
//使用选择项索引选择第三个选项
dropSelect.selectByIndex(3);
//根据value值选择
dropSelect.selectByValue("value");
//根据选项文字选择
dropSelect.selectByVisibleText("苹果");
//取消所有选项的选中状态
dropSelect.deselectAll();
//取消第三个的选中状态
dropSelect.deselectByIndex(3);
//根据value值取消选择
dropSelect.deselectByValue("value");
//根据选项文字取消选择
dropSelect.deselectByVisibleText("苹果");

7、同步

THREAD.SLEEP
Thread.Sleep(1000); //Will wait for 1 second.
显式等待
WebElement DynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("DynamicElement")));
隐式等待
WebElement DynamicElement = driver.findElement(By.id("DynamicElement"));
流利等待
Wait wait = new FluentWait(driver)
.withTimeout(60, SECONDS)
.pollingEvery(10, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement dynamicelement = wait.until(new Function<webdriver,webElement>()
{
public WebElement apply(WebDriver driver)
{
return driver.findElement(By.id("dynamicelement"));
}
}
);

8、拖放

http://www.yiibai.com/selenium/selenium_drag_drop.html#article-start
WebElement From = driver.findElement(By.xpath(".//*[@id='j3_7']/a"));
WebElement To = driver.findElement(By.xpath(".//*[@id='j3_1']/a"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From)
.moveToElement(To)
.release(To)
.build();
dragAndDrop.perform();

9、键盘操作

void sendKeys(java.lang.CharSequence keysToSend)
void pressKey(java.lang.CharSequence keyToPress)
void releaseKey(java.lang.CharSequence keyToRelease)

10、鼠标操作

Click - 进行点击。我们还可以执行基于坐标的点击。
contextClick - 执行上下文点击/右键单击一个元素或基于坐标
doubleClick - 执行双击webelement或基于坐标。如果留空它执行双击当前位置。
mouseDown - 执行一个元素上按下鼠标操作或基于坐标。
mouseMove - 执行元素上的鼠标移动操作或基于坐标。
mouseUp - 释放鼠标通常伴随着鼠标按下的动作和行为的基础上统筹。
void click(WebElement onElement)
void contextClick(WebElement onElement)
void doubleClick(WebElement onElement)
void mouseDown(WebElement onElement)
void mouseUp(WebElement onElement)
void mouseMove(WebElement toElement)
void mouseMove(WebElement toElement, long xOffset, long yOffset)

11、多选择操作

12、查找所有链接

java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Number of Links in the Page is " + links.size());
for (int i = 1; i<=links.size(); i=i+1)
{
System.out.println("Name of Link# " + i - + links.get(i).getText());
}

13、等待操作

设定查找页面元素的最大等待时间,调用findElement方法的时候没有能立即找到某个元素,则程序会每隔一段时间后不断的尝试判断页面的DOM中是否出现被查找的元素,如果超过设定的等待时长依旧没有找到,则抛出NoSuchElementException

14、隐形等待

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

@BeforeClass
public static void init() {
System.out.println("init...");
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// 创建一个 ChromeDriver 的接口,用于连接 Chrome,
//必须要有chromedriver.exe文件,selenium默认不能启动chrome
// 创建一个 Chrome 的浏览器实例
driver = new ChromeDriver();
//最大化浏览器
driver.manage().window().maximize();
//设置全局的隐形等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

隐性等待的默认时长是0,一旦设置,这个隐试等待会在webdriver对象实例的整个生命周期起作用 

15、显示等待

显示等待比隐式等待更节约测试脚本执行的时间,推荐使用显示等待判断页面元素是否存在
使用ExpectedConditions类中自带的方法,可以进行显示等待的判断

//页面元素是否在页面上可用和可被点击
ExpectedConditions.elementToBeClickable(By locator);
//页面元素是否处于被选中状态
ExpectedConditions.elementToBeSelected(By locator);
//页面元素在页面是否存在
ExpectedConditions.presenceOfElementLocated(By locator);
//是否包含特定的文本
ExpectedConditions.textToBePresentInElement(locator, text)
//页面元素值
ExpectedConditions.textToBePresentInElementValue(locator, text);
//标题
ExpectedConditions.titleContains(title); // 等待元素可见且可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));

16、自定义的显示等待

public static void sendKeysByXPath(WebDriver driver, String path, String key) {
WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
WebElement element = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(path));
}
});
highLightElement(driver,element);
element.clear();
element.sendKeys(key);
}

17、操作javascript的Alert弹窗

driver.findElement(By.xpath(path)).click();
//切换到alert弹出框
Alert alert = driver.switchTo().alert();
AssertionUtil.assertEquals("这是个alert弹出框", alert.getText());
//使用alert的accept方法,单击alert的确定按钮,关闭alert
alert.accept();
//如果alert未弹出,会抛出NoAlertPresentException异常
//切换回去原来的窗体
driver.switchTo().defaultContent();
alert.accept();是确定
alert.dismiss();是取消

18、操作模态框

driver.switchTo().activeElement();

==========================================================================================================

写在后面:

这个模块学习的内容,应该是平时写代码过程中用到的最多的内容。可以尝试将部分内容写成共通,比如alert框的确定和消失等。

==========================================================================================================

【Selenium-WebDriver自学】WebDriver交互代码(十一)的更多相关文章

  1. [Selenium] 搭建 Android WebDriver 环境

    1.安装 Android SDK 到如下网址下载 Android SDK http://developer.android.com/sdk/index.html 2.创建 Android 虚拟设备 解 ...

  2. 【译】Selenium 2.0 WebDriver

    Selenium WebDriver   注意:我们正致力于完善帮助指南的每一个章节,虽然这个章节仍然存在需要完善的地方,不过我们坚信当前你看到的帮助信息是精确无误的,后续我们会提供更多的指导信息来完 ...

  3. Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考

    Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...

  4. 浅谈python中selenium库调动webdriver驱动浏览器的实现原理

    最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...

  5. selenium跳过webdriver检测并爬取淘宝我已购买的宝贝数据

    简介 上一个博文已经讲述了如何使用selenium跳过webdriver检测并爬取天猫商品数据,所以在此不再详细讲,有需要思路的可以查看另外一篇博文. 源代码 # -*- coding: utf-8 ...

  6. Python3.x:Selenium中的webdriver进行页面元素定位

    Python3.x:Selenium中的webdriver进行页面元素定位 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver ...

  7. Selenium Firefox 官方Webdriver -- Geckodriver 下载地址

    Selenium Firefox 官方Webdriver -- Geckodriver 下载地址 https://github.com/mozilla/geckodriver/releases

  8. 孤荷凌寒自学python第八十一天学习爬取图片1

    孤荷凌寒自学python第八十一天学习爬取图片1 (完整学习过程屏幕记录视频地址在文末) 通过前面十天的学习,我已经基本了解了通过requests模块来与网站服务器进行交互的方法,也知道了Beauti ...

  9. Python + Selenium +Chrome 批量下载网页代码修改【新手必学】

    Python + Selenium +Chrome 批量下载网页代码修改主要修改以下代码可以调用 本地的 user-agent.txt 和 cookie.txt来达到在登陆状态下 批量打开并下载网页, ...

随机推荐

  1. Hive HiveServer2+beeline+jdbc客户端访问操作

    HiveServer 查看/home/hadoop/bigdatasoftware/apache-hive-0.13.1-bin/bin目录文件,其中有hiveserver2 启动hiveserver ...

  2. Linux常用命令之定时任务

    定时任务的实现,可以让我们把很多重复的,有规律的事情交给机器做.我们就不用苦逼的烦躁做同一件事,这样也让我们做程序的有更多的乐趣和价值.用技术的手段解决常人花时间精力解决的问题.在Linux下实现定时 ...

  3. linux lvm create and manager

    http://blog.51cto.com/dreamfire/1084729 https://www.cnblogs.com/xiaoluo501395377/archive/2013/05/24/ ...

  4. webstorm 破解码

    https://blog.csdn.net/voke_/article/details/76418116 摘自此博客

  5. a:hover应用精粹

    原本想把题目叫做“纯CSS相册2”的,但在实现过程中试验了许多东西,干脆全部写出来分享了.大家知道,能兼容IE6的具有动态切换能力的CSS属性也只有hover伪类了,但hover伪类在IE仅对链接生效 ...

  6. 【springBoot】之定制Banner

    springboot启动时控制台打印图案如下: 1.假如我们不想看到这个图案 public static void main(String[] args) { SpringApplication ap ...

  7. 阿里云服务器 ECS Linux操作系统加固

    1. 账号和口令 1.1 禁用或删除无用账号 减少系统无用账号,降低安全风险. 操作步骤 使用命令 userdel <用户名> 删除不必要的账号. 使用命令 passwd -l <用 ...

  8. SFINAE简单实例

    SFINAE(Substitution failure is not an error),是C++11以来推出的一个重要概念,这里,只是简单举一个例子,可能会有人需要. // 添加 scalar nu ...

  9. Spring的LoadTimeWeaver(代码织入)(转)

    https://www.cnblogs.com/wade-luffy/p/6073702.html 在Java 语言中,从织入切面的方式上来看,存在三种织入方式:编译期织入.类加载期织入和运行期织入. ...

  10. SqlServer全表遍历

    DECLARE @temp TABLE ( , ) , ) ) DECLARE @tempId INT , ) INSERT INTO @temp VALUES ( 'a' ) INSERT INTO ...