How to empower GUI Automation and integrate it in Powershell

Fazle Kareem K M Sep 29 - 3 min read

Audio : Listen to This Blog.

In this blog we will discuss about how to empower GUI Automation and integrate it into Powershell framework. In general, Powershell scripts are mainly used for automating the middleware and backend system functionality.

But, in order to get end-to-end automation, we will have to integrate the frontend GUI Automation too. GUI Automation can be used as a validation point, soon after performing the middleware and backend system API execution.

This can be achieved by using binary module [Using C# .Net Automation Libraries] through which UI automation can be bridged with middleware and backend system automation.
Let’s take a look at “How binary module integration is done”

What is Powershell Binary Module?

Importing a dll [Using C# .Net libraries] into powershell is referred to as Binary Module. A binary module can be any assembly (.dll) that contains cmdlet classes. A dll which contains cmdlet classes can be imported into powershell. By default, all the cmdlets in the assembly are imported when the binary module is imported.
Binary Module integration is segregated into two divisions,

  1. Creating library file using C#.
  2. Loading the dll into powershell

1. Creating library file using C#.

STEP 1 : Creating a New Project

Select the Visual C# from the installed Templates list and then pick Class Library since compiled modules in PowerShell are in DLL format. Let’s name this project as DemoModule.

STEP 2 : Navigate to Properties

Select the Debug option in the menu and select DemoModule Properties.

STEP 3 : Choose .NET framework

Go to Application and choose the appropriate (suitable for powershell version) .NET framework version under “Target framework”.

In Visual Studio 2012 the assembly will be built using .NET 4.5, but you’ll need to switch it to use the older framework, so that Powershell can load the assembly.
Note : Select the .NET Framework depending on the lowest version of PowerShell to support. You can use the following as reference:
                PowerShell 2.0 – .NET Framework 3.5
                PowerShell 3.0 – .NET Framework 4
                PowerShell 4.0 – .NET Framework 4.5

STEP 4: Adding reference.

Right click on Reference in the Solution Explorer and select Add Reference.

Loading the following UI Automation library as reference

Need to load the System.Management.Automation library as a reference to our project to make the PowerShell API calls available.

Select browse under Reference manager.

Navigate to location
C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35 and select the file System.Management.Automation.dll

After adding the following library in references.

STEP 5 : Import “System.Windows.Automation” library

Include the “System.Windows.Automation” reference to the class to get access to API calls in order to build a PowerShell module.

STEP 6 : Converting the class into a single Powershell cmdlet

Creating Powershell cmdlet using dataanotation. “[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, ”DemoName”)]”

STEP 7 : Build the Project to dll

Build solution is done either by hitting F7 or selecting “Build Solution” from the BUILD menu. After build succeeds, collect the dll from the path displayed in Output pane.

2. Loading the dll into powershell :

STEP 1: Navigate the location of dll with Import-Module along with -Verbose to load the module and give output. It shows that Get-DemoName is loaded.
DLL received from .Net environment is been loaded into Windows powershell prompt using cmdlet “Import-Module”

STEP 2: Get-Module displays the module information which in turn implies that DLL gets loaded successfully.

Powershell cmdlet “Get-help Get-DemoName” shows the detailed information of the cmdlet

Conclusion

Importing binary modules written in C# helps achieve GUI automation through Powershell.

Leave a Reply

Download our Success Story for UI Automation where we built a docker compatible UI automation framework for end-to-end testing of a network product with SolarCapture.