2014-10-07 37 views
0

我試圖用兩個複選框和一個提交超鏈接創建一個編碼的UI測試(使用VS2010旗艦版)。複選框具有相同的文本標籤; 「我同意」。VS2010編碼UI測試 - 測試生成器無法映射具有相同文本的兩個複選框

  • 使用編碼的UI測試構建器來記錄動作,只捕獲一個複選框,因爲兩個複選框都具有相同的文本/相同的UIMap名稱。
  • 使用十字光標工具選擇第二個複選框,它將替換上一個複選框實例,因爲它們具有相同的文本/相同的UIMap名稱。

當測試運行時,第一個複選框被選中,第二個複選框被選中,並且超鏈接被單擊以提交表單(失敗驗證)。

如何將第二個複選框添加到測試圖並區分兩者?

回答

0

如果複選框本身沒有唯一屬性,請指定每個複選框的父對象以區分它們。

例子:
對於

<div id="box1Parent"> 
    <input label="I Agree"/> 
</div> 
<div id=box2Parent"> 
    <input label="I Agree"/> 
</div> 

你會定義對象是這樣的:

public HtmlCheckBox AgreementBox1() 
{ 
    HtmlDiv parent = new HtmlDiv(browser); 
    parent.SearchProperties["id"] = "box1Parent"; 
    HtmlCheckBox target = new HtmlCheckBox(parent); 
    target.SearchProperties["label"] = "I Agree"; 
    return target; 
} 

然後,做了第二個盒子一樣的,但父指向box2Parent。這將是您的代碼在.uitest類的非設計器部分。

0

有多種方法可以做到這一點。

  1. 試着找出像id,name這樣的對象的唯一屬性。

  2. 嘗試查找複選框的父控件/容器,然後使用{TAB}或{UP}/{DOWN}鍵。

  3. 使用鍵盤的{TAB}鍵。找到以前的控件 - >點擊該控件 - >使用該控件中的{TAB}來關注複選框控件,並使用{UP}/{DOWN}箭頭鍵導航。

  4. 找到文件的文本,並根據您的需要點擊第一或第二次出現。 代碼找出文檔中的文本,

    public string GetCurrentPageVisibleTexts() 
        { 
         var window = this.UIMap.<WindowObject> 
         UITestControlCollection c = window.GetChildren(); 
         var pgContent = (string)c[0].GetProperty("OuterHtml"); 
         var document = new HtmlAgilityPack.HtmlDocument(); 
         document.LoadHtml(pgContent); 
    
         // We don't want these in our result 
         var exclusionText = new string[] { "<!--", "<![CDATA", "function()", "</form>" }; 
    
         var visibleTexts = new List<string>(); 
         //var nodes = document.DocumentNode.Descendants().Where(d => !d.Name.ToLower().Equals("span")); 
         foreach (var elem in document.DocumentNode.Descendants()) 
         { 
          // Foreach element iterate its path back till root 
          // and look for "display: none" attribute in each one of its parent node 
          // to verify whether that element or any of its parent are marked as hidden 
          var tempElem = elem; 
    
          while (tempElem.ParentNode != null) 
          { 
           if (tempElem.Attributes["style"] != null) 
           { 
            // if hidden attribute found then break. 
            if (tempElem.Attributes["style"].Value.ToLower().Contains("display: none")) break; 
           } 
           tempElem = tempElem.ParentNode; 
          } 
    
          // If iteration reached to head and element is found clean of hidden property then proceed with text extraction.      
          if (tempElem.ParentNode == null) 
          { 
           if (!exclusionText.Any(e => elem.InnerText.Contains(e)) 
           && (!elem.InnerText.Trim().IsNullOrEmpty()) 
           && (!elem.HasChildNodes)) 
           { 
            visibleTexts.Add(elem.InnerText); 
           } 
          } 
         } // Foreach close 
    
         var completeText = string.Join(" ", visibleTexts).Replace("&nbsp;", " "); 
         return Regex.Replace(completeText, @"\s+", " "); 
        } 
    
相關問題