Robert Petz
Robert Petz

Reputation: 2764

Xamarin Forms isn't setting StyleId for UITest

I'm trying to get Xamarin Forms working with UITest and I cannot seem to get the StyleId property to be set in iOS (I'm don't have licensing for Android).

I've followed all of the documentation and these are the steps I've taken thus far:

AppDelegate:

static readonly IntPtr setAccessibilityIdentifier_Handle = Selector.GetHandle("setAccessibilityIdentifier:");

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    Forms.Init();

    Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) => {
        // http://developer.xamarin.com/recipes/testcloud/set-accessibilityidentifier-ios/
        if (null != e.View.StyleId) {
            e.NativeView.AccessibilityIdentifier = e.View.StyleId;
            Console.WriteLine("Set AccessibilityIdentifier: " + e.View.StyleId);
        }
    };

    ...

    #if DEBUG
    Xamarin.Calabash.Start();
    #endif

    ...
}

XAML ContentPage:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="Ignite.Abacus.Mobile.OptionsView"
             Title="Options">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Test" StyleId="Test" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Now, here's the kicker:

If I debug the application I can clearly see the StyleId is getting set because the Console.WriteLine call in the Forms.ViewInitialized handler in AppDelegate is writing to the application output. But if I debug the unit test I have and query it from the REPL it does not appear to be wiring up the StyleId property because app.Query(c => c.Marked("Test")) returns zero results.

I'd love to see the console output of the application when running it under test scope - but it does not appear to output anything to the Application Output window or the Test Results window (with output toggled on)...

Any ideas as to what I am missing?

Upvotes: 1

Views: 1797

Answers (2)

Geoffrey Huntley
Geoffrey Huntley

Reputation: 506

Ran into this today; there is a simpler workaround than manually "File -> Reset Simulator" and that is to configure your [SetUp] to do it for you automatically:

using System.Diagnostics;

[SetUp]
public override void SetUp()
{
    const string simulatorId = "2261E5E1-758A-4967-8BF2-181E8099379F"; // iPhone 5s iOS 8.2
    ResetSimulator(simulatorId);

    // an API key is required to publish on Xamarin Test Cloud for remote, multi-device testing
    app = ConfigureApp.iOS.AppBundle(PathToIPA).DeviceIdentifier(simulatorId).EnableLocalScreenshots().ApiKey(Constants.XamarinTestCloudApiKey).StartApp();
}

public static void ResetSimulator(string deviceId)
{
    var shutdownProcess = Process.Start("xcrun", string.Format("simctl shutdown {0}", deviceId));
    shutdownProcess.WaitForExit();
    var eraseProcess = Process.Start("xcrun", string.Format("simctl erase {0}", deviceId));
    eraseProcess.WaitForExit();
}

Upvotes: 2

Robert Petz
Robert Petz

Reputation: 2764

Ah-HA! I had a feeling this is what it was when I first started to see the issue, but thanks to the question asked here I was able to find out what I needed to do to resolve this.

Basically - I had started to create unit tests using UITest without any StyleId properties set - then I started to retroactively go through and add StyleId properties to my elements. What was happening was that the iOS Simulator was caching my application and not using the latest build. I did EVERYTHING from deleting the bin/obj folders in all of my projects and rebuilding them through Xamarin Studio and NOTHING was working so I assumed it was the UITest system after that.

In the question linked above I saw someone note that you need to go into the iOS Simulator and click 'iOS Simulator -> Reset Content and Settings' and it would then remove my cached application. I rebuilt and it all appears to be working now.

This was driving me up a wall though because it ONLY appears to have this problem when launched through the Unit Tests window in Xamarin Studio - if you run or debug the application directly (not in unit test scope) it does not cache the application. However, if you then re-launch the application through the Unit Tests window it will go back and use the cached application again.

I'll have to see if I can reproduce in a simple project and file a bug.

Upvotes: 1

Related Questions