Use TestStack.White for automated testing of Windows UI (1) Basics

 Reference   https://www.dazhuanlan.com/2019/09/04/8c18f8874287/

This article will briefly introduce TestStack.White, a Windows UI automated test suite, and perform automated Windows UI testing on a simple adder Windows Form program.

 

Preface

I signed up for SkillTree’s "Automatic Testing and TDD Practical Development" course a few months ago. I was baptized by 91 brothers’ 3-day course. At last I have a relatively complete understanding of Unit Test/TDD/BDD, and I can finally use it in practice. Although many parts are still groping and learning, they have also gained a lot from the important concept of testing first. In addition to getting rid of the dilemma of always tying hands and feet when refactoring in the past, the development speed and function The quality has been improved.

In the class, Brother 91 spent a lot of effort on the introduction of Web UI Testing, but because of the current working environment, there are still many systems that use Windows Form development. In the course exercises, I also asked if there is a suite similar to Selenium's Windows UI Testing. Unfortunately, 91 brother mainly researched the automatic test of the Web, so he did not get an answer. After groping for himself, he found a Windows UI automated test suite of TestStack.White, although unlike Selenium, it can easily record the operation script and convert it into a C# test program. , But it is not too difficult to directly use the program to operate the components on the Windows Form on the basis of familiarity with the related components of the Windows Form. In addition, White also has a Web Testing-like Page Object Pattern suite called Screen Objects that can pull the UI operation logic out of an abstraction layer to reduce the coupling between the test logic and the operation logic, and then have time to study it. Share after watching.

About TestStack.White

TestStack.White (hereafter referred to as White) is a suite that wraps the UI automation test framework "UIAutomation" that Windows itself has, and makes it more convenient to write test programs for Windows UI. Next, let’s briefly introduce it. How to use White

Program to be tested

First, I created an adder project and set the program output path to C:temp, and the program name was AddCalculator.exe. In the following test projects, we will automatically open the adder program in the test program. Enter the number and press the calculation button, then verify that the result produced is correct.

Adder screen

 

"Add" button procedure

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var result = Convert.ToInt32(txtNum1.Text) + Convert.ToInt32(txtNum2.Text);
            lblResult.Text = result.ToString();
        }

From the program screen and the program, it can be seen that if the result of the test project is to be verified, there are probably several steps.

  1. The program must be executed first
  2. Find the two-digit control component and enter the value to be tested
  3. Find the "add" button and let it trigger the Click event
  4. Find the label of the result and check whether the content is correct

Complete the test project

With the basic program practice, then we can directly create a test project and add the TestStack.White package through NuGet package management.

install-package TestStack.White

Then add a test class and add the following program

    [TestClass]
    public class CalculatorFormUITest
    {
        private Application application { get; set; }
        private Window window { get; set; }

        [TestInitialize]
        public void TestInitialize()
        {
            var applicationPath = "C:\temp\AddCalculator.exe";
            application = Application.Launch(applicationPath);
            window = application.GetWindow("加法器", InitializeOption.NoCache);
        }

        [TestCleanup]
        public void TestCleanup()
        {
            application.Close();
        }
    }

The declarations of application and window are used to store the opened application and window data. These two variables will be obtained during TestInitialize. In TestInitialize, first use Application.Launch() to open our executable file, and then use application.GetWindow() to get the content of the window we want to test. The first parameter of application.GetWindow() is the title of the window.

What TestCleanup does is simple, just close the window.

The contents of TestInitialize and TestCleanup allow us to open the window to be tested at the beginning of each test, and close the window after the test ends; then we will add a new test method to simulate the UI operation and verification results:

        [TestMethod]
        public void TestMedthod_Num1_Is_3_And_Num1_Is_5_Then_Result_Is_8()
        {
            // Arrange
            var txtNum1 = window.Get("txtNum1");
            txtNum1.Text = "3";

            var txtNum2 = window.Get("txtNum2");
            txtNum2.Text = "5";

            var expected = "8";

            // Act
            var button = window.Get("btnAdd");
            button.Click();

            // Assert
            var lblResult = window.Get("lblResult");
            var actual = lblResult.Text;

            Assert.AreEqual(expected, actual);
        }

In Arrange, we use window.Get<control class> ("component name") to get the component and content to be set, and then use the window.Get method to get the button to be pressed during Act. , But the Type is changed to Button and the correct component name, and finally Assert grabs the label showing the result, and then checks whether the result is the same as expected.

After executing this test method, you can see the result like the following figure

The test program will automatically execute the developed Windows Form, find the corresponding control simulation behavior, and then verify the result!

The related program can be downloaded here: https://github.com/wellwind/WhiteUiTestingSamples/tree/master/Sample01

Conclusion

This article briefly introduces the use of TestStack.White to automate the test of Windows From, find the corresponding component by directly looking for the UI name and automatically manipulate the behavior of the component, and then test whether the result after the behavior is correct.

Use a simple adder program to represent the complex UI we actually want to test, and then find the corresponding control in the test project, and simulate the actual operation behavior, and finally check the correctness of the operation result.

White has many functions to search for UI components, which can make it easier for us to do automated testing for Windows Form. The details of this part will be introduced later.

In addition to White, TestStack has also launched some useful test suites. For example, those who use the BDD development system can study TestStack.BDDfy; when writing test programs for ASP.NET MVC programs, you can also use TestStack.FluentMVCTesting to make the test program more Colloquial. If you are interested, you can check it out on TestStack's GitHub.

Add progress bar when page is read

 

[VB.NET]TextBox judges whether there is pressing Enter after input

Guess you like

Origin blog.csdn.net/mainmaster/article/details/109856674