我很抱歉我的英语不是最好的。
我正在尝试为我的 C# 页面对象模型自动化框架实现动态用户数据系统。我正在尝试创建一个 JSON 系统,其中可以从 JSON 文件传递用户名和密码等登录数据。我正在 IOS 模拟器上运行它。我遇到的问题是用户名和密码永远不会从 json 文件中生成,并且用户名和密码框永远不会被填写。当我使用字符串或默认值时,它工作得很好。如果可以的话请帮忙我非常感激。
我的全局方法中有这个方法,它应该反序列化我的 json 文件数据:
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.");
}
}
这是它从中选取数据的 json 文件。
{
"Username": "john.doe",
"Password": "s3cr3t"
}
我尝试调用该方法并将 json 数据传递到此测试中,其中发送密钥凭据步骤不会将任何数据返回到我的模拟器中:
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}");
}
出于隐私原因,我省略了一些信息。
我希望从 json 传递值,但用户名和密码框中没有输入任何内容。
using Newtonsoft.Json;
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 = JsonConvert.DeserializeObject<UserCredentials>(jsonString); //using newtonsoft json
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.");
}
}
}
您尝试过使用NewtonsoftJson吗?我觉得比较靠谱