Skip to content

Instantly share code, notes, and snippets.

@MatisseHack
Last active August 10, 2016 17:32
Show Gist options
  • Select an option

  • Save MatisseHack/001410e9c36074fbe820 to your computer and use it in GitHub Desktop.

Select an option

Save MatisseHack/001410e9c36074fbe820 to your computer and use it in GitHub Desktop.
using System;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.iOS;
using Xamarin.UITest.Queries;
namespace [YourNamespace]
{
public abstract class BasePage
{
protected readonly IApp app;
protected bool OnAndroid { get; private set; }
protected bool OniOS { get; private set; }
protected Func<AppQuery, AppQuery> Trait { get; set; }
protected BasePage()
{
app = AppInitializer.App;
OnAndroid = app.GetType() == typeof(AndroidApp);
OniOS = app.GetType() == typeof(iOSApp);
InitializeCommonQueries();
}
protected BasePage(Func<AppQuery, AppQuery> androidTrait, Func<AppQuery, AppQuery> iOSTrait)
: this()
{
if (OnAndroid)
Trait = androidTrait;
if (OniOS)
Trait = iOSTrait;
AssertOnPage(TimeSpan.FromSeconds(30));
app.Screenshot("On " + this.GetType().Name);
}
protected BasePage(string androidTrait, string iOSTrait)
: this(x => x.Marked(androidTrait), x => x.Marked(iOSTrait))
{
}
/// <summary>
/// Verifies that the trait is still present. Defaults to no wait.
/// </summary>
/// <param name="timeout">Time to wait before the assertion fails</param>
protected void AssertOnPage(TimeSpan? timeout = default(TimeSpan?))
{
if (Trait == null)
throw new NullReferenceException("Trait not set");
var message = "Unable to verify on page: " + this.GetType().Name;
if (timeout == null)
Assert.IsNotEmpty(app.Query(Trait), message);
else
Assert.DoesNotThrow(() => app.WaitForElement(Trait, timeout: timeout), message);
}
/// <summary>
/// Verifies that the trait is no longer present. Defaults to a two second wait.
/// </summary>
/// <param name="timeout">Time to wait before the assertion fails</param>
protected void WaitForPageToLeave(TimeSpan? timeout = default(TimeSpan?))
{
if (Trait == null)
throw new NullReferenceException("Trait not set");
timeout = timeout ?? TimeSpan.FromSeconds(2);
var message = "Unable to verify *not* on page: " + this.GetType().Name;
Assert.DoesNotThrow(() => app.WaitForNoElement(Trait, timeout: timeout), message);
}
#region CommonPageActions
// Use this region to define functionality that is common across many or all pages in your app.
// Eg tapping the back button of a page or selecting the tabs of a tab bar
void InitializeCommonQueries()
{
if (OnAndroid)
{
}
if (OniOS)
{
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment