即使存在该过程,也会出现“ System.Data.SqlClient.SqlException:无法找到存储过程”

时间:2020-06-16 10:12:04

标签: c# selenium automated-tests

因此,我正在学习如何使用Selenium来测试项目,并且想将存储过程中的值与输入值进行比较。我已经检查了存储过程(XE_SEARCH),但不知道它出了什么问题,使我有此错误。我尝试在同一数据库中使用另一种过程(CM_XE_Search,这是一个旧的过程,我们不再使用它),并且运行良好。我提供了照片,以便您更好地解决问题。有人可以帮助我吗?非常感谢。

enter image description here

enter image description here

enter image description here

enter image description here

这是代码

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Linq;
using Group11.XE.Test.XEDTO;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;
using Group11.XE.Test.Helper;


namespace Group11.XE.Test
{
    [TestClass]
    public class CreateXE : Load
    {

        public CreateXE()
        {
            driver = new ChromeDriver();
            homeURL = "http://localhost:4200";

        }

        [TestMethod]
        [DataRow("Exciter", "Đen", 2, "Ex 150", "71C4-45677", 42000000, 2, "Xe vua mua", 135,"Yamaha", 2019)]
        public void Create_XE_With_OK_Status(String Name, String Color, int Seats, String Model, String License, int Price, int Consumption, String Notes, int Mspeed, String Manufacturer, int Manufacture_year)
        {
            String Code = (new Random().Next(1, 99999999)).ToString();


            Login();

            driver.Navigate().GoToUrl(homeURL + "/app/admin/xe-group11-add");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(7);
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
            wait.Until(e => e.FindElement(By.Name("xE_CODE")));
            Thread.Sleep(8000);

            //Act;
            driver.FindElement(By.Name("xE_CODE")).SendKeys(Code);
            driver.FindElement(By.Name("xE_NAME")).SendKeys(Name);
            driver.FindElement(By.Name("xE_COLOR")).SendKeys(Color);
            driver.FindElement(By.Name("xE_SEATS")).SendKeys(Seats.ToString());
            driver.FindElement(By.Name("xE_MODEL")).SendKeys(Model);
            driver.FindElement(By.Name("xE_LICENSE_PLATE")).SendKeys(License);
            driver.FindElement(By.Name("xE_PRICE")).SendKeys(Price.ToString());
            driver.FindElement(By.Name("xE_CONSUMPTION")).SendKeys(Consumption.ToString());
            driver.FindElement(By.Name("xE_NOTES")).SendKeys(Notes);
            driver.FindElement(By.Name("xE_MAX_SPEED")).SendKeys(Mspeed.ToString());
            driver.FindElement(By.Name("xE_MANUFACTURER")).SendKeys(Manufacturer);
            driver.FindElement(By.Name("xE_MANUFACTURE_YEAR")).SendKeys(Manufacture_year.ToString());
            //driver.FindElement(By.Name("xE_STATUS")).SendKeys(Status);

            IWebElement comboBox = driver.FindElement(By.Name("xE_STATUS"));
            SelectElement selectedValue = new SelectElement(comboBox);
            selectedValue.SelectByIndex(0);
            String Status = selectedValue.ToString();

            //get value of select element



            IWebElement ele = driver.FindElement(By.XPath("/html/body/app-root/ng-component/div/div/div[2]/ng-component/form/ul/li[1]"));
            IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
            executor.ExecuteScript("arguments[0].click();", ele);


            //Check with value of DB
            //CM_XE_DTO input = new CM_XE_DTO(Code, Name, Origin);
            XE_DTO finalRow = DataProvider.Instance.GetData<XE_DTO>("XE_SEARCH", new { XE_CODE = Code, XE_Name = Name, XE_COLOR = Color, XE_SEATS = Seats, XE_MODEL = Model, XE_LICENSE_PLATE = License, XE_PRICE = Price, XE_CONSUMPTION = Consumption, XE_NOTES = Notes, XE_MAX_SPEED = Mspeed, XE_MANUFACTURER = Manufacturer, XE_MANUFACTURE_YEAR = Manufacture_year, XE_STATUS = Status }).ToList().FirstOrDefault();

            //Assert
            Assert.AreEqual(Code, finalRow.XE_CODE);
            Assert.AreEqual(Name, finalRow.XE_NAME);
            Assert.AreEqual(Color, finalRow.XE_COLOR);
            Assert.AreEqual(Seats, finalRow.XE_SEATS);
            Assert.AreEqual(Model, finalRow.XE_MODEL);
            Assert.AreEqual(License, finalRow.XE_LICENSE_PLATE);
            Assert.AreEqual(Price, finalRow.XE_PRICE);
            Assert.AreEqual(Consumption, finalRow.XE_CONSUMPTION);
            Assert.AreEqual(Notes, finalRow.XE_NOTES);
            Assert.AreEqual(Mspeed, finalRow.XE_MAX_SPEED);
            Assert.AreEqual(Manufacturer, finalRow.XE_MANUFACTURER);
            Assert.AreEqual(Manufacture_year, finalRow.XE_MANUFACTURE_YEAR);
            Assert.AreEqual(Status, finalRow.XE_STATUS);

            driver.Close();

        }
}

数据提供者

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Dapper;
using Group11.XE.Test.Helper.DTO;

namespace Group11.XE.Test.Helper
{
    class DataProvider
    {
        private static DataProvider instance;

        private DataProvider() { }
        internal static DataProvider Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataProvider();
                }
                return instance;
            }

            private set => instance = value;
        }//Cap Instance
        string conection = "Server=tcp:quanlitaisan.database.windows.net,1433;Initial Catalog=QuanLiTaiSan;Persist Security Info=False;User ID=QLTS_Dev;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";



        private List<ProcedureParamInfo> GetParamInfos(IDbConnection conn, string procedureName)
        {
            var rr = conn.Query<ProcedureParamInfo>($"select PARAMETER_NAME, PARAMETER_MODE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH from information_schema.parameters where specific_name = '{procedureName}'");
            return rr.ToList();
        }

        public List<T> GetData<T>(string procedureName, object parameter)
        {
            List<T> result;

            using (IDbConnection con = new SqlConnection(conection))
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();

                var paramsInfo = GetParamInfos(con, procedureName);

                DynamicParameters parameters = new DynamicParameters();

                var properties = parameter.GetType().GetProperties();

                foreach (var param in paramsInfo)
                {
                    var property = properties // nsx_code == nsx_code
                                    .Where(x => x.Name.ToLower() == param.PARAMETER_NAME.Replace("@", "").ToLower())
                                    .FirstOrDefault();
                    if (property == null)
                    {
                        continue;
                    }
                    var debug1 = property.GetValue(parameter);
                    parameters.Add(param.PARAMETER_NAME, property.GetValue(parameter));
                }

                result = con.Query<T>(procedureName, parameters, null, true, null, System.Data.CommandType.StoredProcedure).ToList();
            }

            return result;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

确定要连接到正确的数据库吗?

相关问题