Monday 27 January 2014

Create Database Connection using c-sharp in selenium webdriver.


 
Test Scanerio :  Login Page.

Find below statement : 


driver.Navigate().GoToUrl(baseURL + "/");

driver.FindElement(By.Id("Login")).Click();        //Open Login Page



driver.FindElement(By.Id("UserID")).SendKeys("UserName");

driver.FindElement(By.Id("Password")).SendKeys("Password");
driver.FindElement(By.Id("btnSubmit")).Click();   // Successfully Login.

Above statement was for hardcoded value.

Just few modification required for database connection using c#.

driver.Navigate().GoToUrl(baseURL + "/login.aspx");
Assert.AreEqual("Welcome", driver.Title); // Page title checkpoint
// DB connection Command                
SqlCommand comm = new SqlCommand();
comm.Connection = new SqlConnection("Server=192.168.02.1;Initial Catalog=BasantDB; uid=sa;pwd=basant123"); //Windows Authentication
string sqluserid = @"select userid, dbo.decrypt(password) as password from memberlogin";
          
            comm.CommandText = sqluserid;
            comm.Connection.Open();
            SqlDataReader cursor = comm.ExecuteReader();
            while (cursor.Read())
            {
               
                string username = cursor["UserId"].ToString();
                string password = cursor["Password"].ToString();
                driver.FindElement(By.Id("header1_hreflogin")).Click();
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
                driver.FindElement(By.Id("UserId")).Clear();
                driver.FindElement(By.Id("UserId")).SendKeys(username);
                driver.FindElement(By.Id("Password")).Clear();
                driver.FindElement(By.Id("Password")).SendKeys(password);
                driver.FindElement(By.Id("btnSubmit")).Click();
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(40));

                driver.FindElement(By.Id("header1_hrfLogout")).Click();

                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));      
             
            }
            comm.Connection.Close();

        }





No comments:

Post a Comment