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

写在前面:

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

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

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

参考链接: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. SD-SDI播出系统---使用GTX TX产生恢复时钟

    SD-SDI播出系统---使用GTX TX产生恢复时钟 1. 2.SD-SDI恢复时钟的生成

  2. datax 数据同步迁移

    https://github.com/alibaba/DataX/blob/master/mysqlwriter/doc/mysqlwriter.md https://github.com/aliba ...

  3. matlab与示波器连接及电脑连接

    参考:http://blog.sina.com.cn/s/blog_4eff3a0e0100zb8h.html 最近进行了示波器的数据采集,MSO2014,openChoice软件+Tekvisa驱动 ...

  4. WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS

    先放结论: ASIO:硬件支持+对应驱动程序 DS:兼容性最好,一般也是默认的. WASAPI:是Vista之后的,较佳选择输出方式. 再来详细看: ASIO.WDM都是指音频通道,就是音频数据走的路 ...

  5. [蓝桥杯]ALGO-116.算法训练_最大的算式

    问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号 ...

  6. 测试用例脚本,调用其他模块方法的实例(数据分类 appium 和 selenium 看这里)

    1.脚本里调用其他类里面的方法 需要把脚本里面的self.dr 传到其他类里面,其他类里面要先初始化这个self.dr 变成自己类里面的 脚本里面的dr是 appium启动的代码 dr= webdri ...

  7. Hive的安装配置 & 基础指令

    Hive 基础命令

  8. Hive格式各种格式下不同压缩算法的比较

    原始Text格式的hive分区大小为119.2G. 压缩算法 Text格式 Parquet格式 ORC RCFile 不压缩 119.2G 54.1G 20.0G 98G Snappy压缩 30.2 ...

  9. [UE4]自定义服务器Service

  10. SQL SERVER 事务相关

    1 准备数据 及 涉及到的几个设置 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED  --设置事务会话的隔离等级(默认值为 READ UNCOMMIT ...