Wednesday, March 19, 2014

Selenium : Handling Images , pictures and flash elements

Handling Images , pictures and flash elements



This problem can be tackled by using Sikuli .

1. Download and install Sikuli IDE or Sikuli X  (Sikuli-X)  
   You can go to this website : http://www.sikuli.org/downloadrc3.html to find setup file compatible with your OS.
  • Download the following zip file: Sikuli X r930. This contains important bug fixes
  • Unzip the above file
  • Copy all the contents of the unzipped files and replace it in the installation folder.
  • )
2. Once installed you should have Sikuli IDE available in your  Start Options for Windows .

3. Configuring Eclipse
  1. You must use a 32bit JRE version (I use jre-7u4-windows-i586.exe)
  2. Download Sikuli IDE for Windows (I use "Sikuli X r930", portable version)
  3. copy all the installation files into a new folder inside your project (ex:"sikuli-ide"), ref screenshot http://i.stack.imgur.com/LSiQV.png)
  4. Add sikuli-script.jar to the Referenced Libraries (Project > Properties > Java Build Path > Libraries, ref screenshot http://i.stack.imgur.com/N2SJ8.png)
  5. Set PATH and SIKULI_HOME environment vars (Run > Run Configurations > Environment, ref screenshot http://i.stack.imgur.com/HboXk.png)
  6. You're ready to go.

Running sample Code :

The below code will open YouTube page with a video and does the following operations:

1. Click on Pause button on the player.
2.Click on Large Player Button.
3.Click on Small Player Button.

Note :
1. Take precaution there should be no advertisement.
2. Make separate files for a)pause.png b) size_increase.png  c)size_decerease.png

 (You can take a screenshot of the player and crop these objects and place it your drive .)


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.sikuli.script.SikuliException;


public class Sikuali_test {

    public static void main(String[] args) throws SikuliException, InterruptedException {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.youtube.com/watch?v=b-NeudACWzc");
       
        Screen screen = new Screen();
        Pattern image = new Pattern("D:\\pause.png");
        screen.wait(image, 10);
        screen.click(image);

        Pattern image2 = new Pattern("D:\\size_increase.png");
        screen.wait(image2, 10);
        screen.click(image2);
               
        Pattern image3 = new Pattern("D:\\size_decerease.png");
        screen.wait(image3, 10);
        screen.click(image3);

    }

}












No comments:

Post a Comment