Friday, 25 November 2011

Different setup fixtures for different browsers in selenium webdriver using NUnit Framework

 public abstract class testwithMultiBrowser
    {
        protected IWebDriver driver;
        [Test]
        public void test()
        {
            driver.Navigate().GoToUrl("http://google.co.in");
        }
    }
  [TestFixture]
  class testwithFirefox:testwithMultiBrowser
    {
      [SetUp]
      public void firefoxSetup()
      {
          driver = new FirefoxDriver();
          IWindow window = driver.Manage().Window;
          window.Size = new Size(1366, 768);
      }
      [TearDown]
      public void tearDown()
      {
          driver.Quit();
      }
    }
    [TestFixture]
    class testwithIE:testwithMultiBrowser
    {
      [SetUp]
      public void iesetUP()
      {
          driver = new InternetExplorerDriver();
      }
      [TearDown]
      public void tearDown()
      {
          driver.Quit();
      }
 }

Thursday, 24 November 2011

Run selenium test scripts in multiple browsers using NUnit Framework

[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(FirefoxDriver))]
public class testwithMultipleBrowsers<Testwebdriver> where Testwebdriver:IWebDriver ,new()
{
 IWebDriver driver;
 [SetUp]
 public void testsetup()
   {
    driver = new Testwebdriver(); 
    }
 [TearDown]
 public void teardownTest()
 {
  driver.Quit();
  }
  [Test]
  public void testMulitBrowser()
 {
  driver.Navigate().GoToUrl("http://google.co.in");
  driver.FindElement(By.Name("q")).SendKeys("testingtools");
  driver.FindElement(By.Name("btnG")).Click();
  }
}

Handling Ajax Autosuggests using selenium webdriver with CSharp

driver.Navigate().GoToUrl("http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx");
driver.FindElement(By.XPath("//input[contains(@id,'SampleContent_myTextBox')]")).SendKeys("test");
Thread.Sleep(TimeSpan.FromSeconds(2));
IWebElement element = driver.FindElement(By.XPath("//div[contains(@id,'master_contentplaceholder')]//div//ul"));
ReadOnlyCollection<IWebElement> texts=element.FindElements(By.TagName("li"));
IKeyboard key = ((IHasInputDevices)driver).Keyboard;
foreach(IWebElement text in texts)
{
  key.PressKey(Keys.ArrowDown);
  if (Equals("testMal", text.Text))
     {
        Console.WriteLine(text.Text);
        key.PressKey(Keys.Enter);
     }
     else
     {
        key.PressKey(Keys.ArrowDown);
        key.PressKey(Keys.Enter);
     }
  }

Wednesday, 23 November 2011

Data Driven using Excel Data W/O oledb driver in selenium webdriver with CSharp

for (int i = 2; i <= 4; i++)
{
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.LinkText("REGISTER")).Click();
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("D:\\Data.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
username = xlWorkSheet.get_Range("A" + i, "A" + i).Value2.ToString();
password = xlWorkSheet.get_Range("B" + i, "B" + i).Value2.ToString();xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
driver.FindElement(By.Name("email")).SendKeys(username);
driver.FindElement(By.Name("password")).SendKeys(password);
}

Data Driven using Excel Data with oledb connection in selenium webdriver with CSharp

string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "d:\\Userdata.xlsx" + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
 string selectusername = "SELECT *from [Sheet1$] ";
OleDbConnection con = new OleDbConnection(connectionstring);
OleDbCommand cmd = new OleDbCommand(selectusername, con);
con.Open();
OleDbDataReader thedata = cmd.ExecuteReader();
while (thedata.Read())
{
username = thedata[0].ToString();
password = thedata[1].ToString();
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.LinkText("REGISTER")).Click();
driver.FindElement(By.Name("email")).SendKeys(username);
driver.FindElement(By.Name("password")).SendKeys(password);
driver.FindElement(By.Name("confirmPassword")).SendKeys(password);
}


Data Driven using SQL database in selenium webdriver with CSharp

driver.Navigate().GoToUrl(baseURL);
SqlCommand comm = new SqlCommand();
comm.Connection = new SqlConnection("Data Source=DANJANEYULU\\SQLEXPRESS;" + "Initial Catalog=Userdata; Trusted_Connection=Yes"); //Windows Authentication
string sql = @"select Username,Password from Userdata";
comm.CommandText = sql;
comm.Connection.Open();
SqlDataReader cursor = comm.ExecuteReader();
while (cursor.Read())
{
  string username=cursor["Username"].ToString();
  string password = cursor["Password"].ToString();
  driver.Navigate().GoToUrl(baseURL);
  driver.FindElement(By.LinkText("REGISTER")).Click();
  driver.FindElement(By.Name("email")).SendKeys(username);
  driver.FindElement(By.Name("password")).SendKeys(password);
  driver.FindElement(By.Name("confirmPassword")).SendKeys(password);
}
comm.Connection.Close();



Tuesday, 22 November 2011

Connecting Remote System with Selenium webdriver using CSharp


Selenium standalone server should be run on the remote system.Then follow the procedure

namespace SeleniumFeatures
{
    [TestFixture]
    class SeleniumRemoteWebdriver
    {
        RemoteWebDriver driver;

        [SetUp]
        public void setupTest()
        {
driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),  DesiredCapabilities.Firefox());   // Change the ip iddress to target remote system ip address
        }
        [Test]
        public void Test()
        {
            driver.Navigate().GoToUrl("http://google.co.in");
            driver.FindElement(By.Name("q")).SendKeys("testing tutorials");
            driver.FindElement(By.Name("btnG")).Click();

        }
    }
}