sing NextGen.Mobile.UITests.Barcodes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Xamarin.UITest;
using Query = System.Func;
namespace NextGen.Mobile.UITests
{
public abstract class BasePage
{
private readonly List locations;
public readonly IApp app;
protected BasePage(IApp app)
{
locations = new Locations().GetLocations();
this.app = app;
}
public void ScanBarcode(string barcode)
{
Thread.Sleep(1000);
if (!app.Invoke("SendScan", barcode).ToString().Equals("true"))
{
Thread.Sleep(2500);
app.Invoke("SendScan", barcode);
}
}
// Used to verify success of selection or barcode scan
public bool VerifyScanOrSelection(string item)
{
// app cannot see element if user cannot see it.
ScrollToItem(item);
var success = app.Query(x => x.Marked(item).All().Sibling().Marked("IsCompleted"))[0].Text;
Debug.WriteLine("Item selected is successful??? " + success);
return (success != null && success.Length > 0 && success.Equals("true"));
}
public BasePage ScanPrePostItem(string itemName, string itemBarcode)
{
WaitForElement(x => x.Marked(itemName), "Unable to locate preroute item from list.", 1000);
ScanBarcode(itemBarcode);
VerifyScanOrSelection(itemName);
return this;
}
public BasePage ScrollToItem(string item)
{
try
{
app.ScrollDownTo(item);
}
catch (Exception)
{
try
{
app.ScrollUpTo(item);
}
catch (Exception) { }
}
return this;
}
protected string GetLocationBarcodeFor(string locationName)
{
string locationBarcode = null;
foreach (Location lc in locations)
{
if (lc.LocationName.Equals(locationName))
{
locationBarcode = lc.LocationBarcode;
}
}
return locationBarcode;
}
protected IEnumerable GetSublocationsFor(string locationName)
{
return
from lc in locations
where lc.LocationName.Equals(locationName)
select lc.SublocationBarcode;
}
protected IEnumerable GetSublocationNameFor(string locationName)
{
return
from lc in locations
where lc.LocationName.Equals(locationName)
select lc.SublocationName;
}
protected IEnumerable GetSublocationTempFor(string locationName)
{
return
from lc in locations
where lc.LocationName.Equals(locationName)
select lc.SublocationTemp;
}
public BasePage Select(Query element, string timeoutMsg, int millisecondsBeforeTap = 5000)
{
WaitForElement(element, timeoutMsg, millisecondsBeforeTap);
app.Tap(element);
return this;
}
public BasePage WaitForElement(Query query, string timeoutMsg, int millisecondsBeforeTap = 5000)
{
TimeSpan timeOut = new TimeSpan(0, 0, 0, 15); // timeout after 15 seconds of not finding anything
TimeSpan retryFrequency = new TimeSpan(0, 0, 0, 0, 500); // retry every half second
TimeSpan postTimeOut = new TimeSpan(0, 0, 0, 0, millisecondsBeforeTap);
app.WaitForElement(query, timeoutMsg, timeOut, retryFrequency, postTimeOut);
return this;
}
public BasePage WaitForNoElement(Query query, string msg, int millisecondsBeforeTap = 5000)
{
TimeSpan timeOut = new TimeSpan(0, 0, 0, 15); // timeout after 15 seconds of not finding anything
TimeSpan retryFrequency = new TimeSpan(0, 0, 0, 0, 500); // retry every half second
TimeSpan postTimeOut = new TimeSpan(0, 0, 0, 0, millisecondsBeforeTap);
app.WaitForElement(query, msg, timeOut, retryFrequency, postTimeOut);
return this;
}
public bool IsOnPage(string pageTitle)
{
return pageTitle.Equals(app.Query(x => x.Class("AppCompatTextView"))[0].Text);
}
public bool IsSelectionPresentInField(string fieldName, string expectedText)
{
return expectedText.Equals(app.Query(x => x.Marked(fieldName))[0].Text);
}
public int GetNumberInString(string fieldName)
{
string textFromField = app.Query(x => x.Marked(fieldName))[0].Text;
string number = "";
for (int i = 0; i < textFromField.Length; i++)
{
if (Char.IsDigit(textFromField[i]))
number += textFromField[i];
}
int results = int.Parse(number);
return results;
}
}
}