[英]Using Command Line Arguments in C# Unit Testing
I'm running C# unit tests to run selenium tests and in this I need to sign into different user accounts. 我正在运行C#单元测试以运行硒测试,因此我需要登录不同的用户帐户。 I'm trying retrieve the username and password as command line arguments but cannot find a way to do this in C# visual studio unit tests. 我正在尝试将用户名和密码作为命令行参数进行检索,但是找不到在C#visual studio单元测试中执行此操作的方法。
Does anyone know how I could do this? 有人知道我该怎么做吗?
Part of my code: 我的部分代码:
[TestMethod]
public void Login()
{
driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(this.baseURL);
driver.FindElementById("ctl00_ContentPlaceHolder1_Login1_UserName").SendKeys(userID);
driver.FindElementById("ctl00_ContentPlaceHolder1_Login1_Password").SendKeys(password);
driver.FindElementById("ctl00_ContentPlaceHolder1_Login1_LoginButton").Click();
}
Here the "userID" and the "password should be my command line arguments. 这里的“ userID”和“ password”应该是我的命令行参数。
Thank you in advance for your help. 预先感谢您的帮助。
In a unit test you should not use variable input from the command line but have it be constant. 在单元测试中,您不应使用来自命令行的变量输入,而应使其恒定。
For example your test should be: 例如,您的测试应为:
[TestMethod]
public void Login()
{
driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(this.baseURL);
driver
.FindElementById("ctl00_ContentPlaceHolder1_Login1_UserName")
.SendKeys("testuser");
driver
.FindElementById("ctl00_ContentPlaceHolder1_Login1_Password")
.SendKeys("testpass");
driver
.FindElementById("ctl00_ContentPlaceHolder1_Login1_LoginButton")
.Click();
}
You should then ensure that the database is properly mocked or initialized with the correct state before the test is run. 然后,您应该确保在运行测试之前,已正确模拟数据库或使用正确的状态初始化了数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.