Search This Blog

Tuesday, October 28, 2014

Drag and Drop objects to a specific location using Sikuli

Sometimes we need to drag and drop objects in order to automate certain functionality.For Eg: dropping files into upload area in order to get uploaded. We use 'Actions' class in selenium whereas when we need to drop an image on a specific location on the screen, i prefer Sikuli. Below code describes it :

      @Test  
      public static void dragAndDrop() throws FindFailed{  
           Screen scr = new Screen();  
           Pattern pt=new Pattern("dest.png");  
           scr.wait(pt,10);  
           Region iregion=scr.find("dest.png");  
           iregion.dragDrop(iregion,iregion.offset(new Location(566,363)));  
      }  
     


Specific location on the screen can be indicated using 'Location' class.It accepts x-cord and y-cord and returns the region.It follows the below flow :

  • Prepare the pattern using the image
  • Wait for pattern to be visible on the screen
  • Create a Screen Object
  • Locate the image region using new Screen().find()
  • Use dragDrop() to move the region with the help of Location class.
Note: Please take a screenshot of the image and use it with the above code and use SIKULI-IDE to simulate before code.

Page Object vs LoadableComponent

Page Object Pattern

As per selenium wiki : The Page Object pattern represents the screens of your web app as a series of objects.Here what we mean by objects is, these are the sample areas within a page. In simple words, Page Object model is the combination of Pages and its associated components.

Benefits

  • Maintainability is easy : When application represented as a series of pages, if we have a change of functionality/UI in one of the page we can very easily fix that up and should concerned about that page , not the whole framework.
  • Reuses the Code : A single method inside a page class can be called N no. of times
  • Easy Readability : Below diagram explains it better

Design Overview


Pitfall

As noticed from the above design, When i wanted to test few objects from HomePage , I defined my HomePageTest class but unfortunately i had to call few methods of LoginPage in order to visit HomePage.

Now why cant we have a mechanism/structure where, when we create object of HomePage, it should ensure that HomePage is loadable and takes the user to the HomePage. Something like below : 

Loadable Component

LoadableComponent is a abstract base class, whose only task is to ensure that all pages are loadable when we create object of a page class. 
When we extend LoadableComponent class, we should override these 2 following abstract methods:
  • load()
  • isLoaded()

Design Overview






































Hope it helps :)