Test Universal Windows Platform Apps using Windows Device Portal - Part II
In my previous blog, I talked about how to test your Windows Apps on various windows device types using Device Portal on your web browser. If you have not read it yet, check it out here.
Now let's discuss how you can programmatically use the Device Portals' REST APIs to test your apps. It is very common for developers to use the same set of devices during their testing. Using the Device Portal’s browser UX to deploy your app is a good ad-hoc solution.
If deploying the app to your devices is a regular occurrence, using the Device Portal REST APIs is a more efficient solution. For enthusiasts among you, you can build your own custom automation tools using these APIs directly. Below we will go through some sample code to show you how you can use REST APIs to write your own tool to manage your target devices.
To expedite the process of building custom tools, the Device Portal team have released an open source solution called the wrapper project. It is available on GitHub and as a NuGet package. The wrapper project is a C# layer atop the REST APIs to provide an easy, simple way to connect and manage your devices. The wrapper project takes care of all the complexities of building multipart REST API calls, error handling and retry logic.
Connect to Device
Before we can perform any actions on the device, we need to first connect to the target device. Below is the snippet that allows you to connect -
DevicePortal portal;
portal = new DevicePortal(
new DefaultDevicePortalConnection(
this.address.Text, //Target Device IP Address
this.username.Text, //Authentication creds
this.password.Password //Authentication creds
));
Install an App
portal.InstallApplicationAsync(
myAppName, //App Name[required]
packagePath.Text, //Package file name [required]
null, //List of dependency file names [required]
null, // Certificate file name
500, // Freq of install state check
15, // Operation timeout
true // uninstall previous version
));
Get Installed Apps
Task getInstalledAppsTask = new Task(
async () =
{
try
{
AppPackages appPackages = await portal.GetInstalledAppPackagesAsync();
foreach (PackageInfo package in appPackages.Package)
{
// List installed apps
};
}
catch (Exception ex)
{
Console.WriteLine("Failed to get installed app info.");
Console.WriteLine(ex.GetType().ToString() + " - " + ex.Message);
}
});
getInstalledAppsTask.Start();
Uninstall an App
portal.UninstallApplicationAsync(
package.FamilyName //Package family name
));
We have a sample UWP and win32 clients that use the Device Portal wrapper project and show to call the REST APIs using the wrapper functions. The full sample solutions are also available on GitHub. Please fork the project and create your own Device Portal tools.
If you have any questions regarding how the APIs should be used, post your questions or any comments below.
Thanks,
Chaitanya Donthini, Program Manager - Windows Developer Platform