Monday, June 22, 2015

How to perform wait in selenium

Using the thread,sleep user can pause the currant thread.But handling a simple waiting for a element is a bit advance thing,
Because this need more resources from the computer(mainly ram usage)

1).Thread.Sleep(5000);


Using the webdriverwait:

2).WebDriverWait wait = new WebDriverWait(<webdriver>,<timetowait>);
eg:

   WebDriverWait wait = new WebDriverWait(WebDriverInti.driver, TimeSpan.FromSeconds(5000));

   wait.Until(ExpectedConditions.ElementExists(By.XPath("//*[@id=\"rdiv\"/h3/a")));



Using webdriverwait: (More advance way)

3).
WebDriverWait wait = new WebDriverWait(WebDriverInti.driver, TimeSpan.FromSeconds(5000));
wait.Until<IWebElement>((d) =>
            {
              IWebElement element = WebDriverInti.driver.FindElement(By.XPath("//*[@id=\"rso\"]/div[2]/li[1]/div/h3/a"));
                if (element.Displayed && element.Enabled)
                {
                    return element;
                }

                return null;

            });


Using webdriverwait: (Friends way)

_driver = new ChromeDriver(@"C:\ChromeDriver", options, TimeSpan.FromSeconds(180));

Wednesday, June 17, 2015

Passing values to a "Authentication Required" alert box on selenium automation

Below is a sample "Authentication Required" alert box which we need to pass values.
In standard selenium automation we cant find the elements on "Authentication Required" alert directly, there for we cant communicate with the alert box and our test case will fail.Because cant move ahead without giving the correct credentials to the  "Authentication Required" alert box.
There are several defendant methods which are widely use in java and .net .Some methods might not successfully work based on the development framework or the selected web driver.

I will be given a easy way to pass values to Authentication alert box in .net development framework.
To do this we need to have a 3rd party library called "AutoItX3.dll".Download the latest EXE from here,().
  1. Install the AutoIt.exe and keep the installed location in your mind in order to get the AutoItX3.dll as a reference to our project in latter part of this article.
  2. In your selenium project create a separate class to do all the steps in order to handle the  "Authentication Required" alert box.
  3. Add the AutoItX3.dll as a reference to the selenium project.(as default this dll file will be available under the (C:\Program Files (x86)\AutoIt3\AutoItX ).
  4. Use the reference by adding the namespace "using AutoItX3Lib" at the top of the class.
  5. Now by using AutoIt.dll we can communicate with the "Authentication Required" alert box using below piece of code.

     var AutoIT = new AutoItX3();
     IWebDriver driver = new FirefoxDriver();
     Thread.Sleep(1000);
     driver.Url ="your desired wep page url/default.aspx";//required web page.
    //Wait for the authentication window to appear, then send username and password
     AutoIT.WinWait("Authentication Required");
     AutoIT.WinActivate("Authentication Required");
     AutoIT.Send("chathura");//username
     AutoIT.Send("{TAB}");//TAB key on the key board
     AutoIT.Send("Welcome@123");//password
     AutoIT.Send("{ENTER}");//enter key on the key board.
  6. Now user will directed to the required web page with the given credentials.

Sunday, June 7, 2015

Test Attributes in Unit Test

This is a sample well coded test class.
This contain the correct flow of using Unit Test Attributes.
  • [AssemblyInitialize()]
  • [ClassInitialize()]
  • [TestInitialize()]
  • [TestCleanup()]
  • [AssemblyCleanup()]
  • [TestMethod()]

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SampleClassLib;
using System;
using System.IO;
using System.Windows.Forms;

namespace TestNamespace
{
   [TestClass()]
   public class DivideClassTest
   {
      [AssemblyInitialize()]
      public static void AssemblyInit(TestContext context)
      {
         MessageBox.Show("Assembly Init");
         }

      [ClassInitialize()]
      public static void ClassInit(TestContext context)
      {
         MessageBox.Show("ClassInit");
      }

      [TestInitialize()]
      public void Initialize()
      {
         MessageBox.Show("TestMethodInit");
      }

      [TestCleanup()]
      public void Cleanup()
      {
         MessageBox.Show("TestMethodCleanup");
      }

      [ClassCleanup()]
      public static void ClassCleanup()
      {
         MessageBox.Show("ClassCleanup");
      }

      [AssemblyCleanup()]
      public static void AssemblyCleanup()
      {
         MessageBox.Show("AssemblyCleanup");
      }

      [TestMethod()]
      [ExpectedException(typeof(System.DivideByZeroException))]
      public void DivideMethodTest()
      {
         DivideClass target = new DivideClass();
         int a = 0; 
         int actual;
         actual = target.DivideMethod(a);
      }
   }
}



ref:https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute.aspx