C# 中的应用程序配置文件问题
我正在使用 selenium 和 c# 为网站编写自动化测试。我仍在学习 selenium 和 c#,并时不时地遇到问题。我不确定什么是好方法。 c# 中的 app.config 文件是否是存储网站中对象的所有 xpath 的好主意?或者将数百个这样的对象定义存储在像 app.config 这样的 xml 文件中不是一个好主意。另外,现在,我有:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Machine" value="localhost"/>
</appSettings>
</configuration>
并且,我正在类中使用一个方法来从此 app.config 文件中获取值:
public string GetAppConfig(string key)
{
return ConfigurationManager.AppSettings[key].ToString();
}
这是从 app.config 中检索值的好方法吗?或者有更好的方法吗?
预先感谢您的帮助。我真的很困惑我应该做什么来存储 xpath。我不想做类似的事情:
Selenium.Click(\\id="...."\)
因为如果以后网站中的 xpath 发生变化,那么就很难改变。
I am using selenium with c# to write automated tests for a website. I am still learning selenium and c#, and getting into issues here and there. I am not sure what would be a good approach. Would app.config file in c# be a good idea to store all xpaths of objects in the website? Or is it not a good idea to store hundreds of these object definitions in an xml file like app.config. Also, right now, I have:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Machine" value="localhost"/>
</appSettings>
</configuration>
and, I am using a method in my class to get the values from this app.config file:
public string GetAppConfig(string key)
{
return ConfigurationManager.AppSettings[key].ToString();
}
Is this a good way to retrieve values from app.config. Or is there a better way to do it?
Thanks in advance for your help. I am really confused to as what I should be doing to store xpaths. I do not want to do something like:
Selenium.Click(\\id="...."\)
because that would be difficult to change if the xpaths change in the website later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽可能使用内置的
ConfigurationManager
,如果不需要,请避免自行部署。您可以将此帮助器类设为静态,以便可以直接调用它而无需创建新实例。请注意,在您的代码中,如果密钥不存在,它将引发异常,根据您的要求,您可能想要捕获它?
Use the built in
ConfigurationManager
where possible, avoid rolling your own if you don't need to. You can make this helper class static so it can be called direct without creating a new instance.Note that in your code, if the key doesn't exist it will throw an exception, depending on your requirements you may want to catch it?
您可以为此使用配置文件,但必须处理不必要的 xml。
就我个人而言,我只会编写一个简单的文件解析器来读取 key=property (逐行):
.NET 可以加载并解析与 Java Properties 类等效的属性文件吗?
You could use the config file for this, but would have to deal with unessessary xml.
Personally I would just write a simple file parser that reads key=property (line by line):
Can .NET load and parse a properties file equivalent to Java Properties class?