Skip to content

Instantly share code, notes, and snippets.

@MatisseHack
Last active November 3, 2016 18:44
Show Gist options
  • Select an option

  • Save MatisseHack/2fceb3c96a17fc490eaa to your computer and use it in GitHub Desktop.

Select an option

Save MatisseHack/2fceb3c96a17fc490eaa to your computer and use it in GitHub Desktop.
Use a csv file for login credentials in UITest
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using Xamarin.UITest;
namespace YourNamespace
{
public abstract class LogInSample
{
protected void LogIn()
{
var user = GetUserCredentials();
// Log in with user.Password and user.Username
}
private User GetUserCredentials(bool randomizeLocalUser = true, bool reuseUsersInXTC = false)
{
var deviceIndex = Environment.GetEnvironmentVariable("XTC_DEVICE_INDEX");
var accounts = new List<string[]>();
// TODO Must set accounts.csv's build action to "EmbeddedResource"
using (var accountsStream = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream($"{this.GetType().Namespace}.accounts.csv")))
{
string line;
while ((line = accountsStream.ReadLine()) != null)
accounts.Add(line.Split(','));
}
int userNumber;
if (deviceIndex == null)
userNumber = randomizeLocalUser ? new Random().Next(0, accounts.Count) : 0;
else
userNumber = reuseUsersInXTC ? int.Parse(deviceIndex) % accounts.Count : int.Parse(deviceIndex);
if (userNumber >= accounts.Count)
throw new IndexOutOfRangeException($"Only enough logins for {accounts.Count} users, index {userNumber} is out of range.");
return new User(accounts[userNumber][0], accounts[userNumber][1]);
}
private class User
{
public string Username { get; set; }
public string Password { get; set; }
public User(string username, string password)
{
Username = username;
Password = password;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment