How can I automate Silverlight application with selenium webdriver? - silverlight

I'm new with Silverlight and Selenium. I searched to automate my Silverlight application with Selenium webdriver but I didn't find any useful source. Can someone guide me?
Cheers,

It's possible to declare methods and attributes as [Scriptable] or a whole class as [ScriptableType]. This way you can invoke/access them via JavaScript, which can be done via WebDriver's executeScript and executeAsyncScript methods. In the class constructor, you can make the instance visible in the DOM by calling:
HtmlPage.RegisterScriptableObject("AnyNameYouWant", this);
Note that no default WebDriver interaction (click, typeKeys) will work within your Silverlight object, so a click on a button, for instance, will have to be done programmatically like:
var peer = new ButtonAutomationPeer(button);
var ip = (IInvokeProvider)peer;
ip.Invoke();
The silverlight-selenium project (https://code.google.com/p/silverlight-selenium/) provides some fixtures for common UI components, relying solely on this JavaScript to Silverlight bridge. Unfortunately, this project is not currently active, but the examples should give you some insights.

Related

Winappdriver and selenium in the same automated test

Hi I could do with some help / more experienced eyes.
I have a WPF application which I have started automating some UI tests for using winappdriver, upon further investigation it has embeded html in it - webview, and can fire off requests to open the default browser with app related content - such as help files.
Has anyone had experience in working with this? For example:
open the WPF app,
click on help button on the WPF app which will open a browser and
then continue the test to ensure that the correct help page has been launched with the correct content in relation to the WPF page it was fired from.
Presumably this can be done in my case with chrome driver (winappdriver cannot see the content on the webpage). I have tried using selenium's window handles, but it's like the driver can't see the already open browser page. So I am at a bit of a loss and really not sure what to do.
In previous roles I was used to using Ranorex, which does both windows and web based UI automation. So I have never had any experience using multiple driver types to do the one test.

Debugging JavaScript in Chromium Embedded Framework

I have a WPF application which uses CEF to display web content. My question is, is there a way to debug the Javascript/Web parts inside a WPF application?
You may also use ShowDevTools() extension method (source)
ChromiumWebBrowser browser = new ChromiumWebBrowser();
browser.ShowDevTools(); // Opens Chrome Developer tools window
Enable remote debugging in your application:
C# (CefSharp)
CefSettings.RemoteDebuggingPort = 8088;
C++
CefSettings settings;
settings.remote_debugging_port = 8088;
then run your app and point your browser to http://localhost:8088/ to access the Chromium developer console (the same you have in Chrome with Ctrl+Shift+j)
While the accepted answer is correct, it doesn't really have enough detail.
I got this working in CefSharp using the WinForms control in a WPF application. (the WinForms control seems to have better performance). The code for remote debugging will probably be very similar for the WPF control though.
var settings = new CefSettings { RemoteDebuggingPort = 8088 };
Cef.Initialize(settings);
WindowsFormsHost.Child = new ChromiumWebBrowser(url);
Then go to http://localhost:8088/ in your browser.
To use 'ShowDevTools()' you will need first verify if the browser is initialized.
An example solution:
//Add an event to check
ChromeBrowser.IsBrowserInitializedChanged += ChromeBrowser_IsBrowserInitializedChanged;
//Declare the event method to be called
private void ChromeBrowser_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
{
if (e.IsBrowserInitialized)
{
ChromeBrowser.ShowDevTools();
}
}
To open the Chromium Dev-Tools window you can do the following:
CefBrowser.GetBrowser().GetHost().ShowDevTools();
This is similar to Eido95's answer, but it doesn't require the extension methods, which essentially just wrap these method calls.
NOTE: The control needs to be initialized before calling this method can be called. If you're wiring-up and F12-like functionality this shouldn't be a problem. If you're trying to do this when the app is starting you will need to listen for the ChromiumWebBrowser.IsBrowserInitializedChanged event
An alternative can be to launch cef with --enable-chrome-runtime.
You'll have the fully featured debugger (link files on disk and edit them from the debugger)

Access Silverlight window within Chrome via System.Windows.Automation

This is probably futile, but I'm wondering if anyone out there has experience doing this.
I'm trying to access a Silverlight application hosted within Google Chrome by using System.Windows.Automation (e.g., AutomationElement).
The problem I'm having is that Chrome hosts the Silverlight app within a child process. If I attempt to find the "Silverlight Control" AutomationElement (by using the main process' hWnd), it fails.
If I locate the Silverlight host child process, it does not have a window handle, and if I attempt to find the control using the child process' Handle it fails.
I know it's there... I can see it using Inspect
but I can only find this by clicking in the Silverlight app and navigating up in Inspect. I cannot navigate down from the tab window using AutomationElement.FindFirst or Inspect.
Its like there is a disconnect between the window and the Silverlight plugin that isn't seen in IE or Firefox, and I don't know how to get around it.
Has anybody else been able to do this?
I'm not sure if this helps or not in your instance but I've run into a couple instances where I needed to enable communication into Silverlight from outside (Office Application AddIns that need to communicate with the Silverlight Application). I've used javascript in the html page hosting the Silverlight Application as a bridge for that communication:
function sendToNav(message) {
var nav = document.getElementById("Nav");
nav.content.NavigationPage.HandleScriptedMessage(message);
}
function passMessageToHost(message) {
if (window.external == null) return;
window.external.HandleScriptedMessage(message);
}
If there any additional information I can provide please let me know. Hope this is of some use to you.

Recording Screen Interactions using UI Automation for Silverlight controls

I am trying to use UI Automation to record screen interactions in my Silverlight app. I have the Automation Peers for my control setup but am not able to figure out how to track these controls.
For my WPF app I would get hold of AutomationElement.RootElement and then navigate down to my App and setup Automation event Handlers on the controls I am interested in.
How would I do the same for Silverlight app? I want the screen recorder to start automatically whenever my app runs. The screen recorder has to sit within my App. But the System.Windows.Automation silverlight namespace doesn't have a AutomationElement class. So how would I do this in Silverlight?
Thanks
Shivaprasad
As you observed, the AutomationElement class doesn't exist in Silverlight: the UIAutomation API is available only in the full .Net framework. Silverlight is only supported as an UI Automation provider - that is, Silverlight applications can be controlled by UI Automation, but they are not able to control other applications.
For this reason, I don't think you're going to be able to create your screen recorder within the Silverlight application - unless you are prepared to resort to some nasty hacks.
Such hacks might include:
Using Silverlight COM Automation to call the new UIAutomation COM API. Users would be required to have the COM API installed on their machines in addition to Silverlight.
Create a "server" application in .Net that you can run on the target machine that does the actual UIAutomation work, then communicate with this from your Silverlight application using WCF.

Open a wpf app from a console app and pass objects to it

We would like to open a wpf app from a console app and pass objects which were initialized in the console app to the wpf app. Just wondering if this is possible.
Thanks
N
You could use the command line and pass serialized objects as arguments but this will only work 1 time, when you start the wpf app.
If you need to maintain an ongoing conversation between the two you will need to host a wcf service in the wpf app and talk to it that way.
IMO hosting a service in the WPF app is the best way to go.
You can pass objects in the constructor of your wpf window just like any other class..
Ex: Window1 window = new window1(myObject);

Resources