Showing posts with label Flex Webdriver. Show all posts
Showing posts with label Flex Webdriver. Show all posts

Wednesday 27 November 2013

Flex Testing using Selenium Webdriver

To Test Flex Applications Create Package {e.g. testscript } .Copy below 2 files in it.Right Click Youtube.java and Select Run As Java Application.

//FlexWebDriver.java
package testscript;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class Flextesting {
    private final WebDriver webDriver;
   private final String flshObjectId;

    public FlexWebDriver(final WebDriver webDriver, final String flshObjectId) {
        this.webDriver = webDriver;
        this.flshObjectId = flshObjectId;
    }

    public String click(final String objectId, final String optionalButtonLabel) {
        return call("doFlexClick", objectId, optionalButtonLabel);
   }

    public String click(final String objectId) {
         return click(objectId, "");
    }

//... Omitted for clarity ...

    public String call(final String functionName, final String... args) {
      final Object result =
            ((JavascriptExecutor)webDriver).executeScript(
                 makeJsFunction(functionName, args),
                 new Object[0]);

         return result != null ? result.toString() : null;
    }

    private String makeJsFunction(final String functionName, final String... args) {
         final StringBuffer functionArgs = new StringBuffer();

        if (args.length > 0) {
            for (int j = 0; j < args.length;j++) {
                if (j > 0) {
                    functionArgs.append(",");
            }
                functionArgs.append(String.format("'%1$s'", args[j]));
         }
        }
        return String.format(
            "return document.%1$s.%2$s(%3$s);",
            flshObjectId,
            functionName,
            functionArgs);
    }
}

//Youtube .java

package testscript;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class Youtube {

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

System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+ "\\IEdriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
 Flextesting flashApp = new Flextesting(driver, "movie_player");

driver.get("http://www.youtube.com/watch?v=qdf5NofgGfU&feature=fvst");
System.out.println(" Page Opened is: "+driver.getCurrentUrl()+driver.getTitle() );
Thread.sleep(2000L);
    // let the video load
    while (Integer.parseInt(flashApp.call("getPlayerState")) == 3){
    Thread.sleep(1000);
    }
   
    // Play the video for 10 seconds
    Thread.sleep(5000);
    flashApp.call("pauseVideo");
    System.out.println(" Executing pauseVideo  function ");    
    Thread.sleep(5000);
    flashApp.call("playVideo");
    System.out.println(" Executing playVideo  function ");
    Thread.sleep(5000);
    flashApp.call("seekTo","140","true");
    System.out.println(" Executing seekTo 140  function ");
    Thread.sleep(5000);
    flashApp.call("mute");
    System.out.println(" Executing mute audio  function ");
    Thread.sleep(5000);
    flashApp.call("setVolume","50");
    System.out.println(" Executing setVolume to 50  function ");
    Thread.sleep(5000);
    System.out.println(" Closing Browser");
    driver.close();
}
             
}



For More Details Go To:
//http://code.google.com/apis/youtube/flash_api_reference.html#playVideo