Watij——Java开源Web测试工具

Watij——Java开源Web测试工具

1.下载Watij
https://sourceforge.net/project/showfiles.php?group_id=165206下载Watij的发布版本
解压watij_X_x.x.zip

2.安装
jniwrap.dll复制到system32目录下(通常为C:/WINDOWS/system32/
打开你最喜爱的IDE并创建一个工程(我最喜欢的是Intellj
watij.jar以及lib目录下的所有jar包都加入到工程的classpath
创建一个JUnit TestCase,并确保包含这句话“import static watij.finders.SymbolFactory.*;”
试着编写如下示例程序

注意:在/watij_X_xx/src/java-test/watij下可以查看示例程序

Google Search Test Example

import junit.framework.TestCase;
import static watij.finders.SymbolFactory.*;
 
public class GoogleTest extends TestCase {
    public void testGoogleSearch() throws Exception {
        IE ie = new IE();
        ie.start("
http://www.google.com");
        ie.textField(name,"q").set("XWiki");
        ie.button("Google Search").click();
        assertTrue(ie.containsText("/Java wiki engine/"));
    }
}
注意,具体方法可以查询WatijJavadoc API文档。


再介绍些UserGuide中现有的内容

创建一个IE浏览器实例

 IE ie = new IE();
 
启动一个IE浏览器实例

 IE ie = new IE();
 ie.start();
 
启动一个IE浏览器实例并访问Google

 IE ie = new IE();
 ie.start("
http://www.google.com");

使用当前已经打开的IE浏览器

 根据URL指定
 
 IE ie = new  IE();
 ie.attach(url,"
http://www.google.com")
 
 
根据标题指定
 
 IE ie = new IE();
 ie.attach(title,"Google");
 
IE
导航
 ie.goTo("http://www.mysite.com");
IE浏览器访问www.mysite.com
 
 ie.navigate("
http://www.mysite.com");
功能同上
 
 ie.forward();
相当于浏览器中的前进
 
 ie.back();
相当与浏览器中的后退


窗口焦点、位置、大小以及模式的设置

 ie.bringToFront(); //将窗口置于其他浏览器窗口之前
 ie.focus(); //
设定焦点为指定的ie窗口
 
 ie.maximize(); //
将窗口最大化,知道占满整个屏幕
  ie.minimize(); //
将窗口最小化
 ie.restore(); //
将窗口的大小设为上一次的大小。
 
  ie.fullScreen(true); //
打开窗口的全屏幕模式
  ie.fullScreen(false); //
禁止窗口的全屏幕模式
 
  ie.theatreMode(true); //
打开窗口的剧院(Theatre)模式

  ie.theatreMode(false); //
禁止窗口的剧院模式
 
  ie.visible(false); //
将窗口从桌面上隐藏起来
  ie.visible(true); //
恢复窗口可见
 
  ie.left(100); /
将窗口到屏幕的左边距设为100
  ie.top(200); //
将窗口到屏幕的上边距设为200

得到页面中name="button1"的按钮

  ie.button(name,”button1”);
 
得到页面中name="deleteButton"的所有按钮集合

  ie.buttons(name,”deleteButton”);

得到上面这个集合中的第二个元素,即页面中第二个name="deleteButton"的按钮
 ie.buttons(name,”deleteButton”).get(1); 
// OR
  ie.buttons(name,”deleteButton”).button(1);
 
得到页面中name="deleteButton",value="delete"的所有按钮集合中的第三个按钮
  Button button = ie.buttons(name,”deleteButton”).buttons(value,”Delete”).button(2);
 
得到页面中<MYTAG>HTML元素
 
例如<MYTAG>My Text1</MYTAG>
 ie.htmlElement(tag,”MYTAG”);
 
假如有

 <MYTAG myattr1=”attribute1”>My Text1</MYTAG>
  <MYTAG myattr2=”attribute2”>My Text2</MYTAG>
则可以通过如下方式得到第一个元素
  ie.htmlElement(attribute(“myattr1”,"attribute1"));
 
其中htmlElement()中的参数还可以换成以下方式
  tag(String tagName)
  attribute(String name, String value)
  index(int index)
 text(String text)
 name(String value)
 value(String value)
 caption(String value)
 id(String value)
 title(String value)
 alt(String value)
 src(String value)
 action(String value)
 method(String value)
 url(String value)
 href(String value)
 xpath(String expression)
 
超链接
 假如页面中有如下链接
 <a href="googlehttp://google.com">Google</a>
 
则可以通过text,urlxpath三种方式得到该链接,并可以使用正则表达式作为参数,click()表示模拟点击该链接
 ie.link(text,"Google").click();
 
 ie.link(text,"/oogl/").click();
 
 ie.link(url,"
 
多选框
 假如页面中有如下多选框
 Check Me: <input name="checkme" value="1" type="checkbox">
 
则可以通过如下方式来实现勾选该多选框
 ie.checkbox(name,"checkme").set();
 
 ie.checkbox(value,"1").set();
 
 ie.checkbox(xpath, "//INPUT[@name='checkme' and @value='1']").set();
 
还可以调用clear()来取消多选框的选中状态
 
单选框
 假如页面中有如下单选框
 Click Me: <input name="clickme" id="1" type="radio">
 
则可以通过如下方式来实现勾选该多选框
 ie.radio(name,"clickme").set();
 
 ie.radio(id,"1").set();
 
 ie.radio(xpath, "//INPUT[@name='clickme' and @id='1']").set();
 
还可以调用clear()来取消单选框的选中状态
 
按钮
 假如页面中有如下按钮
 <input type = "submit" value = "ClickTheButton" name="b1"></input>
 
可以通过如下方式得到该按钮
 ie.button(name, "b1").click();
 
 ie.button(value,"ClickTheButton").click();
 
默认会根据按钮的value值来寻找该按钮,如
 ie.button("ClickTheButton").click();
 
下拉列表
 假如页面中有如下下拉列表
 <select name = "selectme" > <option name="1">O1<option name="2">O2</select>
 
可以通过如下方式选中该下拉列表中的选项
 //(finds the option in the select list with name 1)
 ie.selectList(name, "selectme").option(name,"1").select();
 
 //(finds the second option in the list)
 ie.selectList(name, "selectme").option(index,1).select();
 
 //(
返回当前下拉列表是否可用,即disable的状态)
 ie.selectList(name, "selectme").disabled();
 
 
可以使用clearSelection()方法清除下拉列表的选中状态。

 
图片
 假如页面中有如下图片元素
 <img src = images/square.jpg id=square title="square_image">
 
可以通过如下方式实现点击该图片
 ie.image(src, "/square/").click();
 
 ie.image(title, "square_image").hasLoaded();
 
 ie.image(id,"square").width();
 
 ie.image(xpath, "//IMAGE[@id='square']").click();
 
文本框和多行文本区域
 
假如页面中有如下文本框
 <input name="username" type="text">
 
可以通过如下方式得到文本框的值,或设置其值
 ie.textField(name, "username").set("myusername");
 
 ie.textField(name, "username").value();

得到子浏览器
 当当前浏览器打开一个新的浏览器窗口时,可以使用如下方法得到新的窗口
 IE ie = new IE().start("

www.mysite.com");
 //lets click a link that causes a new window to popup
 ie.link(name, "mylink").click();
 //now lets get a handle on the child browser;
 IE new_ie = ie.childBrowser();

 

#############################################################################################

#

Watij is a Java API that provides control and automation of Internet Explorer in order to test web applications. It was created out of the need to have a reliable, easy to use, and robust web application testing tool using Java as the scripting language. But why Java?

With Java at your fingertips you have access to a tremendous amount of existing libraries and tools. This means in your test scripts you are not restricted to testing only web applications. For example with Java you can write a test that calls a web service to do some processing and in the same test bring up your web application using Watij and validate that everything is correct. And if your web application is developed in Java, Watij integrates nicely into your already existing environment.

With Watij you can imitate real user interaction with your web application. Simple user actions like navigating, clicking links, filling out forms, and validating content are a breeze with Watij. Watij also makes it easy to handle more complex actions like file downloading and uploading, popup windows and dialogs, and screen captures.

Finding HTML elements on a page can be a cumbersome task using the traditional DOM. To make this easier Watij includes a powerful element finding capability that allows your scripts to find, access, and control any HTML element on the page. It even supports XPath expressions that are lightning fast.

Watij is a standalone Java API that purposely doesn’t force you into using a proprietary testing framework or IDE. Two of the most popular Java testing frameworks are JUnit and TestNG, and Watij integrates seamlessly with either one. Watij is just like any other Java library, so it’s very is easy to import into your favorite Java IDE like Eclipse or IntelliJ. And even though its main use is in testing web applications, because it is a standalone Java API Watij is not limited to the testing paradigm.

To begin you must create an instance of the IE object, but at this point the actual Internet Explorer browser has not been created.

IE ie = new IE();

It’s not until you call start that the instance of Internet Explorer browser appears

IE ie = new IE()
ie.start(); //starts up Internet Explorer

As a convenience you can also provide the url to navigate to

IE ie = new IE()
ie.start("http://www.google.com"); //starts up Internet Explorer and navigates to www.google.com

Watij allows you connect to and control an instance of Internet Explorer that is already open. You can find an IE browser by url or by title.

Attaching by url:

IE ie = new IE()
ie.attach(url,"http://www.google.com"); //connects to the first open browser with the provided url

Attaching by title:

IE ie = new IE()
ie.attach(title,"Google"); //connects to the first open browser with the provided title

Once you are attacted to the target browser, you can now use your IE object as if you started it up yourself.

Several means of navigating Internet Explorer are available to you at any time:

ie.goTo("http://www.mysite.com"); //navigates IE to www.mysite.com
ie.navigate("http://www.mysite.com"); //also navigates IE to www.mysite.com
ie.forward(); //navigates forward one item in the history list
ie.back(); //navigates back one item in the history list

Watij allows you to position your Internet Explorer instance virtually anywhere on the desktop, toggle visual modes, as well as size the window to your needs.

ie.bringToFront(); //brings the Iternet Explorer window to the foreground, in front of every other window
ie.focus(); //gives the window focus
 
ie.maximize(); //maximizes the window to take up the entire desktop scren
ie.minimize(); //minimizes the window to the desktop taskbar
ie.restore(); //restores the window to its previous positioned state
 
ie.fullScreen(true); //turns the window's full screen mode on
ie.fullScreen(false); //turns the window's screen mode back to normal
 
ie.theatreMode(true); //turns the window's theatre mode on
ie.theatreMode(false); //turns the window's screen mode back to normal
 
ie.visible(false); //hides the window from being seen on the desktop
ie.visible(true); //make the window visible on the desktop
 
ie.left(100); //sets the screen coordinate of the left edge of the window to 100
ie.top(200); //sets the screen coordinate of the top edge of the window to 200

Finding HTML elements on the page is handled through Watij Symbols, Finders, and XPath Expressions.

Watij brought over from Watir (Web Application Testing in Ruby) the concept of using Symbols to specify what to search by. The following code uses the Symbol name to search for a button with the attribute name=”button1”.

ie.button(name,”button1”);

Every HTML element modeled in Watij provides the singular form and the plural form of the object. So the object Button represents a single button, and the object Buttons represents a collection of Buttons.

Using Symbols we can find a collection of all the HTML elements on the page with specific properties.

ie.buttons(name,”deleteButton”); //returns all the HTML buttons on the page with the name=”deleteButton”

You can also access an HTML element in a returned collection by index. Indexes in Watij are zero-based, so the first element is at index 0.

ie.buttons(name,”deleteButton”).get(1);  //returns the 2nd Button in the collection
// OR
ie.buttons(name,”deleteButton”).button(1); //returns the 2nd Button in the collection

You are not limited to the amount of filtering using Symbols, so anything is possible:

Button button = ie.buttons(name,”deleteButton”).buttons(value,”Delete”).button(2); //finds the 3 Button with value=”Delete” and name=”deleteButton”

Watij also provides a generic object named HtmlElement that will allow you to access any other Html element not modeled in the API. For example if you have the following html:

<MYTAG>My Text1</MYTAG>

One way of accessing MYTAG is with the following code:

ie.htmlElement(tag,”MYTAG”);

To better equip you with the ability to access any element on the page in a simple easy to manner, Watij introduced a new concept called Finders. Finders are like Symbols except they are not limited by a single parameter. In fact later on you will see that the sky is the limit when it comes to Finders, especially when creating your own custom ones. For now let’s say you have the following html:

<MYTAG myattr1=”attribute1”>My Text1</MYTAG>
<MYTAG myattr2=”attribute2”>My Text2</MYTAG>

By using the Finder named attribute which takes in a name and value you can do the following:

ie.htmlElement(attribute(“myattr1”,"attribute1")); //returns the HtmlElement with myattr1="attribute1"

The following Finder objects are accessible via:

import static watij.finders.FinderFactory.*;

tag(String tagName)
attribute(String name, String value)
index(int index)
text(String text)
name(String value)
value(String value)
caption(String value)
id(String value)
title(String value)
alt(String value)
src(String value)
action(String value)
method(String value)
url(String value)
href(String value)
xpath(String expression)

Your browser would disply a link to Google as Google. The actual HTML would look something like:

<a href="http://google.com">Google</a>

There are many ways to have Watij click the above link to Google. You can find and click the link using the text or url attribute, which both also accept regular expressions. You can also use xpath to find the link.

ie.link(text,"Google").click();
 
ie.link(text,"/oogl/").click();
 
ie.link(url,"http://google.com").click();
 
ie.link(url,"/google.com/").click();
 
ie.link(xpath, "//A[@url='http://google.com']").click();

See the Watij API for a complete list of methods available for links

Watij sets or clears checkboxes based on the name and value attributes provided in the checkbox HTML tag. The HTML of a check box looks like:

Check Me: <input name="checkme" value="1" type="checkbox">

We can search for this checkbox using our Symbols

ie.checkbox(name,"checkme").set();
 
ie.checkbox(value,"1").set();
 
ie.checkbox(xpath, "//INPUT[@name='checkme' and @value='1']").set();

We can also use the clear method on a checkbox. See the Watij API for a complete list of methods available for checkboxes

Watij sets or clears radios based on the name and value attributes provided in the radio HTML tag. The HTML code of a radio looks like:

Click Me: <input name="clickme" id="1" type="radio">

We can search for this radio using our Symbols

ie.radio(name,"clickme").set();
 
ie.radio(id,"1").set();
 
ie.radio(xpath, "//INPUT[@name='clickme' and @id='1']").set();

We can also use the clear method on a radio. See the Watij API for a complete list of methods available for radios

Watij can submit HTML buttons on a web page based on the name, value (caption) and id attributes. The HTML code of a button looks like:

<input type = "submit" value = "ClickTheButton" name="b1"></input>

We can search for this select box using our Symbols

ie.button(name, "b1").click();
 
ie.button(value,"ClickTheButton").click();

The default way to find a button is through its value. Because of this, there is a simple way to find a button:

ie.button("ClickTheButton").click();

See the Watij API for a complete list of methods available for buttons

Watij selects an item in a select list (or dropdown box) based on the name and option value attributes provided in the select HTML tag. Or we can identify it based on the name and attribute, and the value we want to set. The HTML code of a SelectList looks like:

<select name = "selectme" > <option name="1">O1<option name="2">O2</select>

We can search for this select list using our *Symbols*

//(finds the option in the select list with name 1)
ie.selectList(name, "selectme").option(name,"1").select(); 
 
//(finds the second option in the list)
ie.selectList(name, "selectme").option(index,1).select(); 
 
//(returns whether the select list is disabled)
ie.selectList(name, "selectme").disabled();

You can also clear a select list using the clearSelection() method. See the Watij API for a complete list of methods available for select boxes

Watij clicks images based on the name, id, src, alt and other attributes provided in the image HTML tag. The HTML code of a image looks like:

<img src = images/square.jpg id=square title="square_image">

We can search for this select box using our Symbols

ie.image(src, "/square/").click();
 
ie.image(title, "square_image").hasLoaded();
 
ie.image(id,"square").width();
 
ie.image(xpath, "//IMAGE[@id='square']").click();

See the Watij API for a complete list of methods available for images

Watij sets textfields and textareas based on the name and id and other attributes provided in the HTML tag. The HTML code of a textfield looks like:

<input name="username" type="text">

We can search for this textfield using our Symbols

ie.textField(name, "username").set("myusername");
 
ie.textField(name, "username").value();

See the Watij API for a complete list of methods available for textfields and textareas

Watij supports attaching to child browsers that are created from a parent window. For example, you might have a link that creates a new popup window. This is easily handled in watij:

IE ie = new IE().start("www.mysite.com");
//lets click a link that causes a new window to popup
ie.link(name, "mylink").click();
//now lets get a handle on the child browser;
IE new_ie = ie.childBrowser();

Wasn’t that easy! You can also get the child browser at a specific index away from the parent window. The third child browser would be ie.childBrowser(2). See the Watij API for a complete list of methods available for browsers

Watij supports many methods for validation. You may check to see if an object is disabled, enabled, isSet, readOnly, or any other available element attribute. When writing Watij tests, you extend Junit, so you have the full capability for validation that JUnit and Java have to offer. I would highly suggest looking at the Watij unit tests to get a deeper understanding of the validation capabilities of Watij, JUnit, and Java. The Unit Tests are a great resource - and they all work, too!!

Watij is packaged with a preconfigured version of BeanShell. You can always run Beanshell and just start using Watij to explore an automation idea.     

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值