Selenium Webdriver

https://www.yiibai.com/selenium/selenium_overview.html#

webdriver自动化俗称Selenium 2.0测试Web应用程序工具。 webdriver使用不同的底层框架,Selenium 遥控器使用JavaScript的Selenium 核嵌入式已经在有一定的局限性的浏览器中。 webdriver直接交互而不与Selenium 远程控制,依赖于服务器上的任何中介的浏览器。它是用在以下方面:

在Selenium开发者社区努力下,不断提高Selenium webdriver与Selenium的整合。

  • MULT浏览器测试,包括对不能很好地支持Selenium的远程控制浏览器改进的功能(硒1.0)

  • 处理多个帧,多个浏览器窗口,弹出窗口和警报。

  • 复杂的页面导航。

  • 高级用户导航,如拖动和拖放。

  • 基于AJAX的UI元素

体系结构

webdriver最好用一个简单的架构图,说明,如下图所示。

Selenium RC VS webdriver

Selenium RC Selenium WebDriver
Selenium RC的结构复杂,因为服务器需要启动在开始试运行前。 webdriver架构比Selenium RC简单,因为它控制着从操作系统层面的浏览器。
Selenium服务器充当浏览器和Selenese的命令之间的中间人 webdriver直接相互作用,以在浏览器和使用浏览器的引擎进行控制。
Selenium RC的脚本执行速度较慢,因为它使用了Javascript来与RC互动 webdriver的速度更快,因为它直接交互使用的浏览器。
Selenium RC不能支持无头,因为它需要一个真正的浏览器一起工作。 webdriver可以支持无头执行
它是一个简单的API 复杂,API相比,RC有点大
减面向对象的API 纯粹的面向对象的API
不能测试移动应用程序 可测试iPhone/Android应用程序

使用webdriver脚本

让我们了解webdriver如何工作。为了演示目的,我们将使用http://www.calculator.net/。我们将执行“百分比计算器”,这是位于“数学计算器”。我们已经下载了所需要webdriver的JAR。请参阅环境设置一章。

第1步:从提取Eclipse文件夹中启动“Eclipse”。

第2步:点击“Browse”按钮选择工作区。

第3步:现在,创建“New Project”,从“File”菜单。

第4步:输入项目名称,然后单击“Next”。

第五步:进入Libraries选项卡,并选中所有的JAR包文件,我们已经下载(请参阅环境搭建章)。添加引用Selenium webdriver的库文件夹中的所有JAR,selenium-java-2.42.2.jar和selenium-java-2.42.2-srcs.jar

第6步:如下图所示创建包。

第7步:现在,让我们创建一个通过执行'Class'右键单击程序包,然后选择“New”>>“Class”

第8步:现在命名类,并让它设置为main方法

第9步:类概要如下所示。

步骤10:现在是时候编写代码了。下面的脚本更容易理解,因为它清楚地解释了一步,在嵌入的注释步骤。请看看“Locators”一章,了解如何捕捉对象的属性。

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver; public class webdriverdemo
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver(); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website
driver.navigate().to("http://www.calculator.net/"); //Maximize the browser
driver.manage().window().maximize(); // Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath
String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); //Print a Log In message to the screen
System.out.println(" The Result is " + result); //Close the Browser.
driver.close();
}
}

第11步:以上脚本的输出将被打印在控制台。

最常用的命令

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

Commmand 描述
driver.get("URL") 导航到应用程序
element.sendKeys("inputtext") 输入一些文本输入框
element.clear() 从输入框清空内容
select.deselectAll() 这将取消选择页面上的第一个选择所有选项:
select.selectByVisibleText("some text") select the OPTION with the input specified by the user.
driver.switchTo().window("windowName") Moving the focus from one window to another
driver.switchTo().frame("frameName") swing from frame to frame
driver.switchTo().alert() Helps in handling alerts
driver.navigate().to("URL") Navigate to the URL
driver.navigate().forward() To Navigate forward
driver.navigate().back() To Navigate back
driver.close() Closes the current Browser associated with the driver
driver.quit() Quits the driver and closes all the associated window of that driver.
driver.refresh() Refreshes the current page.

Selenium Webdriver概述(转)的更多相关文章

  1. 转载 基于Selenium WebDriver的Web应用自动化测试

    转载原地址:  https://www.ibm.com/developerworks/cn/web/1306_chenlei_webdriver/ 对于 Web 应用,软件测试人员在日常的测试工作中, ...

  2. Selenium WebDriver Code

    Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...

  3. 使用httpclient 调用selenium webdriver

    结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...

  4. selenium webdriver 右键另存为下载文件(结合robot and autoIt)

    首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...

  5. Selenium Webdriver java 积累一

    Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...

  6. Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)

            研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...

  7. Selenium Webdriver下click失效问题解决

    最近在使用Selenium Webdriver(Selenium2.0)进行界面自动化测试的时候发现单击事件无效,通过driver.findElement的方式是可以找到click元素的,但是就是cl ...

  8. 如何用selenium webdriver 捕获js error

    ### 问题 捕捉页面上js error ### 解决办法 从Selenium webdriver log 中解析 # -*- coding:utf8 -*- import unittest from ...

  9. Selenium WebDriver 之 PageObjects 模式 by Example

    目录 1. 项目配置 2. 一个WebDriver简单例子 3. 使用Page Objects模式 4. 总结 5. Troubleshooting 6. 参考文档 本篇文章通过例子来阐述一下Sele ...

随机推荐

  1. KEIL5.25生成.bin文件步骤

    添加.bin文件转换工具 KEIL5的自带.bin文件转化工具在安装目录下:我的安装目录是C盘即,C:\Keil_v5\ARM\ARMCC\bin\fromelf.exe 添加格式为:[C:\Keil ...

  2. 【纪中集训2019.3.27】【集训队互测2018】小A的旅行(白)

    题目 描述 ​ \(0-n-1\)的图,满足\(n\)是\(2\)的整数次幂, $ i \to j $ 有 $ A_{i,j} $ 条路径: ​ 一条路径的愉悦值定义为起点和终点编号的\(and\)值 ...

  3. OpenStack 镜像服务 Glance部署(六)

    Glance介绍 创建虚拟机我们需要有glance的支持,因为glance是提供镜像的服务. Glance有两个比较重要的服务: Glance-api:接受云系统镜像的构建.删除.读取请求 Glanc ...

  4. vim编辑器基本操作介绍

    vim编辑器基本操作介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 可能很多小伙伴都听说过vi编辑器或是vim编辑器.它们是Unix和Linux世界最流行的编辑器之一,他们的特 ...

  5. *p++、(*p)++、*++p、++*p 的区别

    int a[5]={1,2,3,4,5};int *p = a; *p++ 先取指针p指向的值(数组第一个元素1),再将指针p自增1: cout << *p++; // 结果为 1 cou ...

  6. Spring RedisTemplate操作-ZSet操作(6)

    @Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...

  7. [转载]JavaScript 的轻框架开发

    http://www.open-open.com/news/view/1d64fed 为什么我们不用 Angular, Ember 或者 Backbone! Muut 是一个特殊的论坛平台,它也有着巨 ...

  8. JavaScript继承详解(二)

    这一章我们将会重点介绍JavaScript中几个重要的属性(this.constructor.prototype), 这些属性对于我们理解如何实现JavaScript中的类和继承起着至关重要的作用. ...

  9. nginx1配置文件

    1,查看日志:cat /usr/local/nginx/logs/error.log 2,编辑配置文件:vi /usr/local/nginx/conf/nginx.conf 3,内容:server ...

  10. HDU 4608 I-number 2013 Multi-University Training Contest 1 1009题

    题目大意:输入一个数x,求一个对应的y,这个y满足以下条件,第一,y>x,第二,y 的各位数之和能被10整除,第三,求满足前两个条件的最小的y. 解题报告:一个模拟题,比赛的时候确没过,感觉这题 ...