`
macsu
  • 浏览: 5237 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Selenium Basics

阅读更多
开始– 选择你自己的Selenium 工具
大部分人开始于Selenium-IDE.这也是我们推荐的。这是一种简单的方式来熟悉Selenium命令. 你可以在几分钟内开发你第一个脚本. Selenium-IDE也很容易安装。
你也可以在Selenium-IDE跑你自己的脚本.这很容易用所以推荐给缺少技术的用户.IDE准许你开发和跑测试用例却不需要你有程序开发背景.Selenium-IDE 提供了一个优秀的选择用于训练自动化测试的初学者.一个熟悉web应用测试的测试人员可以很容易的使用Selenium-IDE进行自动化测试
一些测试任务太复杂以至于Selenium-IDE无法执行.当用到程序逻辑的时候Selenium-RC必须被用到. 例如测试需要遍历每个元素需要通过程序逻辑来做. Selenium-IDE不支持循环和条件.
另外Selenium-Core是另外一种方式.他可以跑测试脚本通过html接口TestRunner.html. 这是一种原始的方法来执行Selenium命令.像IDE一样受到很多限制,不支持遍历.
Selenium-Core不能在http和https协议之间切换,Selenium-IDE 和Selenium-RC使用的人数更多,拥有更多的支持。Selenium-Core未来很可能废弃掉。

介绍elenium 命令
Selenium 命令 – Selenese
Selenium提供了超出想象的丰富命令用于测试web应用.这个命令集合叫做 selenese.这些命令本质上创建了一门测试语言.
在seleness里面,可以根据htmltag 测试ui元素的存在。例如特定的内容,链接,输入框,筛选框,提交表单,表格数据。还可以测试窗口大小,鼠标位置,弹出窗口,ajax更能,弹出窗口,事件响应等web应用功能。命令手册列出了所有可能的命令。

一个命令就是告诉Selenium去“做什么”. Selenium 命令来源于三个层面: 行为,存取器,和断言
• 行为 是改变应用状态的命令。他们类似于 “点击一个链接” 和 “选择一个选项”. 如果性能失败或者产生错误,测试执行会失败.
很多行为被加上了 “AndWait” 后缀例如 “clickAndWait”。这个后缀告诉Selenium 这个行为会让浏览器请求一次服务端,并且等待一张新页面载入。
• 存取器 examine检查应用状态和存结果到变量中,例如“storeTitle”. 他们也使用与自动生成断言.
• 断言 类似于存取器, 但是它验证应用状态是否相同与我们期望的。例如 “make sure the page title is X” and “verify that this checkbox is checked”.
所有Selenium断言 可以被归为三类: “断言”, “验证”, and ” 等待”. 例如, 你可以“assertText”, “verifyText” and “waitForText”. 当断言失败,测试停止.当验证失败,测试会记录下错误,继续执行. This allows a single “assert” to ensure that the application is on the correct page, followed by a bunch of “verify” assertions to test form field values, labels, etc.
“waitFor” commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting (see the setTimeout action below).
Script Syntax
Selenium commands are simple, they consist of the command and two parameters. For example:
verifyText //div//a[2] Login
The parameters are not always required. It depends on the command. In some cases both are required, in others one parameter is required, and still in others the command may take no parameters at all. Here are a couple more examples:
goBackAndWait
verifyTextPresent Welcome to My Home Page
type id=phone (555) 666-7066
type id=address1 ${myVariableAddress}
The command reference describes the parameter requirements for each command.
Parameters vary, however they are typically
• a locator for identifying a UI element within a page.
• a text pattern for verifying or asserting expected page content
• a text pattern or a selenium variable for entering text in an input field or for selecting an option from an option list.
Locators, text patterns, selenium variables, and the commands themselves are described in considerable detail in the section on Selenium Commands.
Selenium scripts that will be run from Selenium-IDE may be stored in an HTML text file format. This consists of an HTML table with three columns. The first column is used to identify the Selenium command, the second is a target and the final column contains a value. The second and third columns may not require values depending on the chosen Selenium command, but they should be present. Each table row represents a new Selenium command. Here is an example of a test that opens a page, asserts the page title and then verifies some content on the page:
<table>
    <tr><td>open</td><td></td><td>/download/</td></tr>
    <tr><td>assertTitle</td><td></td><td>Downloads</td></tr>
    <tr><td>verifyText</td><td>//h2</td><td>Downloads</td></tr>
</table>
Rendered as a table in a browser this would look like the following:
open /download/
assertTitle Downloads
verifyText //h2 Downloads
The Selenese HTML syntax can be used to write and run tests without requiring knowledge of a programming language. With a basic knowledge of selenese and Selenium-IDE you can quickly produce and run testcases.
Test Suites¶
A test suite is a collection of tests. Often one will run all the tests in a test suite as one continuous batch-job.
When using Selenium-IDE, test suites also can be defined using a simple HTML file. The syntax again is simple. An HTML table defines a list of tests where each row defines the filesystem path to each test. An example tells it all.
<html>
<head>
<title>Test Suite Function Tests - Priority 1</title>
</head>
<body>
<table>
  <tr><td><b>Suite Of Tests</b></td></tr>
  <tr><td><a href="./Login.html">Login</a></td></tr>
  <tr><td><a href="./SearchValues.html">Test Searching for Values</a></td></tr>
  <tr><td><a href="./SaveValues.html">Test Save</a></td></tr>
</table>
</body>
</html>
A file similar to this would allow running the tests all at once, one after another, from the Selenium-IDE.
Test suites can also be maintained when using Selenium-RC. This is done via programming and can be done a number of ways. Commonly Junit is used to maintain a test suite if one is using Selenium-RC with Java. Additionally, if C# is the chosen language, Nunit could be employed. If using an interpreted language like Python with Selenium-RC than some simple programming would be involved in setting up a test suite. Since the whole reason for using Sel-RC is to make use of programming logic for your testing this usually isn’t a problem.
Commonly Used Selenium Commands¶
To conclude our introduction of Selenium, we’ll show you a few typical Selenium commands. These are probably the most commonly used commands for building test.
open
opens a page using a URL.
click/clickAndWait
performs a click operation, and optionally waits for a new page to load.
verifyTitle/assertTitle
verifies an expected page title.
verifyTextPresent
verifies expected text is somewhere on the page.
verifyElementPresent
verifies an expected UI element, as defined by it’s HTML tag, is present on the page.
verifyText
verifies expected text and it’s corresponding HTML tag are present on the page.
verifyTable
verifies a table’s expected contents.
waitForPageToLoad
pauses execution until an expected new page loads. Called automatically when clickAndWait is used.
waitForElementPresent
pauses execution until an expected UI element, as defined by its HTML tag, in present on the page.
Summary¶
Now that you’ve seen an introduction to Selenium, you’re ready to start writing your first scripts. We recommend beginning with the Selenium IDE and its context-sensitive, right-click, menu. This will allow you to get familiar with the most common Selenium commands quickly, and you can have a simple script done in just a minute or two. Chapter 3 gets you started and then guides you through all the features of the Selenium-IDE.
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics