首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 开源 FAQ 第二书店 博文视点 程序员
频道: 研发 数据库 中间件 信息化 视频 .NET Java 游戏 移动 服务: 人才 外包 培训
    图书品种:235680
       
热门搜索: ASP.NET Ajax Spring Hibernate Java

6.8  示例程序:ScriptBasedUITest

这个程序综合了本章所讲到的几种技术,创建了一个轻量级的自动化测试套件,用以测试如图6-1所示的右边frame里的ASP.NET Web页面程序。这个自动化测试程序有三个文件组成。第一个文件WebForm.aspx,在本章的导读部分已有介绍。它是一个简单的客户机-服务器模式的计算器演示程序。第二个文件是WebAuto.html。这个HTML文件有两个frame,一个包含待测Web程序,另一个包含测试程序代码。下面是WebAuto.html的代码:

<html>

  <head>

    <script language="JavaScript">

    var description = "Demo Test Scenario";

    var whenRun = new Date();

    var loadCount = 0;

    var pass = true;

    </script>

  </head>

  <frameset cols="40%,*">

    <frame src="TestCode.html" name="leftFrame">

    <frame src="../TheWebApp/WebApp.aspx" name="rightFrame"

onload="leftFrame.updateState();">

  </frameset>

</html>

本测试场景的第三个文件叫作TestCode.html。这个HTML页面包含一系列用JavaScript编写的测试程序代码。这个页面的代码见清单6-1。测试程序的时候,输出信息如本章导读部分图6-1所示。本节的代码假定自动化测试套件以如下方式进行组织:有一个根目录文件夹包含两个子文件夹,分别是TheWebApp和TestAutomation。TheWebApp文件夹包含待测Web程序(WebApp.aspx)。TestAutomation文件夹包含主测试套件的结构代码文件(WebAuto.html),页面TestCode.html也位于这个文件夹里,这个页面包含用于运行测试场景的JavaScript代码。

清单6-1  Test Harness File TestCode.html

<html>

  <head>

    <script language="JavaScript">

      function updateState()

      {

        parent.loadCount++;

          if (parent.loadCount > 1)

          runTest();

      } // updateState()

      function runTest()

      {

      try {

        if (parent.loadCount == 1)

        {

          logRemark("Setting TextBoxes to '7' and '5'");

          logRemark("Selecting RadioButton1");

          logRemark("Clicking Calculate button");

          parent.rightFrame.document.theForm.TextBox1.value = "7";

          parent.rightFrame.document.theForm.TextBox2.value = "5";

          parent.rightFrame.document.all["RadioButton1"].checked = true;

          parent.rightFrame.document.theForm.Button1.click();

        }

        else if (parent.loadCount == 2)

        {

          logRemark("Verifying '12.0000'");

          verify("TextBox3", "12.0000");

          logRemark("Selecting RadioButton2");

          logRemark("Clicking Calculate button");

          parent.rightFrame.document.all["RadioButton2"].checked = true;

          parent.rightFrame.document.theForm.Button1.click();

        }

        else if (parent.loadCount == 3)

        {

          logRemark("Verifying '2.0000'");

          verify("TextBox3", "2.0000");

          logRemark("Selecting RadioButton3");

          logRemark("Clicking Calculate button");

          parent.rightFrame.document.all["RadioButton3"].checked = true;

          parent.rightFrame.document.theForm.Button1.click();

        }

        else if (parent.loadCount == 4)

        {

          logRemark("Verifying '35.0000'");

          verify("TextBox3", "35.0000");

          logRemark("Determining pass / fail");

          if (parent.pass == true)

            theForm.result.value = " Pass ";

          else

            theForm.result.value = " *FAIL* ";

          logRemark("Saving result to 'results.txt'");

          saveResults();

          logRemark("Run at " + parent.whenRun);

        }

      }

      catch(e) {

        logRemark("Unexpected fatal error: " + e);

      }

} // runTest()

    function logRemark(comment)

    {

      var currComment = document.all["comments"].value;

      var newComment = currComment + "\n" + comment;

      document.all["comments"].value = newComment;

    } // logRemark()

    function verify(ctrl, val)

    {

      if (parent.rightFrame.document.all[ctrl].value != val)

        parent.pass = false;

}

    function saveResults()

    {

      var fso = new ActiveXObject("Scripting.FileSystemObject");

      var f = fso.CreateTextFile("C:\\results.txt", true, false);

      f.WriteLine("Description = " + parent.description);

      if (parent.pass == true)

        f.WriteLine("Result = Pass");

      else

        f.WriteLine("Result = FAIL");

      f.Close();

      // document.all["sender"].click();

      //document.all["theForm"].submit();

    } // saveResults()

    </script>

    </head>

  <body bgColor="#aaff99">

    <h3 style="font-size: 14; font-family: Verdana">UI Test Script

    </h3>

    <p><input type="button" value="Run UI Test" onclick="runTest();">

    </p>

    <p>Actions:</p><p><textarea id="comments" rows="15" cols="34">

    </textarea></p>

    <form name="theForm" method="Post" action="..\\SaveResults.asp">

      <p>Test Result = <input type="text" name="result" size="12"></p>

      <p><input type="submit" name="sender" value="Save Results"></p>

    </form>

    <p>

  </body>

</html>

查看所有评论(0)条】

最近评论



正在载入评论列表...
热点评论