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
//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
Hello Ruchi,
ReplyDeleteI am very new to Selenium WebDriver-Flex.I found your page on google and could you please give me the prerequisites to start coding for a flex application using selenium webdriver.What I meant is,dependencies to add to pom file and also what other dependencies I need if I want to use it with Maven and also do I need any files to be copied to my flex project..etc.
Any help on this is greatly appreciated.
Hi Ruchi u r looking cute in the photo.
ReplyDeletehahahahahaha jutha kahin ka
ReplyDeleteHi Ruchi,
ReplyDeleteI have issue with flex pucchi. I have downloaded the flex-piolot but it won't work.
Can u share information on how to handle silver light application using webdriver ?
ReplyDeleteThanks in advance ...
I really enjoy the blog.Much thanks again. Really Great selenium online course
ReplyDelete