qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          使用Selenium/Ant做Web應(yīng)用遠(yuǎn)程自動化測試

            Client端主要是通過一個ant build文件來啟動JUnit的TestCase的,進(jìn)而啟動TestCase中的test方法,連接并激活server端進(jìn)行自動化測試。Client端核心測試單元的代碼如下:
          package com.tail.p2test;
          import junit.framework.Test;
          import junit.framework.TestCase;
          import junit.framework.TestSuite;
          import junit.textui.TestRunner;
          import com.thoughtworks.selenium.DefaultSelenium;
          import com.thoughtworks.selenium.Selenium;
          public class DemoTest extends TestCase {
          private Selenium selenium;
          public void setUp() throws Exception {
          String url = "http://localhost:8080/";
          selenium = new DefaultSelenium("localhost", 4444, "*chrome", url);
          selenium.start();
          }
          protected void tearDown() throws Exception {
          selenium.stop();
          }
          public void testNew() throws Exception {
          selenium.setTimeout("100000");
          selenium.open("/login.action");
          selenium.type("username", "admin");
          selenium.type("password", "123");
          selenium.click("http://input[@value='Log In']");
          selenium.waitForPageToLoad("100000");
          Thread.sleep(10000);
          for (int second = 0;; second++) {
          if (second >= 60) fail("timeout");
          try { if (selenium.isElementPresent("signLabel")) break; } catch (Exception e) {}
          Thread.sleep(1000);
          }
          // omit lines
          ...
          selenium.open("/main.action");
          }
          }
           當(dāng)然,應(yīng)用可以直接在Eclipse中運行,但是為了能更加靈活,我們考慮用ant腳本來控制client的運行,這里使用ant腳本的一個好處就是可以很方便快捷的輸出測試報告,在本例中輸出報告的目的就是那個report目錄咯。
            ant的Build.xml的腳本詳細(xì)如下:
          <?xml version="1.0"?>
          <project name="portal" default="junit" basedir=".">
          <property name="source.dir" value="src" />
          <property name="build.dir" value="build" />
          <property name="lib.dir" value="lib" />
          <property name="classes.dir" value="${build.dir}/classes" />
          <property name="report.dir" value="report" />
          <!-- ================================================================== -->
          <!-- C L E A N                                                          -->
          <!-- ================================================================== -->
          <target name="clean">
          <delete dir="${classes.dir}" />
          <mkdir dir="${classes.dir}" />
          <delete dir="${report.dir}" />
          <mkdir dir="${report.dir}" />
          </target>
          <!-- ================================================================== -->
          <!-- C O M P I L E                                                      -->
          <!-- ================================================================== -->
          <target name="compile" depends="clean">
          <!-- local project jars -->
          <patternset id="lib.includes.compile">
          <include name="*.jar" />
          </patternset>
          <fileset dir="${lib.dir}" id="lib.compile">
          <patternset refid="lib.includes.compile" />
          </fileset>
          <pathconvert targetos="windows" property="libs.compile" refid="lib.compile" />
          <!-- compile -->
          <javac srcdir="${source.dir}" destdir="${classes.dir}" classpath="${libs.compile}" includes="**/*.java" debug="true">
          </javac>
          </target>
          <!-- ================================================================== -->
          <!-- J U N I T                                                          -->
          <!-- ================================================================== -->
          <target name="junit" depends="compile">
          <junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
          <classpath>
          <pathelement path="${classes.dir}" />
          <fileset dir="${lib.dir}">
          <include name="**/*.jar" />
          </fileset>
          </classpath>
          <formatter type="xml" />
          <batchtest todir="${report.dir}">
          <fileset dir="${classes.dir}">
          <include name="**/*Test.*" />
          </fileset>
          </batchtest>
          </junit>
          <junitreport todir="${report.dir}">
          <fileset dir="${report.dir}">
          <include name="TEST-*.xml" />
          </fileset>
          <report format="frames" todir="${report.dir}" />
          </junitreport>
          <fail if="tests.failed">
          </fail>
          </target>
          </project>
            以后,你只需要在work目錄下執(zhí)行一個簡單的 ant 命令就能輕松運行整個測試了。

          posted on 2014-06-05 13:40 順其自然EVO 閱讀(327) 評論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄selenium and watir webdrivers 自動化測試學(xué)習(xí)

          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高要市| 黄大仙区| 张掖市| 阿巴嘎旗| 平安县| 沙田区| 金堂县| 永吉县| 宜丰县| 昭苏县| 河津市| 微山县| 镇巴县| 东乡族自治县| 南丹县| 扶绥县| 凤阳县| 尚志市| 临江市| 浮山县| 邯郸市| 丁青县| 昌乐县| 马鞍山市| 正定县| 博白县| 将乐县| 九寨沟县| 和林格尔县| 杭州市| 重庆市| 隆化县| 信丰县| 大足县| 彭水| 凤冈县| 乌鲁木齐县| 襄垣县| 常德市| 乐至县| 祁连县|