简体   繁体   English

如何将SQL Server数据库与编码的UI测试一起使用?

[英]How do I use a SQL Server database with Coded UI Test?

First day learning Coded UI Test for Visual Studio 2017 Enterprise. 第一天学习Visual Studio 2017 Enterprise的编码UI测试。 I've got a calculator app that has a few tests and they all pass. 我有一个包含一些测试的计算器应用程序,它们都通过了。

Have another doctors surgery management system app with a login screen, username and passwords are stored on the database. 另一个具有登录屏幕的医生手术管理系统应用程序,用户名和密码存储在数据库中。

The idea is to have the table data for username and password as the parameters for the assertion. 想法是让用户名和密码的表数据作为断言的参数。

How do I add the database to the [Test method]? 如何将数据库添加到[Test方法]?

Heres my code 这是我的代码

Login Form 登录表单

    private void btnLogin_Click(object sender, EventArgs e)
    {


        //Try and open a connection with database and run the code
        try
        {

            //Create new instance of sql connection, pass in the connection string for BayOneSurgerySystem.mdf to connect to database.
            SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\davie\Documents\UniWork\Software Engineering\BayOneSurgerySystem1.0\Database\BayOneSystem.mdf;Integrated Security=True;Connect Timeout=30");

            //Create new instance of SQlCommand and pass in a query to be called to retrieve table data for username and passwords aswell as the connection object.
            SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Username = @username and Password = @password", conn);
            //This passes user input into @username and @password
            cmd.Parameters.AddWithValue("@username", txtBoxUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtBoxPassword.Text);

            //Open connection with database
            conn.Open();

            //Create new instance of dataSet to hold the data retrieved from sql query
            DataSet ds = new DataSet();
            //Create new instance of DataAdpater to retrieve the data pass in Sql command
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            //using DataAdapter fill in dataSet wiht data if user input and stored data matches
            da.Fill(ds);

            //Close the connection now data table is filled with username and password
            conn.Close();

            //declare bool, true if there is a match with database and user input
            bool loginSuccess = (ds.Tables[0].Rows.Count == 1);


            //if login success is true then open menu
            if (loginSuccess)
            {
                /*Change state of enum RoleTypes based on result from dataSet Role_ID column.
                 In UserRole table records are as follows: 
                 Role_ID 1 = PracticeManager
                 2 = Doctor
                 3 = Receptionist*/

                //Print role_ID to console to check that is been set.
                Console.WriteLine(ds.Tables[0].Rows[0]["Role_ID"]);

                try
                {
                    //Condition for the switch statement is: check Role_ID from UserRoles table
                    switch (ds.Tables[0].Rows[0]["Role_ID"])
                    {
                        //if the case is that Role_ID for the user logged in is 1 then run the function etc.
                        case 1:
                            {
                                Roles.Role = Roles.RoleType.practiceManager;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        case 2:
                            {
                                Roles.Role = Roles.RoleType.doctor;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        case 3:
                            {
                                Roles.Role = Roles.RoleType.receptionist;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        default:
                            break;

                    }
                }//Switch condition cannot be reached then catch exception and print to console.
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }


                Console.WriteLine("Logged in.");
                FrmMenu menu = new FrmMenu();
                menu.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Invalid username or password.", "Error!", MessageBoxButtons.RetryCancel);
                Console.WriteLine("Not logged in");
            }


        }

        //If connection cant be opened diplsay error message and catch exception and print to console
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            MessageBox.Show("Sorry can't connect");
        }



    }

Coded UI Test 编码的UI测试

 /// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{
    public CodedUITest1()
    {
    }

    [TestMethod]
    public void CodedUITestMethod1()
    {

    }


    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }
    private TestContext testContextInstance;

    public UIMap UIMap
    {
        get
        {
            if (this.map == null)
            {
                this.map = new UIMap();
            }

            return this.map;
        }
    }

    private UIMap map;
}

} }

[Data source (does the connection string go here?), Test method] [数据源(连接字符串在这里吗?),测试方法]

Thanks in advance! 提前致谢!

Coded UI supports several types of data source including CSV, Excel, SQL and XML. 编码的UI支持多种类型的数据源,包括CSV,Excel,SQL和XML。 For a CSV data sources and some general information on data driving please read this answer here on Stack Overflow. 有关CSV数据源和有关数据驱动的一些常规信息,请在Stack Overflow上阅读此答案 This (old) web page appears to be the main documentation of data sources. 该(旧) 网页似乎是数据源的主要文档。 The "Other types" section of the table on that web page refers to an MSDN article which gives connection strings for several other type of SQL database. 该网页上表的“其他类型”部分是指MSDN文章 ,该文章提供了其他几种SQL数据库类型的连接字符串。

For data held in SQL Express use DataSource and TestMethod attributes based on: 对于SQL Express中保存的DataSource ,请基于以下条件使用DataSourceTestMethod属性:

[DataSource("System.Data.SqlClient",
            "Data Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated Security=True",
            "Data", DataAccessMethod.Sequential),
 TestMethod]

Other types of SQL database use variations of the above. 其他类型的SQL数据库使用上述内容的变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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