I need to write a test case for a simple grid layout WinForm app in .net4

Following are the screenshot of my app: enter image description here

After I hit the "populateBtn" button the rows gets filled with some dummy data: enter image description here

Here is the code for the button's click event handler:

        private void populateBtn_Click(object sender, EventArgs e)
        {
            Person[] data = new Person[] {
                new Person { Name = "samman", Age = 32, IsMarried = false, },
                new Person { Name = "tom", Age = 32, IsMarried = false, },
                new Person { Name = "harreld", Age = 32, IsMarried = false, },
            };

            int idx = 0;
            foreach (Person p in data )
            {
                DataGridViewCheckBoxCell isMarriedCell = new DataGridViewCheckBoxCell();

                isMarriedCell.Value = p.IsMarried;

                dataGridView1.Rows.Add(p.Name, p.Age);
                dataGridView1[2, idx] = isMarriedCell;

                idx++;
            }
        }

The most important thing in the above code is the way I am adding the checkbox. I have created DataGridViewCheckBoxCell object and added it to the third index of each row of the grid.

The app works just fine. But the issue is that I do not understand what attributes should I pass to each of this dynamically created DataGridViewCheckBox so that I can identifies each of them in my test cases.

My requirements are that I need to get the first CheckBox and then click it to see if it works. I am a web developer and in javascript, I would have simply passed a unique "id" attribute to each Checkbox element. I do not know what attribute is similar to "id" for DataGridViewCheckBoxCell or .net WinForm elements in general.

I did try something like isMarriedCell.AccessibilityObject.Name = $"gridviewcheckbox-{idx}"; , But when I use inspect software to inspect the checkboxes, they do not seem to have that Name. Maybe its because DataGridViewCheckBox is some sort of wrapper around Checkbox and it does not pass its Name to the Checkbox it creates.

I might also be able to solve this using indexes, but I do not want to do that. In the future I might have to get a checkbox without knowing its exact position in the grid, so I want to use some sort of id so that no matter the position, I can still get the checkbox in my test cases.

Please Help. Here is the code for my test case if it helps:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.Threading;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        private static WindowsDriver<WindowsElement> session;
        [ClassInitialize]
        public static void ClassInitialize(TestContext context) {
            Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
            Thread.Sleep(400);

            //set the desired capabilities to the PE_ProHat
            DesiredCapabilities appCapabilities = new DesiredCapabilities();

            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            appCapabilities.SetCapability("app", @"C:\Users\Samman.Adhikari\source\repos\ExperimentingWithDataGridView\ExperimentingWithDataGridView\bin\Debug\ExperimentingWithDataGridView.exe");
            appCapabilities.SetCapability("ms:waitForAppLaunch", "15");
            session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723/"), appCapabilities, TimeSpan.FromSeconds(60));
            //set the time out 
            session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);

            //change to the window
            session.SwitchTo().Window(session.WindowHandles[0]);

            session.FindElementByAccessibilityId("populateBtn").Click();

            if (session.FindElementByName("Married Row 0").GetAttribute("Toggle.ToggleState") == "0")
            {
                session.FindElementByName("Married Row 0").Click();
            }
        }

        [TestMethod]
        public void TestMethod1()
        {

        }

    }

------------ [EDIT] ----------------------------

As matthew suggested in the comment, I also did try using Tag but that also didn't work. I got An element could not be located on the page using the given search parameters. error. Here are the changes that I made for this:

In click event handler I added:

isMarriedCell.Tag = $"checkbox-{idx}";

In test ClassInitialize I added:

            if (session.FindElementByTagName("checkbox-0").GetAttribute("Toggle.ToggleState") == "0")
            {
                session.FindElementByTagName("checkbox-0").Click();
            }
0

There are 0 best solutions below