Monday, April 17, 2017

Different methods to scroll down a window in selenium test (code is in C#)

Sometime we used to scroll down a window as per our automation requirement and we have following methods to do it:

Method 1: Using Action Call and keydown event
Method 2: Using Java Script
Method 3: using Action class and movetoelement function

Check the below code to use all three methods:

using System;
using OpenQA.Selenium.Chrome;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliSharp;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;


namespace UnitTestProject1
{
    [TestClass]
   
    public class ScrollDown
    {

        [TestMethod]
        public void TestMethod2()
        {
            ChromeDriver driver = new ChromeDriver(@"E:\Drivers");
            driver.Navigate().GoToUrl("http://robotframework.org/");


            // Method 1 to scroll down a window with action class and Key.Down even

            Actions action = new Actions(driver);

            for (int a = 1; a < 10; a = a + 1)
            {
                action.SendKeys(Keys.Down).Perform();
            }


            // Method 2 to scroll down a window with Java Script executor

            ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0,1050)");


            //Method 3 to scroll down a window with Action class. Find an element and move to that element

            IWebElement element = driver.FindElement(By.Id("chat"));
            Actions actions = new Actions(driver);
            actions.MoveToElement(element);
            actions.Perform();

        }
    }

}

Saturday, April 15, 2017

Sikuli with C# - Sikuli works when others fail


Writing this blog as I found it very beneficial. In one particular scenario, where I have to click on a tile (on a web application), I tried different solution but nothing works. I have heard about sikuli but never used it. Today I tried it and found it useful. It can be used with windows application or if something doesn’t work on web.

Step 1: Installation of sikuli – This is the main step in this whole blog as without it, this sikuli will not work.


In MS VS 2015, Tools > NuGet Package Manager > Manage NuGet Packages for Solution..
Under Browse tab, enter “Sikuli” into the search box. 















Select sikulisharp and install it. You can then validate it in the references.


Step 2: Now you want sikuli installer in your system too.
  •   Install Java on your machine

  •    Download and install sikuli

    1. Follow instructions in Sikuli page. http://www.sikulix.com/quickstart.html. Otherwise major steps are written below:
                                                           






  • Download sikuli from this location. https://launchpad.net/sikuli/sikulix/1.1.1                             
  • Create a folder in C drive and put this sikulixsetup-1.1.1.jar in that folder.
  • When you will click on highlighted/downloaded sikulixsetup-1.1.1.jar, it will download some more jars shown above and setup will start after complete download





  • Choose option ‘to run scripts from the command line – the file sikuli-scripts.jar must be installed’
b. Create an environment variable SIKULI_HOME that points to the folder where you installed Sikuli.
    a. Go to Start > Right click on my computer > Properties. Click Advanced System Settings. Click       Environment Variables






















    b. Under the system variable of the Environment variables window, click new and type in           ‘SIKULI_HOME’ and the Sikuli installation path in the variable name and value fields, respectively. Click OK



Step3: Now you are ready to write your scripts. Create a unit test file in your project and add following code in that.

using System;
using OpenQA.Selenium.Chrome;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliSharp;


namespace UnitTestProject1
{
    [TestClass]


    public class UnitTest1
    {
      
        [TestMethod]
        public void TestMethod1()

        {
            ChromeDriver driver = new ChromeDriver(@"E:\Automation\Drivers");
            driver.Navigate().GoToUrl("http://www.google.com");
          
            using (var session = Sikuli.CreateSession())
            {
               var clickme = Patterns.FromFile("E:\\img.png", 0.9f);
               session.Click(clickme);
            }
        }
    }
}

In this code, I am using an image file which I placed in E directory. This image is shown below.






Step 4: Run it. This code will open www.google.com and on this web page, it will click on this image.

Thursday, April 6, 2017

Parallel execution of scripts with TestNG

Parallel execution of scripts with TestNG


TestNG gives us the facility to execute multiple tests in parallel and two different methods of doing it are:

Method 1: Having multiple test method in one class file

Step 1: Create a class file with name “DummyTest1” and add following code in it.

public class DummyTest1

{

       public WebDriver driver;

  @Test
  public void main()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }
 
  @Test
  public void main1()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }

  @BeforeMethod

  public void beforeMethod()
  {
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://newtours.demoaut.com/");
  }

  @AfterMethod

  public void afterMethod()
  {
       driver.quit();
  }

}

Step 2: Add following code in testng.xml file. Make sure you add the right class name.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Regression 1">
    <classes>
      <class name="AutomationFrameworkForDummy.DummyTest1"/>
    </classes>
  </test>
</suite>


Step 3: Run it and you will see multiple browser window in parallel.

Method 2:  Having two test method in two class file.

Step 1: Create a class file and add following code in it.

public class Chrome_Search {
    WebDriver driver;
    
    @BeforeTest
       @Parameters("browser2")
     public void test1(String browser2)
     {
              if(browser2.equalsIgnoreCase("chrome"))
              {
                     System.setProperty("webdriver.chrome.driver", "E://Rajesh Document//AutomationFrameworks//WordPressAutomation//Drivers//chromedriver.exe");
                       driver =new ChromeDriver();
                       driver.get("www.google.com");
                      
                 } 
              else
              {
                     System.out.println("Google Chrome"); 
              }
         }
  
   }

Step 2: Create another class file and add following code in that class file.

public class Mozilla_OpenAmazon {
    WebDriver driver;
       @BeforeTest
       @Parameters("browser1")
    public void launchbrowser(String browser1)
     {
              if(browser1.equalsIgnoreCase("firefox"))//For Firefox we dont need to download any jar file as we have Inbuilt firefox jar files
              {
                     driver=new FirefoxDriver(); 
                  System.out.println("Hello Google..."); 
                  driver.get("https://www.amazon.com/");
                  driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Dolly");
                                                  
           }
              else
              {
                     System.out.println("Firefox"); 
              }
     }
              @AfterTest
              public void closeBrowser()
              {
                     driver.quit();
              }
}

Step 3: Add following code in testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite" parallel="tests">

  <test name="FirefoxTest" >
  <parameter name = "browser1" value="Firefox"/>
  <classes>
  <class name ="AutomationFrameworkForDummy.Mozilla_OpenAmazon"> </class>
  </classes>
  </test>
   
 
  <test name ="Chrome Test">
    <parameter name = "browser2" value="Chrome"/>
   <classes>
  <class name ="AutomationFrameworkForDummy.Chrome_Search"/>
  </classes>
   </test>
   </suite>


Step 4: Now Run it.