开始– 选择你自己的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.
分享到:
相关推荐
selenium-java-basics
If you are a software tester or a developer who has learnt the basics of Selenium using the WebDriver API and is now ready to take the next step, then this is the book for you. What You Will Learn ...
Selenium基础 这是如何将Selenium WebDriver与Page Object模式一起使用的基本示例,适用于那些开始对网站进行自动测试的用户 贝娄,应配置为运行此项目的列表 玛文 如果您熟悉Java世界,这对您来说并不奇怪。 ...
Selenium基础
SeleniumSelenium WebDriver(基础+高级+建筑师)
WebDriver_TheInternet_Basics 使用 Dave Haeffer 的互联网登录页面进行 Selenium 测试的快速而肮脏的示例。 这是重构示例中代码的粗略草稿。 该网站可以在上找到。 在src / test / java中,有一个包含所有内容...
网络自动化基础使用python和selenium进行Web自动化的基本步骤在此处找到以下教程 强烈推荐给需要快速提高Web自动化效率的任何人
In the process, you’ll learn the basics of Django, Selenium, Git, jQuery, and Mock, along with current web development techniques. If you’re ready to take your Python skills to the next level, this ...
Java Web Developer ...名为udacity-jwdnd-c1-l5-junit-and-selenium-master的练习解决方案目录与教室第2课第6课中的udacity-jwdnd-c1-l5-junit-and-selenium-masterSelenium概念相关联。 udacity-jwdnd-c1-snippet
Beginning with the basics of PhantomJS, this book will dive into its core modules and guide you through how to solve real-world testing problems. This book explores a variety of test automation tasks,...
基础_Python Python自动化的样本主题此文件包含使用Selenium的Python自动化的所有基本主题...。也可以从Pycharm更新此文件
In the process, you’ll learn the basics of Django, Selenium, Git, jQuery, and Mock, along with current web development techniques. If you’re ready to take your Python skills to the next level, this ...
You’ll determine when and how to scrape data from a JavaScript-dependent website using PyQt and Selenium. You’ll get a better understanding of how to submit forms on complex websites protected by ...
Along with a quick Python primer, they cover Selenium for JavaScript-heavy sites, and web crawling in detail. The book finishes with a recap of best practices and a collection of examples that bring ...
from scratch - to teach the TDD metholology, and how it applies to web programming, from the basics of database integration and javascript, going via browser-automation tools like Selenium, to ...
Practical Web Scraping for Data Science: ... Readers unfamiliar with Python will appreciate a quick Python primer in chapter 1 to catch up with the basics and provide pointers to other guides as well.
This book explores Python’s built-in ability to run code found embedded in doc strings and also plugging in to popular web testing tools like Selenium. By the end of this book, you will be ...
Start with the basics, then enhance your knowledge with in-depth discussions on language features, third-party libraries, design patterns and more Practical examples that show how to use TypeScript ...
We'll start off by ensuring you have a solid understanding the basics of HBase, followed by giving you a thorough explanation of architecting a HBase cluster as per our project specifications....