Skip to content

Instantly share code, notes, and snippets.

@willsam100
Last active February 11, 2019 18:13
Show Gist options
  • Save willsam100/53375a4b94b689f3feb8f989414a9ff8 to your computer and use it in GitHub Desktop.
Save willsam100/53375a4b94b689f3feb8f989414a9ff8 to your computer and use it in GitHub Desktop.
TickSpec AppInitializer.fs
namespace FsharpMvvmCross.UITests
open System
open System.IO
open Xamarin.UITest
open Xamarin.UITest.Queries
open NUnit.Framework
open TickSpec
open System.Reflection
open System.Runtime.ExceptionServices
module AppInitializer =
let mutable app: IApp = null
let droidProjectName = "Droid"
let apkName = "com.codingwithsam.FsharpMvvmCross.apk"
let apkPath = sprintf "../../../%s/bin/Debug/%s" droidProjectName apkName
let iOSProjectName = "iOS"
let ipaName = "FsharpMvvmCross.iOS.app"
let ipaPath = sprintf "../../../%s/bin/iPhoneSimulator/Debug/%s" iOSProjectName ipaName
let startApp (platform: Platform) =
if platform = Platform.Android then
ConfigureApp.Android
.ApkFile(apkPath)
.StartApp () :> IApp
else
ConfigureApp.iOS
.EnableLocalScreenshots()
// xcrun instruments -s devices
.DeviceIdentifier("42D35D2F-13A9-4029-BFC2-E557BCC71631")
.AppBundle(ipaPath)
.StartApp () :> _
/// Class containing all BDD tests in current assembly as NUnit unit tests
[<TestFixture>]
type FeatureFixture () =
/// Test method for all BDD tests in current assembly as NUnit unit tests
[<Test>]
[<TestCaseSource("Scenarios")>]
member __.Bdd (scenario:Scenario) =
if scenario.Tags |> Seq.exists ((=) "ignore") then
raise (new IgnoreException("Ignored: " + scenario.ToString()))
try
let platform =
match scenario.Tags |> Seq.contains "android", scenario.Tags |> Seq.contains "ios" with
| true, true -> failwith "Can't run both ios and android. Check your spelling for the tags"
| false, false-> failwith "Must run with platform either ios or android. Add one of: @android, @ios, @android_ios"
| true, false -> Platform.Android
| false, true -> Platform.iOS
AppInitializer.app <- AppInitializer.startApp platform
scenario.Action.Invoke()
with
| ex ->
eprintf "Failed: %s\n%s" ex.Message ex.StackTrace
sprintf "Failed: %s\n%s" ex.Message ex.StackTrace |> Console.WriteLine
raise ex
/// All test scenarios from feature files in current assembly
static member Scenarios =
let enhanceScenarioName parameters scenarioName =
let replaceParameterInScenarioName (scenarioName:string) parameter =
scenarioName.Replace("<" + fst parameter + ">", snd parameter)
parameters
|> Seq.fold replaceParameterInScenarioName scenarioName
let splitTags (tags: string[]) =
tags
|> Array.map (fun x -> x.Split("_"))
|> Array.concat
|> Array.map (fun x -> x.Replace("_", "").Trim().ToLower())
let isPlatform (name:string) tags (scenario:Scenario) feature =
if tags |> Seq.contains (name.ToLower()) then
let scenario =
{scenario with
Name = scenario.Name |> sprintf "%s: %s" name
Tags = tags |> Array.filter (fun x -> x = (name.ToLower())) }
(new TestCaseData(scenario))
.SetName(enhanceScenarioName scenario.Parameters scenario.Name)
.SetProperty("Feature", feature.Name.Substring(9))
.SetCategory(name) |> Some
else None
let createTestCaseData (feature:Feature) (scenario:Scenario) =
let tags = splitTags scenario.Tags
[isPlatform "Android"; isPlatform "iOS"]
|> List.choose (fun f -> f tags scenario feature)
|> Seq.foldBack (fun (tag:string) tests ->
tests |> List.map (fun data -> data.SetProperty("Tag", tag))) scenario.Tags
let createFeatureData (feature:Feature) =
feature.Scenarios
|> Seq.map (createTestCaseData feature)
|> Seq.concat
let assembly = Assembly.GetExecutingAssembly()
let definitions = new StepDefinitions(assembly.GetTypes())
assembly.GetManifestResourceNames()
|> Seq.filter (fun (n:string) -> n.EndsWith(".feature") )
|> Seq.collect (fun n ->
definitions.GenerateFeature(n, assembly.GetManifestResourceStream(n))
|> createFeatureData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment