Esempio n. 1
0
        /// <summary>
        /// This method is used to write the test results of each test run to a database.
        /// Update the Server, Database(in the dbName variable), User Id, and Password field in the connection string below
        /// The SqlCommand will also need to be updated with the tablename and the columns adjusted to match the database
        /// </summary>
        public static void LogResult()
        {
            var          si        = ScenarioContext.Current.ScenarioInfo;
            var          fi        = FeatureContext.Current.FeatureInfo;
            var          error     = ScenarioContext.Current.TestError;
            const string dbName    = "<databasename>";
            var          errorText = "";
            string       testResult;

            if (error != null)
            {
                errorText = error.Message;
            }
            if (TestPass && error == null)
            {
                testResult = "Pass";
            }
            else
            {
                testResult = "Fail";
            }
            if (WriteToDb)
            {
                using (var conn = new SqlConnection())
                {
                    conn.ConnectionString =
                        "Server=<server>;Database=" + dbName + ";User Id=<username>;Password=<password>";
                    conn.Open();
                    try
                    {
                        var command =
                            new SqlCommand(
                                "insert into dbo.<tableName> Values ('" + fi.Title + "','" + si.Title.Replace("'", "") + "',0,'" + MyTimeStamp +
                                "','" + StartTime + "','" + DateTime.Now + "','" +
                                errorText.Replace("\"", "") + "','" + _mainUrl + "','" + testResult + "','" + MyLog +
                                "','" + Browser.GetThreadID() + "','" + "0" + "')", conn);
                        Assert.IsTrue(command.ExecuteNonQuery() == 1);
                    }
                    catch (SqlException)
                    {
                        LogFunctions.WriteError("Did not get expected result from SQL.");
                        throw;
                    }
                    LogFunctions.WriteInfo("Result Logged to " + dbName + " Database");
                }
            }
        }