简体   繁体   中英

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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM