1

I'm using Selenium in C# to retreive the data from a database with 2 rows of data with the columns; UserID, UserName, and Password. 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. 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. Any help would be greatly appreciated. Thank you very much.

12
  • blog.codinghorror.com/… Commented Oct 18, 2016 at 13:55
  • Then have a separated function to open a new page inside the Loop and send the Login/password there to a new page each time. Commented Oct 18, 2016 at 13:57
  • your while loop is fine. But why are you entering a 2nd user before you are done testing the first? Commented Oct 18, 2016 at 13:57
  • @NeilN I have multiple rows of data, the problem I'm having is that I'm finished with the first test and want to open a new test with the second row of data but it tries to overwrite the first row's data when it hits the next page instead of restarting the whole process. I'm not trying to enter the 2nd user before I'm done testing the first. Commented Oct 18, 2016 at 14:03
  • Right, I get that... but you don't have any code to return to the start of the test. You just submit the form and start entering the next record. You need to add at least two steps: 1) confirm that the login worked, 2) return to the start of the test. Then you can enter the next record Commented Oct 18, 2016 at 14:05

1 Answer 1

2

Heres is the pseudo code for what you want to do:

  1. Load data
  2. Loop over data:

    a. Enter username

    b. enter password

    c. press enter

    d. wait for page load

    e. confirm login worked

    f. load login page again.

add in the missing steps and you should be good to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.