简体   繁体   English

codedui复选框已选中c#

[英]codedui checkbox checked c#

I want to select(check) multiple checkboxes on a html form as shown below using CodedUI I have tried the following but I am not sure how to select a specific check box, I would be very grateful if someone can come up with some ideas thanks: 我想使用CodedUI在html表单上选择(选中)多个复选框,我已经尝试了以下操作,但是我不确定如何选择特定的复选框,如果有人可以提出一些想法,我将不胜感激,谢谢:

    UITestControl checkBoxes = new UITestControl(browser);
    checkBoxes.TechnologyName = "Web";
    checkBoxes.SearchProperties.Add("TagName", "TD");
    checkBoxes.SearchProperties.Add("ControlType", "Cell");
    checkBoxes.SearchProperties.Add("Name", "checkboxes[]");
    // checkBoxes.SearchProperties[HtmlCheckBox.PropertyNames.Value] = "cb2";
    //  checkBoxes.SearchProperties.Add("InnerText", "Checkbox Items:");

    checkBoxes.SetProperty("Checked", true);




    <tr>
      <td>
        Checkbox Items:<br />
        <input type="checkbox" name="checkboxes[]" value=
        "cb1" />Checkbox 1 
        <input type="checkbox" name="checkboxes[]"
        value="cb2" />Checkbox 2 
        <input type="checkbox" name=
        "checkboxes[]" value="cb3" checked="checked" />Checkbox 3
      </td>
    </tr>
    HtmlCheckBox mUICheckboxesCheckBox = new HtmlCheckBox(parentControl);

#region Search Criteria for first checkBox cb1
                        mUICheckboxesCheckBox.SearchProperties[HtmlCheckBox.PropertyNames.Id] = null;
                        mUICheckboxesCheckBox.SearchProperties[HtmlCheckBox.PropertyNames.Name] = "checkboxes[]";
                        mUICheckboxesCheckBox.SearchProperties[HtmlCheckBox.PropertyNames.Value] = "cb1";
                        mUICheckboxesCheckBox.SearchProperties[HtmlCheckBox.PropertyNames.LabeledBy] = null;
                        mUICheckboxesCheckBox.FilterProperties[HtmlCheckBox.PropertyNames.Title] = null;
                        mUICheckboxesCheckBox.FilterProperties[HtmlCheckBox.PropertyNames.Class] = null;
                        mUICheckboxesCheckBox.FilterProperties[HtmlCheckBox.PropertyNames.ControlDefinition] = "name=\"checkboxes[]\" value=\"cb1\" type=\"ch";
                        mUICheckboxesCheckBox.FilterProperties[HtmlCheckBox.PropertyNames.TagInstance] = "1";
                        mUICheckboxesCheckBox.WindowTitles.Add("http://localhost:23159/HtmlPage1.html");
                        #endregion
if( mUICheckboxesCheckBox.TryFind()) 
mUICheckboxesCheckBox.Checked = true;

Do the same for other two check boxes. 对其他两个复选框执行相同的操作。

Well, depends on what exactly you are trying to do. 好吧,取决于您到底要做什么。 If possible, add an identifier to each checkbox so that you can more easily identify it. 如果可能,在每个复选框中添加一个标识符,以便您可以更轻松地识别它。 If you cannot, but you can assume there are always x number of checkboxes in the same order, then you can still make it work. 如果不能,但是可以假设总是有x个复选框处于相同顺序,那么您仍然可以使它工作。

  1. Add identifier 添加标识符
    <tr>
      <td>
        Checkbox Items:<br />
        <input type="checkbox" name="checkboxes[]" value=
        "cb1" data-automation-name="firstBox" />Checkbox 1 
        <input type="checkbox" name="checkboxes[]"
        value="cb2" data-automation-name="secondBox" />Checkbox 2 
        <input type="checkbox" name=
        "checkboxes[]" value="cb3" data-automation-name="thirdBox" checked="checked" />Checkbox 3
      </td>
    </tr>
HtmlCheckBox firstBox = new HtmlCheckBox(tableRow);
firstBox.SearchProperties.Add(HtmlControl.PropertyNames.ControlDefinition, String.Format("{0}=\"{1}\"", "data-automation-name", "firstBox"), PropertyExpressionOperator.Contains);

HtmlCheckBox secondBox = new HtmlCheckBox(tableRow);
firstBox.SearchProperties.Add(HtmlControl.PropertyNames.ControlDefinition, String.Format("{0}=\"{1}\"", "data-automation-name", "secondBox"), PropertyExpressionOperator.Contains);

HtmlCheckBox thirdBox = new HtmlCheckBox(tableRow);
firstBox.SearchProperties.Add(HtmlControl.PropertyNames.ControlDefinition, String.Format("{0}=\"{1}\"", "data-automation-name", "thirdBox"), PropertyExpressionOperator.Contains);
  1. Find all and iterate 查找全部并进行迭代

    var checkBoxesLookupFunc = () => new HtmlCheckBox(tableRow).FindMatchingControls().Cast(); var checkBoxesLookupFunc =()=>新的HtmlCheckBox(tableRow).FindMatchingControls()。Cast();

    HtmlCheckBox firstBox = checkBoxesLookupFunc().First(); HtmlCheckBox firstBox = checkBoxesLookupFunc()。First();

    HtmlCheckBox secondBox = checkBoxesLookupFunc().Skip(1).First(); HtmlCheckBox secondBox = checkBoxesLookupFunc()。Skip(1).First();

    HtmlCheckBox thirdBox = checkBoxesLookupFunc().Skip(2).First(); HtmlCheckBox thirdBox = checkBoxesLookupFunc()。Skip(2).First();

You have to use a function that returns a new search collection because the objects seem to be unable to look up a second time. 您必须使用一个返回新搜索集合的函数,因为这些对象似乎无法再次查找。 I believe it has to do with the internal storage of the query path to the control not being set correctly when doing a find matching controls. 我相信这与查询匹配控件时未正确设置该控件的查询路径的内部存储有关。

I would consider using an abstraction over coded ui to help write the selectors. 我会考虑在编码的ui上使用抽象来帮助编写选择器。 I have my own library to help with this which is a simple set of extensions over coded ui. 我有自己的库来帮助解决这个问题,这是对编码ui的一组简单扩展。 Using the extensions, you would write 使用扩展,您将编写

var firstBox = browser.Find<HtmlCheckBox>().WithDataAttribute("automation-name", "firstBox");
var firstBox = browser.FindAll<HtmlCheckBox>().First();

There are other libraries that provide ability to search using jquery . 还有其他库可以使用jquery进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM