visual-studioautomated-testsmauiappium-android

.net Maui - how to tell Test Explorer use emulated tablet for its tests?


running .net8 and maui, have the emulator working just fine, and it works in debug mode, too. I also have a physical tablet plugged in and that works just fine as well.

What I don't know how to do though, is tell Test Explorer to run my tests on the emulated tablet rather than the physical tablet. Put another way, where are the settings that tell Test Explorer to run tests on the physically connected tablet? I want to change those settings so that the tests will run on the emulated tablet. How do I do that?

I've been following this tutorial, which isn't exactly complete, but it's been my springboard to this question. I don't want to have to disconnect my tablet to switch to my emulator, since the emulator is a different device than the physical one. What in Test Explorer or in Appium do I need to change?


Solution

  • The way this works is a bit different than you're used to from how your debug your app. There is no easy way to set the device/emulator that you use, mostly because the Test Explorer is one way of running your tests, but it might as well happen through the command-line.

    What you want to do is go into your AppiumSetup.cs for the platform that you want to configure, in this case Android, and specify the specific device/emulator you want to use. See for instance this line in the sample project. Also pasted below for reference:

    // Specifying the avd option will boot the emulator for you
    // make sure there is an emulator with the name below
    // If not specified, make sure you have an emulator booted
    androidOptions.AddAdditionalAppiumOption("avd", "pixel_5_-_api_33");
    

    So basically you're telling the Appium driver to use this specific emulator in this case.

    If you want to use a physical device, change this to be:

    androidOptions.AddAdditionalAppiumOption("deviceName", "MyAndroidDeviceName")
    

    To be clear: if you want to use an emulator you will need to use avd which stands for Android Virtual Device. If you want to use a physical device change avd to deviceName. In both scenarios you will need to specify the name identifier for the emulator/device as a second parameter.

    If you don't want to do this, do not specify this option and make sure there is just 1 emulator booted (or 1 physical device connected and no emulator booted) and it will just use the emulator/device that it will find at the moment you start running the tests.

    Hope that helps!