用
Selenium
和
Eclipse IDE
实现自动化的网络流量【译文】
URL: http://blog.testsautomation.com/2009/11/automating-web-flow-with-selenium-and-eclipse-ide/
译者:
Rita
原作者:
Waldemar
也许你已经很了解
Selenium
了,它是个很基础的免费开源网络应用功能测试工具,而且它是被大部分人认为可以替代
QTP
的一款测试工具。我最近发现
Selenium
对于负载测试自动化数据生成很有帮助。
下面是对如何设置
Eclipse
和通过它运行
Selenium
脚本的简短介绍。
1>
下载
Eclipse IDE
地址:
http://www.eclipse.org/downloads/
2>
下载
Selenium RC
地址:
http://seleniumhq.org/download/
3>
下载
Junit
地址:
http://www.junit.org/
4>
在
Eclipse
中创建一个新项目:菜单选择
File
à
New
à
Projece
à
JavaProjece
,然后输入一个项目名字如
“Sample”
,点击
“Finish”
按钮创建完毕。
5>
导入
Selenium
和
JUnit
包:在
Package Explorer
标签页中右键单击项目名字
“sample”
选择
“Properties”,
点击左侧
“Java Build Path”
,选择
“Libraries”
标签,点击右侧
“Add External JARs”
并导入
junit-4.7.jar
和
selenium-java-client-driver.jar
两个包。
现在我们的
Eclipse
环境已经创建好了。下一步准备在
Selenium IDE
中的测试用例,为此我已经准备好了一个小测试,是在
google
中查询
”linux”
一词。在
Selenium IDE
的
“option”
中,依次选择
Format -> Java (Junit) Selenium RC
。开始用
Selenium IDE
录制的脚本是类似这样的:
package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class Untitled extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");
}
public void testUntitled() throws Exception {
selenium.open("/");
selenium.type("q", "linux");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
}
}
默认在
Selenium
中生成的
Junit
代码不能用于
Selenium RC
,正因如此,我更新了一下代码,使其连接到本地
Selenium RC
的
4444
端口,更新代码如下:
package
com.example.tests;
import
com.thoughtworks.selenium.*;
import
java.util.regex.Pattern
;
public
class
Untitled
extends
SeleneseTestCase {
public
DefaultSelenium
selenium
;
public
void
setUp()
throws
Exception {
selenium
=
new
DefaultSelenium(
"localhost"
, 4444,
"*iexplore"
,
"http://www.google.com/"
);
selenium
.start();
}
public
void
testUntitled()
throws
Exception {
selenium
.open(
"/"
);
selenium
.type(
"q"
,
"linux"
);
selenium
.click(
"btnG"
);
selenium
.waitForPageToLoad(
"30000"
);
}
}
接下来我们把它加入到
Eclipse
项目中:在
Package Explorer
标签页中右键单击项目名字,选择
New -> Class
,输入类名
“Untitled_linux
”
和
package name “com.example.tests”
,点击
“Finish”
按钮完成。
在运行我们的测试前,还需要开启
Selenium RC
。从系统的命令行进入目录:
selenium-remote-control-1.0.1/selenium-server-1.0.1
,运行命令
“java -jar selenium-server.jar”
。它将在
4444
端口运行
Selenium RC
。
现在开始运行测试(Rita
注:把在
Selenium
中录制或更新的脚本代码
copy
到
Eclipse
的类“
Untitled_linux
”中,见下图
)
,选择菜单
Run -> Run As -> Junit test
即可(或者直接点击运行按钮)。运行后将打开两个
IE
窗口,一个是
Selenium RC
窗口,另一个是
Google
搜索
”linux”
的结果窗口。
(Rita
注:运行前一定要确保在
Eclipse
中的类名和测试脚本的类名一致,否则运行会失败。如我之前创建的类名为
“Untitled_linux
”
,故要确保测试脚本中“
public
class
Untitled_linux
extends
SeleneseTestCase
”的类名一致
)
因为我们运行了
Java
代码,在此仅仅提到如何想去运行测试。下面是我更新了上面的例子的代码,改为
5
秒间隔循环搜索
5
次:
package
com.example.tests;
import
com.thoughtworks.selenium.*;
import
java.util.regex.Pattern
;
public
class
Untitled_linux
extends
SeleneseTestCase {
public
DefaultSelenium
selenium
;
public
void
setUp()
throws
Exception {
selenium
=
new
DefaultSelenium(
"localhost"
, 4444,
"*iexplore"
,
"http://www.google.com/"
);
selenium
.start();
}
public
void
testUntitled()
throws
Exception {
for
(
int
i = 0; i < 5; i++)
{
selenium
.open(
"/"
);
selenium
.type(
"q"
,
"linux"
);
selenium
.click(
"btnG"
);
selenium
.waitForPageToLoad(
"30000"
);
Thread.sleep
(5000);
}
}
}
最后,运行成功后命令行窗口会显示如下信息:
Eclipse
中会显示如下运行成功的界面:
分享到:
相关推荐
西门子 plc Hans Berger Automating with 系列丛书 Automating with SIMATIC Controllers Hardware and Software,Configuration and Programming,Data Communication, Operator Control and Process Monitoring
Automating.ActionScript.Projects.with.Eclipse.and.Ant(2011.9)
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
Introducing Microsoft Flow Automating Workflows Between Apps and Services 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Automating Microsoft Azure with Powershell 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
Automating Manufacturing Systems with PLCs V7.0 2010年版
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
Learn to use PhantomJS and CasperJS to automate your interaction with the web to perform numerous tasks such as data scraping, network monitoring, page rendering, and browser testing in a programmatic...
Automating Inference, Learning, and Design using Probabilistic Programming
西门子 plc Hans Berger Automating with 系列丛书 Automating with SIMATICS 7-1500 Configuring, Programming and Testingwith STEP 7 Professional 2017.pdf
西门子 plc Hans Berger Automating with 系列丛书 Automating with SIMATIC S7-1500_ Configuring, Programming and Testing with STEP 7 Professional-Publicis 2014)
With this book, you will learn how to deal with many tasks such as zipping files, emailing colleagues, and deploying your work at the press of a button. The book is structured into two parts: The ...
西门子 plc Hans Berger Automating with 系列丛书
Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well. ...
Automating with STEP_7_in_LAD_and_FB.pdf 西门子 plc Hans Berger Automating with 系列丛书
The Way of the Web Tester is packed with cartoons, graphics, best practices, war stories, plenty of humor, and hands-on tutorial exercises that will get you doing the right things, the right way. ...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
It is widely used around the world as a tool for automating test for web application. In this book we use Selenium WebDriver to perform automatic operation of the browser. As for browser we use both ...
Techniques for automating continuous integration with AntHill and Cruise Control How to harness plugins for JUnit, Cactus, and Ant in the Eclipse IDE Ways to implement Extreme Programming best ...