Javadoc API for Watij:

http://watij.sourceforge.net/docs/api/index.html?watij/IE.html

第三章 Watij环境设置
3.1 Watij环境要求
l         Watij 3.2.1

l         JDK 1.5

l         Java IDE(本文中使用Eclipse)

3.2 Watij安装及设置
l         下载软件包

Watij 3.2.1:

下载地址:http://sourceforge.net/project/showfiles.php?group_id=165206

 JDK 1.5或以上:

下载地址:http://java.sun.com/javase/downloads/index_jdk5.jsp

安装&配置

1. 安装JDK1.5;

2.解压watij_release_3.2.1.zip;

3.拷贝watij_release_3.2.1\ jniwrap.dll到C:\windows\system32\;

4.配置Eclipse的编译器为JDK1.5;

依次打开Window->Preferences->Java->Compiler,设置“Compiler compliance level”为5.0

新建工程

依次打开New->Project,选择“Java Project”,点击“Next”

“Project Name”中输入“Watij”,点击“Next”

在“Java Settings”中选择“Libraries”选项卡,点击“Add External JARs”,

添加“watij.jar”和lib目录下的所有jar,点击“Finish”

第四章 Watij for google seacher Example
这部分主要描述如何打开IE浏览器、IE导航、IE窗口的设置与调整。

本次Web自动化测试的例子使用LoadRunner中自带的HP Web Tours。

1. 打开Eclipse,新建一个“Junit Test Case”,输入“Name”为“SearcherTest”,点击“Finish”

2. 输入以下代码:

import junit.framework.TestCase;

import watij.runtime.ie.IE;

import static watij.finders.SymbolFactory.*;

public class SearcherTest extends TestCase {

    public void testSearcher () throws Exception {

       IE ie = new IE();  

       //打开 IE 浏览器

       ie.start(http://www.google.com);

   ie.textfield(name,"q").set("XWiki");

   ie.button("google search").click();

   assertTrue(ie.containsText("/java Wiki engine/"));

    }

}

4.       运行:输入以上代码并保存后,选择“Run as”->“JUnit Test”

5.       执行结果:执行过程没有错误时,JUnit执行状态显示绿色, 并且打开google的查询页面

6.       代码解释:

1)       在打开IE浏览器之前,需要实例化一个IE对象:

IE ie = new IE();
 
2)       使用start方法,打开新的IE浏览器:

IE ie = new IE();

ie.start();

也可以打开浏览器后转到某个URL:

IE ie = new IE();

ie.start("http://google.com");
 
3)       IE导航的几种方法:

ie.goTo("http://www.baidu.com"); //转到www.baidu.com

ie.navigate("http://www.google.com"); //转到www.google.com,同goTo

ie.forward(); //转到历史列表中的前一个地址

ie.back(); //转到历史列表中的后一个地址
 

 4)       IE窗口设置、调整:

ie.bringToFront(); //将IE窗口置为最前

ie.focus(); //设置为当前焦点 

ie.maximize(); //最大化窗口

ie.minimize(); //最小化窗口

ie.restore(); //重置窗口为之前的状态   

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); //隐藏窗口

ie.visible(true); //显示窗口     

ie.left(100); //调整窗口位置

ie.top(200); //调整窗口位置

4.1.2 用户登录测试
模拟用户输入;登录成功后,验证提示信息及HTML元素数量。

启动WEB服务器及新建JUnit Test Case步骤(省略)

1. 输入以下代码:

import watij.runtime.ie.IE;

import junit.framework.TestCase;

import static watij.finders.SymbolFactory.*;

public class LoginWithRightUsernamePassword extends TestCase {

    public void testLoginWebTours () throws Exception {

        int button_id;

        int button_numbers = 4;

        String[] button_src = new String[button_numbers];   

        IE ie = new IE();

        ie.start();

        ie.bringToFront();

        ie.goTo("http://127.0.0.1:1080/WebTours");    

        ie.frame("body").frame("navbar").textField(name, "username").set("jojo");

        ie.frame("body").frame("navbar").textField(name, "password").set("bean");

        ie.frame("body").frame("navbar").button(value, "Login").click();    

        // Verify user "jojo" has logged in and four buttons displayed

        assertTrue( ie.frame("body").frame("info").containsText("jojo") );    

        for (button_id=0; button_id<button_numbers; button_id++) {

            button_src[button_id] =                 ie.frame("body").frame("navbar").images().get(button_id).src();

        }

       

        // Capture current Screen

        ie.focus();

        ie.maximize();

       ie.screenCapture("d:\\login_PASSED.png");

    }

}
 


2.代码解释:

1)       输入用户名、密码:

Watij根据HTML标签提供的文字输入列的name、id或其他属性来设置其值。textfield的HTML代码形如:

<input type=text name=username value='' size=14 maxlength=14>

<input type=password name=password value='' size=14 maxlength=14>
 


输入用户名、密码,可以使用如下方法:

ie.textField(name, “username”).set(“jojo”);

ie.textField(name, “password”).set(“bean”);
 
  获取某个textfield的值,使用:

ie.textField(name, “username”).get();

ie.textField(name, “password”).get();
 

2)       点击Login,登录:

Watij中button支持的HTML对象包括:<input> 标签type为image、submit、button或reset

<input type=image name=login value=Login alt=Login border=1

src='/WebTours/images/mer_login.gif'>

<input type="submit" id="SubmitLoginBTN" value="登录(L)" accesskey="L" class="loginSubmit" />

<input type="button" value="登录" name="btnSubmit">

  使用button的click方法来模拟用户点击操作:

ie.button(value, “Login”).click();

ie.button(id, “SubmitLoginBTN”).click();

ie.button(name, “btnSubmit”).click();
 
3)       验证用户是否已经登录:

验证该用户是否已经登录,可以通过提示信息是否包含该用户的名字以及左边框架中包含四个buttons来验证。验证HTML body中是否包含某些文本,使用如下方法:

ie.containsText(“jojo”);

它的返回值是True或False,可以使用断言来验证该用户是否已登录:

assertTrue( ie.containsText(“jojo”) );
 

4)   截图:

最后,要保存一下测试结果,即将登录后窗口截图。如下代码,会将当前屏幕截图保存到d:\login_PASSED.png。

ie.screenCapture(“d:\\login_PASSED.png”);

 

posted on 2010-09-23 11:25  随性生活  阅读(833)  评论(0编辑  收藏  举报