功能自动化工具watiJ(转载)
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”);
?
轉載于:https://www.cnblogs.com/keeping/archive/2010/09/23/1833303.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的功能自动化工具watiJ(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是DMZ
- 下一篇: 趋势不能deploy的解决方法