7.9. Attaching to a running Web Browser

Table of Contents

7.9.1. Overview and Motivation
7.9.2. Making the Browser attachable
7.9.3. Attaching to the Browser from the Script

7.9.1. 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

7.9.2. 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.

7.9.3. 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 the attachToBrowser function and a snooze to set a breakpoint on is sufficient as shown in this example:

[Note]Note

In the example script Internet Explorer is being used as Web browser, for Firefox and Chrome you need to specify the communication port and 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.

JavaScript
function main() {
  // Internet Explorer
  attachToBrowser( "*Automated Cross-Platform GUI Testing" );
  snooze(1);
}
Perl
sub main {
  # Internet Explorer
  attachToBrowser( "*Automated Cross-Platform GUI Testing" );
  snooze(1);
}
Python
def main():
  # Internet Explorer
  attachToBrowser( "*Automated Cross-Platform GUI Testing" )
  snooze(1)
Ruby
def main()
  # Internet Explorer
  attachToBrowser( "*Automated Cross-Platform GUI Testing" )
  snooze(1)
end
Tcl
proc main {} {
  # Internet Explorer
  invoke attachToBrowser "*Automated Cross-Platform GUI Testing"
  snooze 1
}

Once you have set a breakpoint on the line with the snooze, you can execute the 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 (Section 8.2.1), create verifications and record interactions in the web site.

Once the interaction with the browser is finished you can call the closeWindow 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.

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 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 our user guide on testing multiple applications 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.

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