使用Selenium/Ant做Web應用遠端自動化測試
Client端主要是通過一個ant build檔案來啟動JUnit的TestCase的,進而啟動TestCase中的test方法,連線並啟用server端進行自動化測試。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("//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");
- }
- }
當然,應用可以直接在Eclipse中執行,但是為了能更加靈活,我們考慮用ant指令碼來控制client的執行,這裡使用ant指令碼的一個好處就是可以很方便快捷的輸出測試報告,在本例中輸出報告的目的就是那個report目錄咯。
ant的Build.xml的指令碼詳細如下:
- <?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>
相關文章
- Web自動化-Selenium自動化測試-4-編寫測試用例Web
- 利用 Selenium 自動化 web 測試Web
- JMeter做WEB和API自動化測試JMeterWebAPI
- selenium java自動化測試Java
- Selenium自動化測試框架框架
- 自動化測試框架Selenium的使用——安裝Selenium框架
- 關於Web端-UI自動化測試WebUI
- Selenium+Java+TestNG進行web自動化測試JavaWeb
- Web自動化-Selenium自動化測試-1-主要學習計劃Web
- 使用 RFT 設計 Web 2.0 應用的 GUI 自動化測試框架WebGUI框架
- 用python實現selenium 自動化測試Python
- 使用Selenium自動化測試SAP CRM WebClient UIWebclientUI
- Web自動化測試 五 ----- selenium的等待和切換Web
- Web自動化測試 環境搭建(selenium+python)WebPython
- Selenium自動化測試網頁網頁
- java+selenium 自動化測試Java
- selenium自動化測試面試集合面試
- Ant+junit的測試自動化 (轉)
- Web自動化測試 —— 測試環境搭建 (Selenium+Python) Windows篇WebPythonWindows
- 基於Selenium+Python的web自動化測試框架PythonWeb框架
- python自動化測試工具selenium使用指南Python
- 加速Web自動化測試Web
- Selenium用法詳解 - - selenium自動化測試概要
- 自動化測試 selenium 環境搭建
- 自動化測試框架Selenium入門框架
- selenium+python自動化測試Python
- 「UI 測試自動化selenium」彙總UI
- Jmeter+Ant+Python 介面自動化測試JMeterPython
- python3 selenium web自動化測試之excel引數化PythonWebExcel
- Selenium Web Driver自動化測試(java版)系列下半部分(37) - 關鍵字驅動自動化測試框架(2)-測試過程...WebJava框架
- 怎樣開始用selenium進行自動化測試?
- Flutter應用進行自動化測試Flutter
- python 桌面應用自動化測試Python
- 自動化測試進階課程——Selenium自動化測試通關實戰班
- webdriver+ant+jenkins自動化測試實踐WebJenkins
- Selenium 自動化測試從零實戰
- selenium(web端)淘寶測試程式碼Web
- web的自動化測試外掛Selenium-IDE外掛的安裝使用WebIDE