簡體   English   中英

使用javascript從靜態HTML頁面編輯本地xml文件時出錯

[英]Error in editing a local xml file from a static HTML page using javascript

我在本地存儲(硬盤驅動器)上有一個靜態html頁面。 我在這里沒有使用任何類型的服務器。 與此同時,我有一個名為testng.xml的xml文件,我需要使用javascript在按鈕單擊時從html編輯該文件。

我需要根據用戶通過網頁上的復選框進行的選擇,將<include>標記替換為<include> <exclude>標記,反之亦然。 然后通過單擊按鈕執行testng xml。

從html按鈕調用該函數,如下所示

<input type="submit" onclick="editTestNG()" value="Execute Selected Tests">

下面給出的是我用於編輯xml的代碼。

<script type="text/javascript">

function editTestNG() 
{

  xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", "testng.xml", false);
  xmlHttp.send();

  xmlDoc = xmlHttp.responseXML;
  txt = "";

  x = document.getElementsByName("check");
  document.write(x.length + "<br>");  // x.length is number of checkboxes on html page

  z = xmlDoc.getElementsByTagName("run")[1];
  y = z.children;
  document.write(y.length + "<br>"); // y.length is the number of xml child nodes 

  document.write(x.length + "<br>");  // x.length is number of checkboxes on html page

  for(i=0;i<x.length;i++)
  {
    document.write("abc");

    if(x[i].type == 'checkbox')
    {  
        document.writeln("def");

        if(x[i].checked == "true" && y[i].nodeName == "exclude")
        {
            document.writeln("ghi");

            txt = y[i].getAttribute("name");          

            newTag = xmlDoc.createElement("include");
            newTag.setAttribute("name",txt);
            p = z.replaceChild(newTag, y[i]); 
            document.write(p.nodeName+"<br>")
            document.write(newTag.nodeName + "::"+ newTag.getAttribute("name")+"<br>");
        }

        if(x[i].checked == "false" && y[i].nodeName == "include")
        {
            document.writeln("jkl");

            txt = y[i].getAttribute("name");          

            newTag = xmlDoc.createElement("exclude");
            newTag.setAttribute("name",txt);
            p = z.replaceChild(newTag, y[i]); 
            document.write(p.nodeName+"<br>")
            document.write(newTag.nodeName + "::"+ newTag.getAttribute("name")+"<br>");
        }
    }
}
  y = z.children;

  for(i=0;i<y.length;i++)
  {
    document.writeln("<br>"+y[i].nodeName + " ## "+ y[i].getAttribute("name")+"<br>");
  }

}

</script>

單擊按鈕后,html頁面上的輸出如下

140  
7    
0    

exclude ## Group_CCMS_Login

exclude ## Group_CCR_Create_New

exclude ## Group_CCR_Create_New_EngType_M

exclude ## Group_CCR_Create_New_EngType_P

exclude ## Group_Full_ID_Search

include ## Group_Run_Query

exclude ## Group_Dup_Undup

我能夠在z和y變量中檢索xml文件和元素。 同樣在第二個for循環中,將輸出包含xml節點名稱和屬性的輸出。 但是在y變量中獲取xml數據后,我無法獲取或使用任何html頁面元素(例如復選框)。

x.length的值最初為140, y.length值為7,但隨后x.length變為0,並且我的1st for循環不執行。

我無法理解為什么獲取XML數據后無法訪問html元素。

我從未嘗試過,但是據我了解,您可以創建一個bash文件,然后使用jsp代碼將其刪除。 在HTML中添加該jsp代碼。

參考:-

從jsp頁面執行bash腳本

現在,正如您所說的:“我需要准備一個帶有復選框的HTML用戶界面,以選擇或取消選擇要運行的測試,只需單擊一個按鈕即可”

為此,您可能需要創建不同的testng套件,因此需要分別創建更多的bash。

參考:-

http://grokbase.com/t/gg/selenium-users/139r0nszp3/how-to-run-multiple-xml-files-with-testng

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM