A couple of weeks ago, I was preparing for a training about automated testing. Considering the fact that Selenium was widely used in Perficient GDC (Global Delivery Center in Hangzhou, China) and the purpose was to introduce more technologies to the team, I created some demos by Windows API and UI Automation.
Here are three steps to GUI automated testing:
- Attach to the application. Usually, the step is specific to the OS your test will be run on. You can achieve this step through calling the native APIs exposed by the OS, such as FindWindow and FindWindowEx. If you don’t want to reinvent the wheel by interacting with APIs directly, you can utilize the library provided by certain programming language, such as Java and .Net. These languages have provided some well encapsulated libraries to handle process, message, event, etc.
- Locate the UI element. The way you can locate the UI element varies and depends on how the GUI is implemented. A general way is to use the combination of identifier and relationship. If a textbox has an unique ID globablly, then you can easily locate it by looking for the element with this ID. On MS Windows, the hwnd of a window would be a good example of globally unique identifer. However, for the most time developers won’t provide such kind of convinience to automated testing. Hence, you need to handle the duplicate ID issue by some tricks. For instance, you can use the relationship of the element properly (parent, child, sibling, etc). You can also use attributes other than ID to locate the element, such as control type, class name, etc.
- Play with the UI element. As long as you have located the element, you can do whatever you want by simulating the behaviors supported by this type of element. The most common behavior would be click() and rightclick(). How this kind of behavior can be simulated will be totally up to the UI driver you’re using.
So, keep in mind that you only need to take three steps to do the automated testing.