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.

2 comments:

  1. Hi,
    Very nice explanation.
    But I am just curious to know, is there any alternative approach we can use, instead of creating an Environment variable in c#?Like passing the path of "C:\SikuliX" folder somewhere.
    As i don't want to go for creating an environment variable in all the machines.

    ReplyDelete