Metro automation: navigating to a certain page


A typical Metro UI application is a set of pages. One is main, and there are several pages where we can go from the main page.

A Metro UI app is unlikely to a traditional app, which often starts from some starting point. A Metro UI app starts for the first time with its main page, but further is may start from wherever page the user left the app.

In our sample, we’ll be using the *Wizard* cmdlets. What are they?

1. the New-UIAWizard cmdlet creates a wizard object, provides it with name and contains a code to start the AUT (application under test), for example. Some initial code.

2. the Add-UIAWizardStep cmdlet creates a step, provides the step with a name, takes a wizard object as an input.

First action we should do on a step is to examine the step. The -SearchCriteria parameter does exactly this task.

After that, we run code for this step. There are two actions we can use, for forward movement and for backward. In out Metro UI app we will be using only forward steps for simplicity sake.

3. the Invoke-UIAWizard cmdlets runs the wizard (in fact, the code we added to the New-UIAWizard cmdlet). It’s usually used for starting the AUT. For purposes of Metro UI testing, we will use this action to show the Start screen and to open the app window.

4. the Step-UIAWizard cmdlet checks the -SearchCriteria (i.e., checks that controls are here and we are on the right page) and runs the step code.


Set-StrictMode -Version Latest

[UIAutomation.Preferences]::Timeout = 10000;
[UIAutomation.Preferences]::EveryCmdletAsTestResult = $true;
[UIAutomation.Preferences]::OnSuccessDelay = 300;

# names of the pages and buttons in the app menu
[string]$constPageMain = 'Home';
[string]$constPageFeaturedDestinations = 'Featured Destinations';
[string]$constPageDestinations = 'Destinations';
[string]$constPageFlights = 'Flights';
[string]$constPageHotels = 'Hotels';
[string]$constPageBestOfWeb = 'Best Of Web';

# names of wizard steps
[string]$constWizardName = 'TravelWizard';
[string]$constWizardStepHome = 'Home';
[string]$constWizardStepDestinations = 'Destinations';
[string]$constWizardStepFlights = 'Flights';
[string]$constWizardStepHotels = 'Hotels';
[string]$constWizardStepBestOfWeb = 'BestOfWeb';

# we need to clear the collection of wizards or delete
# the wizard we are playing with.
# Otherwise, the next run will throw an exception
# 'Wizard already exists'
[UIAutomation.WizardCollection]::Wizards.Clear();

# Creating a new wizard
New-UIAWizard -Name $constWizardName `
 -StartAction {
 # setting the Metro Start screen
 Get-UIADesktop;
 Show-UIAMetroStartScreen;

# clicking on the tile of the AUT
 Get-UIAListItem -Name 'Travel' | `
 Invoke-UIAListItemClick;

# the loading screen (if happened)
 try {
 Get-UIAWindow -Name 'Travel' | `
 Get-UIAProgressBar;
 #sleep -Seconds 5; # the app is loading
 }
 catch {}

 # start our tests from the Main page
 Show-UIAMetroMenu;
 Get-UIAWindow -Name 'Travel' | `
 Get-UIAMenuBar -Name 'App Bar' | `
 Get-UIAHyperlink -Name $constPageMain | `
 Invoke-UIAControlClick;
 } | `
 Add-UIAWizardStep -Name $constWizardStepHome `
 -SearchCriteria @{ControlType="Text";Name='Bing Travel'} `
 -StepForwardAction {
 #[System.Windows.Forms.MessageBox]::Show("StepHome");
 "<<<<<<<<<< On the Home page >>>>>>>>>>";
 try {
 Get-UIAText -Name 'Bing Travel';
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Home page >>>>>>>>>>" -TestPassed;
 }
 catch {
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Home page >>>>>>>>>>";
 }
 } -PassThru | `
 Add-UIAWizardStep -Name $constWizardStepDestinations `
 -SearchCriteria @{ControlType="Text";Name='Destinations'},@{ControlType="Text";Name='Region'} `
 -StepForwardAction {
 #[System.Windows.Forms.MessageBox]::Show("StepDestinations");
 "<<<<<<<<<< On the Destinations page >>>>>>>>>>";
 try {
 Get-UIAText -Name 'Destinations';
 Get-UIAText -Name 'Region';
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Destinations page >>>>>>>>>>" -TestPassed;
 }
 catch {
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Destinations page >>>>>>>>>>";
 }
 } -PassThru | `
 Add-UIAWizardStep -Name $constWizardStepFlights `
 -SearchCriteria @{ControlType="Text";Name='Flights'},@{ControlType="Text";Name='Schedule'} `
 -StepForwardAction {
 #[System.Windows.Forms.MessageBox]::Show("StepFlights");
 "<<<<<<<<<< On the Flights page >>>>>>>>>>";
 try {
 Get-UIAText -Name 'Flights';
 Get-UIAText -Name 'Schedule';
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Flights page >>>>>>>>>>" -TestPassed;
 }
 catch {
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Flights page >>>>>>>>>>";
 }
 } -PassThru | `
 Add-UIAWizardStep -Name $constWizardStepHotels `
 -SearchCriteria @{ControlType="Text";Name='Hotels'},@{ControlType="Text";Name='Check-in'} `
 -StepForwardAction {
 #[System.Windows.Forms.MessageBox]::Show("StepHotels");
 "<<<<<<<<<< On the Hotels page >>>>>>>>>>";
 try {
 Get-UIAText -Name 'Hotels';
 Get-UIAText -Name 'Check-in';
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Hotels page >>>>>>>>>>" -TestPassed;
 }
 catch {
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the Hotels page >>>>>>>>>>";
 }
 } -PassThru | `
 Add-UIAWizardStep -Name $constWizardStepBestOfWeb `
 -SearchCriteria @{ControlType="Text";Name='Travel'},@{ControlType="Text";Name='Explore'},@{ControlType="Text";Name='Plan a Trip'} `
 -StepForwardAction {
 #[System.Windows.Forms.MessageBox]::Show("Best Of Web");
 "<<<<<<<<<< On the 'Best Of Web' page >>>>>>>>>>";
 try {
 Get-UIAText -Name 'Travel';
 Get-UIAText -Name 'Explore';
 Get-UIAText -Name 'Plan a Trip';
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the 'Best Of Web' page >>>>>>>>>>" -TestPassed;
 }
 catch {
 Close-TMXTestResult -Name "<<<<<<<<<< Navigating to the 'Best Of Web' page >>>>>>>>>>";
 }
 };

# Start the wizard
[UIAutomation.Wizard]$wizard = Invoke-UIAWizard -Name $constWizardName;

# Click on the Destinations button in the app menu
Show-UIAMetroMenu;
Get-UIAWindow -Name 'Travel' | `
 Get-UIAMenuBar -Name 'App Bar' | `
 Get-UIAHyperlink -Name $constPageDestinations | `
 Invoke-UIAControlClick;

# Check that this is the Destinations page and run code (in the future sample)
$wizard | Step-UIAWizard -Name $constWizardStepDestinations

# Click on the Flights button in the app menu
Show-UIAMetroMenu;
Get-UIAWindow -Name 'Travel' | `
 Get-UIAMenuBar -Name 'App Bar' | `
 Get-UIAHyperlink -Name $constPageFlights | `
 Invoke-UIAControlClick;

# Check that this is the Flights page and run code (in the future sample)
$wizard | Step-UIAWizard -Name $constWizardStepFlights;

# Click on the Hotels button in the app menu
Show-UIAMetroMenu;
Get-UIAWindow -Name 'Travel' | `
 Get-UIAMenuBar -Name 'App Bar' | `
 Get-UIAHyperlink -Name $constPageHotels | `
 Invoke-UIAControlClick;

# Check that this is the Hotels page and run code (in the future sample)
$wizard | Step-UIAWizard -Name $constWizardStepHotels;

# Click on the 'Best Of Web' button in the app menu
Show-UIAMetroMenu;
Get-UIAWindow -Name 'Travel' | `
 Get-UIAMenuBar -Name 'App Bar' | `
 Get-UIAHyperlink -Name $constPageBestOfWeb | `
 Invoke-UIAControlClick;

# Check that this is the 'Best Of Web' page and run code (in the future sample)
$wizard | Step-UIAWizard -Name $constWizardStepBestOfWeb;

The dubious piece of code is getting a progress bar on loading the application. Sometimes, it is shown. Sometimes, there is no progress bar.

Advertisements

3 responses

  1. Hi Alex –
    What are the modules that support?
    How to import the module for the above script?

    1. Hi Raja,
      import the module by pointing to its assembly:
      ipmo C:\..path.to.the.binaries….\UIAutomation.dll
      or, in case you have placed binaries in the folder with name UIAutomation:
      ipmo C:\…path.to.the.folder…\UIAutomaiton

      Please refer to the page http://softwaretestingusingpowershell.com/metro-ui-automation/ to learn how to
      download and unblock the package, import its certificate, put it in a secure location or change the policy…

      What’s more, since ~0.8.6, the wizard cmdlet have changed and by the time I’ll publish final 0.8.6, I probably must change this script.

      1. Thanks for the informaton..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: