FlaUI reference a button by a Control instead of a string

128 views Asked by At

I want to find a way to instead of calling the button by name "Add" on line 26, to be called by the control instead. something like nameof(AddButton) or something of the like.

this code is in a test project that is in the same solution as my winform and it has a project reference to it.

using FlaUI.Core;
using FlaUI.UIA2;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestFormRevisitedTests
{
    [TestClass]
    public class UITest
    {
        string _folderPath = @"C:\Users\user\Desktop\testPhotos\";


        [TestMethod]
        public void AddButtonAutomation()
        {
            var app = Application.Launch("C:\\Users\\user\\Desktop\\Self-Study\\TestFormRevisited\\TestFormRevisited\\bin\\Debug\\net7.0-windows\\TestFormRevisited.exe");

            try
            {
                using (var automation = new UIA2Automation())
                {
                    var mainWindow = app.GetMainWindow(automation);

                    if (mainWindow != null)
                    {
                        var button = mainWindow.FindFirstDescendant(b => b.ByName("Add"));
                        var screenBeforeClick = mainWindow.Capture();

                        if (button != null)
                            button.Click();

                        var screenAfterClick = mainWindow.Capture();




                        screenBeforeClick.Save(Path.Combine(_folderPath, "before_click.png"));
                        screenAfterClick.Save(Path.Combine(_folderPath, "after_click.png"));
                    }
                }
            }
            finally
            {
                app.Close();
            }
        }
    }
}

I tried putting calling the controls with

var mainForm = new TestFormRevised();
var controls = mainForm.Controls;

but i don't think it does what i think it does and i couldn't call on the exact buttons and other controls i needed

0

There are 0 answers