Last active
November 3, 2016 18:44
-
-
Save MatisseHack/2fceb3c96a17fc490eaa to your computer and use it in GitHub Desktop.
Use a csv file for login credentials in UITest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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