简体   繁体   English

C#Selenium从数据库(SQL Server)读取完整数据?

[英]C# Selenium Reading the complete data from a Database (SQL Server)?

I'm using Selenium in C# to retreive the data from a database with 2 rows of data with the columns; 我在C#中使用Selenium从数据库中检索包含2行数据和2列数据的数据。 UserID, UserName, and Password. 用户ID,用户名和密码。 I'm just getting the UserName and Password from the rows to enter into the webpage. 我只是从行中获取用户名和密码以进入网页。 I'm able to return the first row of my Database but I'm trying to have my code run as many times as I have rows of data in my database. 我能够返回数据库的第一行,但是我试图使我的代码运行的次数与数据库中数据行的运行次数相同。 Here is my code so far. 到目前为止,这是我的代码。

public class DBConnection
    {
    static SqlConnection con;
    static SqlDataReader dr;
    static SqlCommand cmd;

      public static void DBConnect()
    {
        ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["DB_Table"];
        String connectionString = connection.ConnectionString;
        try
        {
            con = new SqlConnection(connectionString);
            con.Open();
            cmd = new SqlCommand("SELECT UserName, Password FROM dbo.Users", con);

            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string username = dr["UserName"].ToString();
                Console.WriteLine(username);
                string password = dr["Password"].ToString();
                Console.WriteLine(password);

                Thread.Sleep(5000);
                AUT.Browser.FindElement(By.Name("username")).Click();
                AUT.Browser.FindElement(By.Name("username")).Click();
                AUT.Browser.FindElement(By.Name("username")).Clear();
                Thread.Sleep(1000);
                AUT.Browser.FindElement(By.Name("username")).SendKeys(username);
                AUT.Browser.FindElement(By.Name("password")).Clear();
                AUT.Browser.FindElement(By.Name("password")).SendKeys(password);
                AUT.Browser.FindElement(By.Name("password")).SendKeys(Keys.Enter);
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Error" + e.Message);
        }
        finally
        {
            con.Close();
        }
    }
}

This code retrieves the correct data from the Database but it doesn't get the multiple rows of data. 这段代码从数据库中检索了正确的数据,但是没有获取多行数据。 It only grabs the first row and then it tries to overwrite the data using the second row of data from the first row after the webpage reaches the next page which causes my Test to fail. 它仅抓取第一行,然后在网页到达下一页后尝试使用第一行的第二行数据覆盖数据,这导致我的测试失败。 I'm trying to have it reopen the webpage and reenter another userName and Password from the 2 or more rows of data I have in my database after it opens and reads the first row of data. 我试图让它重新打开网页,并在打开并读取第一行数据后,从我数据库中的2行或更多行数据中重新输入另一个userName和Password。 My guess is that I would have to close and reopen the connection in my while loop, but I've tried that and I'm having no luck. 我的猜测是,我必须在while循环中关闭并重新打开连接,但是我已经尝试过了,但是没有运气。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you very much. 非常感谢你。

Heres is the pseudo code for what you want to do: 这是您要执行的操作的伪代码:

  1. Load data 载入资料
  2. Loop over data: 循环数据:

    a. 一种。 Enter username 输入用户名

    b. b。 enter password 输入密码

    c. C。 press enter 按Enter

    d. d。 wait for page load 等待页面加载

    e. e。 confirm login worked 确认登录成功

    f. F。 load login page again. 再次加载登录页面。

add in the missing steps and you should be good to go. 添加缺少的步骤,您应该一切顺利。

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

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