Preparing Silverlight Applications for Testing

Applies to TestLeft 4.74, last modified on February 25, 2021

To test a Silverlight application, configure the application and your testing environment.

Preparing In-Browser Silverlight Applications

Preparing and Embedding the Application Package

Compile and pack your tested Silverlight application to a package file (.xap) from which a wrapper web page will load the application.

The following example demonstrates embedding of supported and unsupported Silverlight applications into a wrapper web page via an object HTML element:

HTML

<!-- Embedding a Silverlight application from a .xap file (supported) -->
<div id="silverlightControlHost">
    <object width="640" height="480" data="data:application/x-silverlight-2," type="application/x-silverlight-2" >
        <param name="source" value="MySilverlightApp.xap"/>
        <param name="minRuntimeVersion" value="4.0.50826.0" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
            <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
        </a>
    </object>
</div>

<!-- Embedding a Silverlight application from a .xaml file (unsupported) -->
<div id="silverlightControlHost">
    <object width="640" height="480" data="data:application/x-silverlight-2," type="application/x-silverlight-2" >
        <param name="source" value="MySilverlightApp.xaml"/>
        <param name="minRuntimeVersion" value="4.0.50826.0" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
            <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
        </a>
    </object>
</div>

Preparing Applications With the tcAgPatcher Utility

By default, TestLeft can access internal objects of Silverlight applications, their properties and methods without any preparations if the following requirements are met:

  • The application is running in Internet Explorer.

    TestLeft cannot access internal of applications running in Internet Explorer 11. See below.
  • The application’s wrapper web page loads the application package file (.xap) directly from a web server.

  • The application’s wrapper web page is loaded via the HTTP protocol.

If one of these requirements is not met, or if TestLeft cannot access the applications internals automatically for other reasons, you have to use the tcAgPatcher utility to prepare the application This can happen, if —

  • A Silverlight application is signed.

  • A Silverlight is running on a server that requires Negotiate, MTLM, or some other authentication.

  • A Silverlight application is running in Internet Explorer 11.

To prepare a Silverlight application for testing and make its internal objects available to TestLeft manually, use the tcAgPatcher utility.

The utility is shipped with TestLeft. It is located in the <TestLeft>\Open Apps\Silverlight\tcAgPatcher.exe folder.

tcAgPatcher does not change the application’s source code. It adds TestLeft libraries directly to the application’s .xap file and registers them in the application’s manifest (AppManifest.xaml) inside the .xap file. These are the same libraries that TestLeft injects into non-prepared Silverlight applications during the test run.

Do not copy tcAgPatcher to another folder. tcAgPatcher works only from its default installation folder.

tcAgPatcher has the following command-line syntax:

tcAgPatcher.exe [/silent] [/noBackup] xapfile

The command-line arguments are as follows:

  • /silent - (Optional) Do not show any error or warning messages.

  • /noBackup - (Optional) Do not create a backup copy of the application’s .xap file. If this argument is not specified, tcAgPatcher creates the application’s backup copy with the .bak extension in the same folder.

  • xapfile - The fully-qualified name of the Silverlight application’s .xap file to process.

You need to replace the .xap file on the application server with the processed .xap file. Do not process a local copy of the .xap file stored in your web browser’s cache.

Note for signed Silverlight applications: To test a signed Silverlight application processed with tcAgPatcher, launch TestLeft with the /agpatcheroff command-line argument:

"<TestLeft>\Bin\TestExecute.exe" /agpatcheroff

Without this argument, your application’s digital signature will be invalid during the testing process.

Preparing Your Web Browsers

  • To make sure the tested application is retrieved from the original location rather than from the cache, clear the browser’s cache before testing your application.

  • We recommend that you launch your Silverlight application in your web browser by using the following method:

    .NET: Driver.Applications.RunBrowser

    Java: driver.getApplications().runBrowser()

    If you start your application in another way, and the URL of the page where the tested application will run is specified in the browser’s command line or in the browser settings, TestLeft will not be able to access the application’s internals automatically.

    This can happen if —

    • You launch the browser from the command line and specify the test page’s URL as a command-line parameter.

    • The tested web page is specified in the browser settings and is opened automatically when the browser launches.

    To avoid the problem, use any of the following approaches —

    • Launch the browser using the RunBrowser method:

      C#

      using SmartBear.TestLeft;


      public void Test()
      {

        string URL = "https://smartbear.com/samples/testcomplete/silverlight/orders.html";
        IWebBrowser browser = Driver.Applications.RunBrowser(BrowserType.IExplorer, PlatformType.Any, URL);
        …

      }

      Visual Basic .NET

      Imports SmartBear.TestLeft


      Public Sub Test()

        Dim URL As String = "https://smartbear.com/samples/testcomplete/silverlight/orders.html"
        Dim browser As IWebBrowser = Driver.Applications.RunBrowser(BrowserType.IExplorer, PlatformType.Any, URL)
        …

      End Sub

      Java

      import com.smartbear.testleft.*;
      import com.smartbear.testleft.testobjects.*;
      import com.smartbear.testleft.testobjects.web.*;


      @Test
      public void Test() throws Exception{

        String url = "https://smartbear.com/samples/testcomplete/silverlight/orders.html";
        WebBrowser browser = driver.getApplications().runBrowser(BrowserType.IExplorer, PlatformType.any, url);
        …

      }
    • Change the launch settings so that the browser first opens the about:blank page and then navigates to the desired web page from the test. Your test may look like the following —

      C#

      using SmartBear.TestLeft;


      public void Test()
      {

        // Calling your custom procedure for launching the browser
        …

        // Getting the browser process and opening the test page
        IWebBrowser browser = Driver.Find<IWebBrowser>(new WebBrowserPattern()
        {
          ObjectIdentifier = "iexplore"
        });

        browser.ToUrl("https://smartbear.com/samples/testcomplete/silverlight/orders.html");
        …

      }

      Visual Basic .NET

      Imports SmartBear.TestLeft


      Public Sub Test()

        // Calling your custom procedure for launching the browser
        …

        // Getting the browser process and opening the test page
        Dim browser As IWebBrowser = Driver.Find(Of IWebBrowser)(New WebBrowserPattern() With {
                  .ObjectIdentifier = "iexplore"
        })

        browser.ToUrl("https://smartbear.com/samples/testcomplete/silverlight/orders.html")

        …

      End Sub

      Java

      import com.smartbear.testleft.*;
      import com.smartbear.testleft.testobjects.*;
      import com.smartbear.testleft.testobjects.web.*;


      @Test
      public void Test() throws Exception{

        // Calling your custom procedure for launching the browser
        …

        // Getting the browser process and opening the test page
        WebBrowser browser = (WebBrowser) driver.find(WebBrowser.class, new WebBrowserPattern() {{
          ObjectIdentifier = "iexplore";
        }});

        browser.toUrl("https://smartbear.com/samples/testcomplete/silverlight/orders.html");

        …

      }
    • Prepare your Silverlight application for testing using the tcAgPatcher utility as it is described above.

Preparing Cross-Domain Silverlight Applications

  1. Create a crossdomain.xml configuration file with the following contents and place the file in the root directory of the web site where the tested Silverlight application’s package file (.xap) resides:

    XML

    <?xml version="1.0"?>
    <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
    <cross-domain-policy>
       <allow-http-request-headers-from domain="*" headers="*"/>
    </cross-domain-policy>

    For more information on this file, see the MSDN Library.

  2. In the application manifest file (AppManifest.xml) of your Silverlight project, set the ExternalCallersFromCrossDomain attribute of the Deployment element to “ScriptableOnly”.

    XML

    <Deployment xmlns="http://schemas.microsoft.com/client/2007"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      ExternalCallersFromCrossDomain="ScriptableOnly"
        ...
    >
        ...
    </Deployment>

    By default, the application manifest file is generated when you are building your Silverlight project. If the file is not generated, make sure that the “Generate Silverlight manifest file” option is enabled in the project’s settings:

    • Right-click the project node in Visual Studio’s Solution Explorer and select Properties from the context menu.

    • In the Project Properties window, switch to the Silverlight tabbed page.

    • Select the Generate Silverlight manifest file check box and specify a manifest file template (by default, it is AppManifest.xml).

    For more information on setting the ExternalCallersFromCrossDomain attribute in the manifest file, see the MSDN Library.

  3. In the source code of the application’s wrapper web page, set the enableHtmlAccess parameter of the Silverlight plugin hosting the tested Silverlight application to true.

    The way Silverlight application developers can change the enableHtmlAccess parameter depends on the way the application is embedded into the wrapper web page.

    If the application is embedded into the web page by using an HTML object element:
    • Open the application’s wrapper web page in an editor.

    • Locate the object element that is used to embed the Silverlight application.

    • Locate or add the child param element named enableHtmlAccess and set the parameter’s value to true as shown in the following example:

      HTML

      <div id="silverlightControlHost">
          <object width="640" height="480" data="data:application/x-silverlight-2," type="application/x-silverlight-2" >
              <param name="source" value="MySilverlightApp.xap"/>
              <param name="minRuntimeVersion" value="4.0.50826.0" />
              <param name="enableHtmlAccess" value="true" />
              <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
                  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
              </a>
          </object>
      </div>

    • Save the changes.

    If the application is added to the web page by using JavaScript functions:
    • Open the web page or JavaScript file containing the application’s functions in an editor.

    • Locate the Silverlight.createObject or Silverlight.createObjectEx function call that is used to embed the Silverlight application.

    • Locate or add the enableHtmlAccess property in the function’s parameter list and set its value to true as shown in the following example:

      JavaScript

      Silverlight.createObject(
          "MyApplication.xap",
          document.getElementById("silverlightControlHost"),
          "silverlightControl",
          {
              width: "640",
              height: "480",
              version: "4.0.50826.0",
              enableHtmlAccess: true
          },
          { },
          null,
          null
      );

      // -- or --

      Silverlight.createObjectEx({
          source: "MyApplication.xap",
          parentElement: document.getElementById("silverlightControlHost"),
          id: "silverlightControl",
          properties: {
              width: "640",
              height: "480",
              version: "4.0.50826.0",
              enableHtmlAccess: true
          },
          events: {}
      });

    • Save the changes.

    For more information on setting the enableHtmlAccess parameter of the Silverlight plugin, see the MSDN Library.

  4. Include the <TestLeft>\Open Apps\Silverlight\tcAgAgent4.dll assembly in your Silverlight project in Visual Studio.

  5. Append the following call to the Startup event handler code:

    C#

    AutomatedQA.TestComplete.AgAgent.Core.AgAgentLoader.LoadAgent();
  6. Rebuild the application. After that, you need to process the generated application package file (.xap) with the tcAgPatcher.exe command-line utility in the manner described above. This will enable extended support for Silverlight controls in your application.

Preparing Out-Of-Browser Silverlight Applications

To enable TestLeft to recognize objects in out-of-browser Silverlight applications, configure TestLeft UI Automation engine so that it provides access to Silverlight objects. To do this, add the MicrosoftSilverlight item to the list of supported UI Automation classes:

C#

using SmartBear.TestLeft;


public void Test()
{

  Driver.Options.UIAutomation.AddWindow("MicrosoftSilverlight");
  …

}

Visual Basic .NET

Imports SmartBear.TestLeft

Public Sub SilverlightTest()

  Driver.Options.UIAutomation.AddWindow("MicrosoftSilverlight")
  …

End Sub

Java

import com.smartbear.testleft.*;

@Test
public void SilverlightTest() throws Exception{

  driver.getOptions().getUIAutomation().addWindow("MicrosoftSilverlight");
  …

}

TestLeft will recognize visual objects for which UI Automation provides access in your Silverlight application. Get those objects in tests by using the UIAPattern class.

See Also

Preparing Web Applications for Testing

Highlight search results