Testing Adobe Flash with Selenium
Flash files are the animated and/or interactive content for web pages. These types of content is primarily created by Flash development environment. However some available addins let you create Flash content on Ecllipse and similar development environments.
Flash files are closed files and are rendered in a container on a webpage. For ex Flash player on Mozilla and chrome and on ActiveX control in IE. The containers are embedded in
<embed/> tags in an HTML. So when you open a web page containing a Flash file browser first loads the Player (Flash or ActiveX) and then load the file into it to display the content. The flash object model is not accessible using any HTML element.
So how do we test Flash content using Selenium?
When to automate Flash content testing?
As of now, it's better to do a solid Return of investment calculations for automating Flash content. The reason is that a tester will have to code lots of functions inside the Flash program and expose them to be called by the browser. If your ROI turns out to be beneficial go ahead and code it. So the next question that comes to mind is
How do we expose methods in Flash?
Its a fairly simple process. All you have to do is use the ExternalInterface class. We will start it with a small example. This is a small flash application which contains 3 buttons. Add, Subtract and Multiply. These three buttons when clicked sends a text to a Text control and write down what the name of the button is. So if you click Add you will find text "Add" being added to the Text control.
To view sample application, click here.
Now the task is how do we do it programmatically from Selenium. This is how a simple Flash file looks like.
The above code is in ActionScript which is pretty much close to java script. Instead of going deep into the gory details of ActionScript and how this code is structured, let's just see the important part here This is a simple Flash file which has three buttons defined by
and a text box
ExternalInterface.addCallback("Multiply" , Multiply); ExternalInterface.addCallback("Subtract" , Subtract); ExternalInterface.addCallback("Add" , Add);
Now the selenium part
As discussed above we have just exposed our methods (Multiply, add and Subtract) to the browser. How do we call these methods?
Simple, by injecting java script using selenium. I hope you guys are familiar with Selenium usage and can easily understand this part. Else try to start up with tutorials on our site, here
Click on view source of the test app page and you will find that .Swf file is hosted using the id:SimpleCalculation
Complete selenium code on C# would look like this
InternetExplorerDriver ie = new InternetExplorerDriver(options); ie.Navigate().GoToUrl(@"https://googledrive.com/host/0B3SaC4MIQsEEZkwwcDhHaVQ4OFk/calc.html"); ie.ExecuteScript("document.getElementById('SimpleCalculation').Add()"); ie.ExecuteScript("document.getElementById('SimpleCalculation').Multiply()"); ie.ExecuteScript("document.getElementById('SimpleCalculation').Subtract()"); ie.ExecuteScript("CallAdd()"); //Look at the page html source to understand this and following methods ie.ExecuteScript("CallMultiply()"); ie.ExecuteScript("CallSubtract()");