I apologize for my english is not the best.
Im trying to implement a dynamic user data system for my c# page object model automation framework. Im trying to create a JSON system where login data like username and password can be passed from a JSON file. I am running this on an IOS Emulator. The problem Im having is the username and password never make it from the json file and the username and password boxes never get filled in. It works perfectly when I use a string or default value. Please help if you can I greatly appreciate.
I have this method in my global methods which should deserialize my json file data:
public class UserCredentials
{
public string Username { get; set; }
public string Password { get; set; }
}
public class CredentialUtilities
{
private static readonly string ProfileFilePath = "FakePath";
public static UserCredentials CurrentCredentials { get; private set; } // Static property to hold credentials
public static UserCredentials LoadCredentials()
{
if (!File.Exists(ProfileFilePath))
{
Console.WriteLine("JSON file not found.");
return null;
}
string jsonString = File.ReadAllText(ProfileFilePath);
UserCredentials credentials = System.Text.Json.JsonSerializer.Deserialize<UserCredentials>(jsonString);
if (credentials == null)
{
Console.WriteLine("Failed to deserialize JSON data.");
return null;
}
CurrentCredentials = credentials; // Set the static property
return credentials;
}
public static void DisplayCredentials()
{
if (CurrentCredentials != null)
{
Console.WriteLine($"Username: {CurrentCredentials.Username}");
Console.WriteLine($"Password: {CurrentCredentials.Password}");
}
else
{
Console.WriteLine("No credentials to display.");
}
}
And this is the json file it is picking data from.
{
"Username": "john.doe",
"Password": "s3cr3t"
}
Im attempting to call that method and pass the json data into this test where the send key credentials steps do not return any data into my emulator:
public bool LoginUseriOS()
{
// Load credentials from JSON file
UserCredentials credentials = CredentialUtilities.LoadCredentials();
bool step1Pass = false, step2Pass = false, step3Pass = false;
try
{
LogTest("Login Step 1: App loads and displays landing page, then clicks the 'Login' button. When the continue button appears, click 'Continue'");
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.loginButton)).Click();
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.continueButton)).Click();
PassStep();
step1Pass = true;
}
catch (Exception ex)
{
FailStep($"Unable to find sign in button on landing page or advance to B2C login page. Error: {ex.Message}");
}
try
{
LogTest("Login Step 2: Safari browser loads B2C login page, attempts to enter username, then password, then clicks 'Login'");
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.dismissKeyboard)).Click();
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.usernameTextBox)).SendKeys(credentials.Username);
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.dismissKeyboard)).Click();
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.passwordTextBox)).SendKeys(credentials.Password);
Driver.FindElement(MobileBy.AccessibilityId(Login_PageObjects.passwordTextBox)).SendKeys(Keys.Return);
PassStep();
step2Pass = true;
}
catch (Exception ex)
{
FailStep($"Unable to send credentials. Error: {ex.Message}");
}
Ive omitted some information for privacy reasons.
I expected the value to be passed from the json but nothing was entered in the username and password boxes.
Aidan Tegtmeier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.