Search code examples
coded-ui-teststestautomationfx

Can I add multiple Test Methods in .cs in Coded UI


Can I add multiple test methods in one .cs file in code UI.

Below is my code. I have two features. 1. Login and Log off.

I have created one CodedUITest1.cs file where i an trying to add multiple methods. Is it really possible to do that

public class CodedUITest1 { public CodedUITest1() { }

    [TestMethod]
    public void Login()
    {
        this.UIMap.Login();
        this.UIMap.Assert_Login();
        this.UIMap.LogOff();
    }

    [TestMethod]
    public void LogOff()
    {
       this.UIMap.LogOff();
    }

Solution

  • Yes, you can have multiple tests in a test class. What is the problem you are noticing?

    Normally, I use the TestInitialize attribute to setup the common steps for all tests and then each test method does something different from that point and performs assertions, etc.

    public class LoginPageTests
    {
        BrowserWindow bw;
        [TestInitialize]
        public void GivenLoginPage()
        {
            bw = BrowserWindow.Launch("http://yoursite.com/loginPage");
        }
    
        [TestMethod]
        public void WhenSupplyingValidCredentials_ThenLoginSucceedsAndAccountsPageIsShown()
        {
            Assert.IsTrue(bw.Titles.Any(x => "Login"));
            HtmlEdit userNameEdit = new HtmlEdit(bw);
            userNameEdit.SearchProperties.Add("id", "userName");
            userNameEdit.Text = "MyUserName";
    
            HtmlEdit passEdit = new HtmlEdit(bw);
            passEdit.SearchProperties.Add("id", "pass");
            passEdit.Text = "MyPassword";
    
            HtmlButton loginButton = new HtmlButton(bw);
            Mouse.Click(loginButton);
    
            // probably can one of the WaitFor* methods to wait for the page to load
            Assert.IsTrue(bw.Titles.Any(x => "Accounts"));
        }
    
        [TestMethod]
        public void WhenNoPassword_ThenButtonIsDisabled()
        {
            HtmlEdit userNameEdit = new HtmlEdit(bw);
            userNameEdit.SearchProperties.Add("id", "userName");
            userNameEdit.Text = "MyUserName";
    
            HtmlButton loginButton = new HtmlButton(bw);
            Assert.IsFalse(loginButton.Enabled);
        }
    }