安裝:php
Eclipse:Help -> Software Updates -> Find and Install -> Search for new features to installhtml
New Remote Sitegit
Name:TestNGgithub
URL: http://beust.com/eclipse編程
目錄瀏覽器
1.如何利用參數使用TestNG的配置方法session
2.怎麼配置你的測試框架
3.爲TestNG建立XML文件eclipse
4.使用Eclipse開始你的測試wordpress
5.如何讓將來的測試設計更好
/**開始**/
創建你的測試用例
在寫測試用例以前,你須要知道如何驗證,或者什麼將被驗證。讓咱們用wordpress 的「create new post」寫一個測試用例。
1.打開網頁 http://demo.opensourcecms.com/wordpress/wp-login.php
2.在用戶名處輸入‘admin’
3.在密碼處輸入demo123
4.點擊「log In」按鈕
5.確認文字「Howdy,admin」存在
6.點擊「Posts」連接
7.點擊「Add New」按鈕
8.在標題區域輸入「Selenium Demo Post」
9.點擊「Publish」按鈕
10.確認文字「Post published」存在
考慮這個情景,想到的第一件事情是按照步驟建立一個很長的測試用例,對於手動測試,這多是一個不錯的測試用例,然而,因爲咱們寫的是自動化用例,咱們想要咱們的腳本竟可能在將來的情景中能夠重用。
如下是我如何分解這個測試:
1.打開workpress的站點
2.打開admin登錄頁
3.輸入登錄數據
4.引導去寫post頁面
5.寫一個post
6.發佈這個post
7.確認發佈成功
記住這只是一個例子,你能夠設計任何你想要的測試,只要他們又自身的商業價值並證實你的價值
讓咱們來看看Java代碼如何實現:
1 @Test(description="Launches the WordPress site") 2 public void launchSite(){ 3 selenium.open(""); 4 selenium.waitForPageToLoad("30000"); 5 assertEquals(selenium.getTitle(), "Demo | Just another WordPress site"); 6 } 7 8 @Test(description="Navigates to the admin page") 9 public void openAdminPage() { 10 selenium.open("wp-admin"); 11 selenium.waitForPageToLoad("30000"); 12 assertEquals(selenium.getTitle(), "Demo 鈥� Log In"); 13 } 14 15 @Test(description="Enters valid login data") 16 public void loginAsAdmin() { 17 selenium.type("user_login", "admin"); 18 selenium.type("user_pass", "demo123"); 19 selenium.click("wp-submit"); 20 selenium.waitForPageToLoad("30000"); 21 assertTrue(selenium.isTextPresent("Howdy, admin")); 22 } 23 24 @Test(description="Navigates to the New Post screen") 25 public void navigateNewPost() { 26 selenium.click("//a[contains(text(),'Posts')]/following::a[contains(text(),'Add New')][1]"); 27 selenium.waitForPageToLoad("30000"); 28 assertTrue(selenium.isTextPresent("Add New Post")); 29 } 30 31 @Test(description="Writes the new post") 32 public void writeBlogPost() { 33 selenium.type("title", "New Blog Post"); 34 selenium.click("edButtonHTML"); 35 selenium.type("content", "This is a new post"); 36 //TODO:Assert 37 } 38 39 @Test(description="Publishes the post") 40 public void publishBlogPost() { 41 selenium.click("submitdiv"); 42 selenium.click("publish"); 43 selenium.waitForPageToLoad("30000"); 44 assertTrue(selenium.isTextPresent("Post published.")); 45 } 46 47 @Test(description="Verifies the post") 48 public void verifyBlogPost() { 49 selenium.click("//a[contains(text(),'Posts') and contains(@class,'wp-first-item')]"); 50 selenium.waitForPageToLoad("30000"); 51 assertTrue(selenium.isElementPresent("//a[text()='New Blog Post']")); 52 } 53 54 @Test(description="Logs out") 55 public void logout() { 56 selenium.click("//a[text()='Log Out']"); 57 //TODO:Assert 58 }
咱們將使用這些測試方法(或者測試步驟)
配置方法
若是你熟悉unit測試框架,你可能知道setup、及teardown方法。TestNG將超越這種想法,並容許你在你的測試用例集、測試組或者 測試方法以前及以後定義這類方法,這對於selenium測試時很是有利的,你能夠在你的測試集以前建立selenium服務及打開瀏覽器。
爲了作到這點,咱們將使用2個TestNG的註釋符:@BeforeSuite 和 @AfterSuite:
@BeforeSuite(alwaysRun = true) public void setupBeforeSuite(ITestContext context) { String seleniumHost = context.getCurrentXmlTest().getParameter("selenium.host"); String seleniumPort = context.getCurrentXmlTest().getParameter("selenium.port"); String seleniumBrowser = context.getCurrentXmlTest().getParameter("selenium.browser"); String seleniumUrl = context.getCurrentXmlTest().getParameter("selenium.url"); RemoteControlConfiguration rcc = new RemoteControlConfiguration(); rcc.setSingleWindow(true); rcc.setPort(Integer.parseInt(seleniumPort)); try { server = new SeleniumServer(false, rcc); server.boot(); } catch (Exception e) { throw new IllegalStateException("Can't start selenium server", e); } proc = new HttpCommandProcessor(seleniumHost, Integer.parseInt(seleniumPort), seleniumBrowser, seleniumUrl); selenium = new DefaultSelenium(proc); selenium.start(); } @AfterSuite(alwaysRun = true) public void setupAfterSuite() { selenium.stop(); server.stop(); }
PS:你有沒有注意到怪異的參數?他們被儲存在XML文件中(咱們將在下一部分看到這個)
經過加入這些註釋符,TestNG引擎將自動在測試先後喚起這些方法(請確保這些測試用例被@test 註釋符標識),啓動和初始化selenium內容只有一次,在測試中相同的瀏覽器session將被複用
建立XML文件
爲了定義測試的順序,咱們須要建立XML文件並列出咱們想要的運行測試方法的順序。確保這些測試用例被@test 註釋符標識,不然TestNG引擎將不會執行他們。
在TestNG5.13.1以前,你不得不用Method Interceptors 若是你在XML中定義想順序執行用例,具體能夠參加做者的例子http://gist.github.com/416310,從5.13.1後,你只須要 增長參數:preserve-order在你的測試標籤,幷包含你想要運行的用例,減小你測試集中沒必要要的代碼
如下是XML文件:
<suite name="Knorrium.info - Wordpress Demo" verbose="10"> <parameter name="selenium.host" value="localhost" /> <parameter name="selenium.port" value="3737" /> <parameter name="selenium.browser" value="*firefox" /> <parameter name="selenium.url" value="http://demo.opensourcecms.com/wordpress/" /> <test name="Write new post" preserve-order="true"> <classes> <class name="test.Wordpress"> <methods> <include name="launchSite" /> <include name="openAdminPage" /> <include name="loginAsAdmin" /> <include name="navigateNewPost" /> <include name="writeBlogPost" /> <include name="publishBlogPost" /> <include name="verifyBlogPost" /> </methods> </class> </classes> </test> </suite>
在Eclipse中啓動你的測試
咱們完成了測試的編寫後,怎麼運行他們呢?
你能啓動從命令行啓動TestNg、使用Eclipse插件或程序化。咱們將演示使用Eclipse插件,按照步驟操做便可,具體可見TestNG的官方文檔:http://testng.org/doc/download.html
若是你已經裝了TestNG,你將會看到右擊XML文件後有選項(RunAs-TestNG Suite)
點擊「RunAs-TestNG Suite」就會開始你的測試,而且你將會看到漂亮的結果樹
對將來的思考
若是你想對你的測試集將來思考的更多,我會推薦你閱讀 Adam Goucher’s article 在PragPub上的,他談論了selenium2 和 Page Objects模式(一種當你使用selenium2時很是好的模式)
因爲還有不少人使用selenium1,因此這片文章還會存在,但最終會被selenium2替代
隨着測試數量在測試集中的增加,你會發現他們在不一樣的測試類分組是個好主意。若是你這樣作,你能夠利用面向對象的編程方式和例如建立一個新類命名BaseTest,並留下您的配置邏輯在那裏。這樣,每個測試類必須繼承BaseTest類並使用靜態屬性。
public class WordPressAdmin extends BaseTest { @Test public void test1(){ selenium.open(""); //... } @Test public void test2(){ selenium.open(""); //... } }