Attaching to a running Web Browser

Overview and Motivation

Squish for Web's default behavior is to automatically start the Web browser you want to test with, and to terminate the Web browser once the test case is finished. But it is also possible to test an already running Web browser by attaching to it. One important difference when attaching is that at the end of the test case Squish does not terminate the Web browser it attached to.

A common usage scenario for this functionality is to automate the Web browser when it is being started by an application that is also automated. Consider a desktop application that allows creation of websites from building blocks and provides a view of the result by starting a browser. This workflow can be automated by Squish through automating the desktop application, and once it started, Squish for Web can attach to the browser and verify that the displayed web content is correct as well as whether links etc. work.

Another use case that is often found are devices that need a user interface to allow for configurations. Some manufacturers choose to use a Web browser running in a special kiosk-mode that shows a web page providing the necessary configuration elements. In such cases the Web browser is often started as part of the system itself and shouldn't be shut down, or it automatically restarts when it is terminated for some reason. Here Squish for Web can automate the browser running on the device to ensure the user interface provided works correctly and can be handled properly by the Web browser.

At the moment this feature is supported only for the following browsers:

  • Internet Explorer on Windows
  • Mozilla Firefox on Windows, Linux and macOS
  • Google Chrome on Windows, Linux and macOS
  • Safari on macOS

Making the Browser attachable

Depending on the Web browser you want to automate there are some installation steps to take before being able to attach to it. For Firefox and Google Chrome, the Squish extension needs to be installed and a communication port needs to be configured. Internet Explorer requires changing a registry setting in Windows. Safari does not need any special setup. Detailed steps how to configure Internet Explorer, Firefox and Chrome can be found in the How to Install Squish section.

Attaching to the Browser from the Script

In contrast to the attachToApplication, there is currently no support for recording a complete testcase against an attachable Web browser. In order to inspect the web site or record interactions its necessary to prepare a short script snippet that attaches to the browser and contains a breakpoint at which the recording can be started. A small main function with attachToBrowser(portOrWindowTitle) and test.breakpoint() is sufficient as shown in this example:

Note: In the example script, Internet Explorer is being used as Web browser. For Firefox and Chrome, you need to specify the communication port, whereas for Safari, the parameter needs to be removed. See the example code in the section about the attachToBrowser function for example code for the other browsers.

function main() {
    // Internet Explorer
    attachToBrowser( "*Automated Cross-Platform GUI Testing" );
    test.breakpoint();
}
sub main {
    # Internet Explorer
    attachToBrowser( "*Automated Cross-Platform GUI Testing" );
    test::breakpoint();
}
def main():
    # Internet Explorer
    attachToBrowser( "*Automated Cross-Platform GUI Testing" )
    test.breakpoint()
def main()
    # Internet Explorer
    attachToBrowser( "*Automated Cross-Platform GUI Testing" )
    Test.breakpoint()
end
proc main {} {
    # Internet Explorer
    invoke attachToBrowser "*Automated Cross-Platform GUI Testing"
    test breakpoint
}

You can execute this test case from the IDE and it will stop at that breakpoint. You can look at the objects of the web site in the Application Objects view, create verifications and record interactions in the web site.

Once the interaction with the browser is finished you can call the closeWindow(":[Window]") function to disconnect from the browser, however this is also done automatically once the test script finishes. You can later attach to the browser again by calling attachToBrowser(portOrWindowTitle).

If you intend to attach to the Web browser from a Squish test suite that has been created for another edition, like Qt or Windows, there is an additional setup step necessary. The attachToBrowser(portOrWindowTitle) function is currently only available when Squish's helper tool webhook is running and connected to. In Squish test suites for automating Web applications this tool is automatically started, but for other types of test suites it has to be started as part of the script. The tool is being used just like any other AUT, so it has an application context which needs to be active before attaching to the browser and any time you want to interact with objects inside the browser. See How to Test Multiple AUTs from a Single Test Script, Using ApplicationContext to better understand how to use this functionality.

The following script code examples show how a test would look like that starts a normal application as AUT to interact with that and then also launches the webhook as an AUT to attach to a browser and interact with that. Since the webhook is an AUT bundled with Squish it does not need to be registered as an AUT, instead its available through a special name (__squish__webhook) all the time.

function main() {
    var autContext = startApplication("addressbook");
    // [ ... interactions with the AUT ... ]
    testSettings.setWrappersForApplication("__squish__webhook", ["Web"]);
    var webContext = startApplication("__squish__webhook");
    attachToBrowser("*froglogic*");
    // [ ... interactions with the Website ... ]
    setApplicationContext(autContext);
    // [ ... interactions with the addressbook AUT again ... ]
    setApplicationContext(webContext);
    // [ ... interactions with the Website again ... ]
    // detach from the browser
    closeWindow(":[Window]");
}
sub main {
    my $autContext = startApplication("addressbook");
    # [ ... interactions with the AUT ... ]
    testSettings->setWrappersForApplication("__squish__webhook", ("Web"));
    my $webContext = startApplication("__squish__webhook");
    attachToBrowser("*froglogic*");
    # [ ... interactions with the Website ... ]
    setApplicationContext($autContext);
    # [ ... interactions with the addressbook AUT again ... ]
    setApplicationContext($webContext);
    # [ ... interactions with the Website again ... ]
    # detach from the browser
    closeWindow(":[Window]");
}
def main():
    autContext = startApplication("addressbook")
    # [ ... interactions with the AUT ... ]
    testSettings.setWrappersForApplication("__squish__webhook", ["Web"])
    webContext = startApplication("__squish__webhook")
    attachToBrowser("*froglogic*")
    # [ ... interactions with the Website ... ]
    setApplicationContext(autContext)
    # [ ... interactions with the addressbook AUT again ... ]
    setApplicationContext(webContext)
    # [ ... interactions with the Website again ... ]
    # detach from the browser
    closeWindow(":[Window]")
def main()
    autContext = startApplication("addressbook")
    # [ ... interactions with the AUT ... ]
    testSettings.setWrappersForApplication("__squish__webhook", ["Web"])
    webContext = startApplication("__squish__webhook")
    attachToBrowser("*froglogic*")
    # [ ... interactions with the Website ... ]
    setApplicationContext(autContext)
    # [ ... interactions with the addressbook AUT again ... ]
    setApplicationContext(webContext)
    # [ ... interactions with the Website again ... ]
    # detach from the browser
    closeWindow(":[Window]")
end
proc main {} {
    set autContext [startApplication "addressbook"]
    # [ ... interactions with the AUT ... ]
    testSettings setWrappersForApplication __squish__webhook {Web}
    set webContext [startApplication "__squish__webhook"]
    invoke attachToBrowser "*froglogic*"
    # [ ... interactions with the Website ... ]
    setApplicationContext $autContext
    # [ ... interactions with the addressbook AUT again ... ]
    setApplicationContext $webContext
    # [ ... interactions with the Website again ... ]
    # detach from the browser
    invoke closeWindow ":\[Window\]"
}

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.