我之前只使用过 Java,但需要在 C# 中设置一些测试。
在登录测试中,我喜欢有一个等待方法来等待设置登录 cookie。
在 Java 中我可以做类似的事情,但无法在 C# 中创建相同的内容,任何人都可以帮我将此代码转换为 C# 吗?
public void getTokenCookie(){
try {
wait.until(
new ExpectedCondition<Cookie>() {
@Override
public Cookie apply(WebDriver webDriver) {
Cookie tokenCookie = driver.manage().getCookieNamed("nameOfCookie");
if (tokenCookie != null) {
System.out.println("\nToken Cookie added: " + tokenCookie);
return tokenCookie;
} else {
System.out.println("waiting for cookie..");
return null;
}
}
}
);
} catch (Exception e){
System.out.println(e.getMessage());
fail("Failed to login, no cookie set");
}
}
Run Code Online (Sandbox Code Playgroud) 在控制台应用程序项目中,我尝试将值写入 app.config 文件并将其永久保存,但只能将其保存在内存中。我已经尝试了在 app.config 文件中写入值中的所有解决方案,但没有一个对我有用。
有什么想法可以使改变永久化吗?
应用程序配置文件:
<appSettings>
<add key="test" value="123456" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
具有两种仅将值保存到内存的方法的类:
public static class ConfigurationHelper
{
public static void SetSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
public static void SetSetting2(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
Run Code Online (Sandbox Code Playgroud)
写出更改的简单测试,但在重建项目或在记事本中打开 app.config 时,更改不会保存:
[Test]
public void FirstTest()
{
Console.WriteLine("Value before: " + ConfigurationHelper.Get<string>("test"));
ConfigurationHelper.SetSetting2("test", "NewValue");
Console.WriteLine("Value after: " + ConfigurationHelper.Get<string>("test"));
}
Run Code Online (Sandbox Code Playgroud)