Can't select by index in a combo box

Feb 1, 2013 at 2:19 PM
All,

I'm trying to click on an item in a combo box based on the index. When I try to select items 0 - 1 I get this error:
Test method IDIBilling.HydraTestFramework.BillingContact_Class.BillingContact threw exception:
System.Exception: Test failed on step 1 - iteration 1. ---> Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException: Cannot perform 'SetProperty of SelectedIndex with value "1"' on the control. Additional Details:
TechnologyName: 'Web'
ControlType: 'ComboBox'
Id: 'PaymentAccountContactID'
Name: 'PaymentAccountContactID'
TagName: 'SELECT'
Failed to find any control that matched the value Contact_@contact.ContactID for the search property Id. ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0xF004F008

Here is the HTML for this section:
<select id="PaymentAccountContactID" class="InputLargeDropdown paymentContactInfo" name="PaymentAccountContactID">
<option id="Contact_@contact.ContactID" value="51232">BILL JENSVOLD</option>
<option id="Contact_@contact.ContactID" value="172453">BILL JENSVOLD</option>
<option value="0">Create New Contact...</option>
</select>

This was working fine before the addition of id="Contact_@contact.ContactID. Also, if I specify the name - BILL JENSVOLD - it finds the first on in the list without a problem. If I specify "2" as the index then again this works fine and select "Create New Contact..." from the list.

Any ideas of what is causing this or how to fix it? Thanks for the help.

Jeff
Coordinator
Feb 2, 2013 at 3:54 PM
Hi Jeff,

I checked in a sample unit test to illustrate this functionality as follows:
        [TestMethod]
        public void SelectItem_ByIndexOnHtmlComboBox_Succeeds()
        {
            //Arrange
            string tempFilePath = Path.GetTempFileName();

            File.WriteAllText(tempFilePath,
@"<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <select id=""selectId"">
            <option>Cricket</option>
            <option>Football</option>
            <option>Tennis</option>
        </select>
    </body>
</html>");

            CUITe_BrowserWindow.Launch(tempFilePath);
            CUITe_BrowserWindow window = new CUITe_BrowserWindow("test");

            //Act
            CUITe_HtmlComboBox comboBox = window.Get<CUITe_HtmlComboBox>("Id=selectId");

            comboBox.SelectItem(1);

            //Assert
            Assert.AreEqual("Football", comboBox.SelectedItem);

            window.Close();

            File.Delete(tempFilePath);
        }
What does your test code look like?

Thank you.
Feb 4, 2013 at 12:15 PM
icnocop,

Thanks for the reply.

The test code looks like this:

public CUITe_HtmlComboBox PaymentAccountContactDropDown { get { return Get<CUITe_HtmlComboBox>("id=PaymentAccountContactID"); } }

Then I have a method in the same class where we pass in the value that we want to select starting at an index of 0:
    public void selectBillContactByNumber(string numberOfContact)
    {
        int numSelect = numberOfContact.ToInt();
        this.PaymentAccountContactDropDown.SelectItem(numSelect);
    }
I also tried this by hardcoding the value in this.PaymentAccountContactDropDown.SelectItem(1); but it didn't make any difference.

One other thing that I did was update the HTML on the page to remove the id attributes and then the select worked fine. The HTML looked like this.
<option value="51232">BILL JENSVOLD</option>
<option value="172453">BILL JENSVOLD</option>
<option value="0">Create New Contact...</option>

Doing this for real on the web page is not practical since the id were added to the control for another purpose.
Coordinator
Feb 7, 2013 at 8:45 AM
Hi Jeff,

I could not reproduce your issue.
        [TestMethod]
        public void SelectItem_ByIndexOnHtmlComboBox_Succeeds()
        {
            //Arrange
            string tempFilePath = Path.GetTempFileName();

            File.WriteAllText(tempFilePath,
@"<html>
    <head>
        <title>test</title>
    </head>
    <body>
<select id=""PaymentAccountContactID"" class=""InputLargeDropdown paymentContactInfo"" name=""PaymentAccountContactID"">
<option id=""Contact_@contact.ContactID"" value=""51232"">BILL JENSVOLD</option>
<option id=""Contact_@contact.ContactID"" value=""172453"">BILL JENSVOLD</option>
<option value=""0"">Create New Contact...</option>
</select> 
    </body>
</html>");

            TestObject testObject = CUITe_BrowserWindow.Launch<TestObject>(tempFilePath);

            //Act
            testObject.PaymentAccountContactDropDown.SelectItem(1);

            //Assert
            Assert.AreEqual("BILL JENSVOLD", testObject.PaymentAccountContactDropDown.SelectedItem);

            testObject.Close();

            File.Delete(tempFilePath);
        }

    public class TestObject : CUITe_BrowserWindow
    {
        public new string sWindowTitle = "test";

        public CUITe_HtmlComboBox PaymentAccountContactDropDown { get { return Get<CUITe_HtmlComboBox>("id=PaymentAccountContactID"); } } 
    }
The text in the exception in your first post seems contradictory to the sample code.
For example, the exception reads, "Cannot perform 'SetProperty of SelectedIndex with value "1"' on the control.", but the sample code indicates you are trying to call the SelectItem method.
Also, the exception reads, "Failed to find any control that matched the value Contact_@contact.ContactID for the search property Id." but this contradicts the search property specified as "id=PaymentAccountContactID".

I recommend running your code with debug symbols so that the line number is included in the stack trace at the time of the exception so you can verify the line of code where the exception is being thrown, etc.

Thank you.