Skip to content

JS handling

aqualityAutomation edited this page Apr 9, 2023 · 2 revisions

Selenium 4 introduces several ways to work with JavaScript on the client side (in the browser). Among them:

Client-side event listeners; Execution of JS code in the browser. In Aquality Selenium, this functionality is mainly implemented in the [JavaScriptHandling] class, as well as in several auxiliary classes. The work of these classes is demonstrated in the test classes [JavaScriptHandlingTests]

Should_BePossibleTo_SubscribeToDomMutationEvent_AndUnsubscribeFromIt

Test method that tests the behavior of subscribing and unsubscribing from a DOM mutation event using a JavaScript engine. The test starts by creating a new instance of a WelcomeForm object and a list to store the DOM mutation data. Then, it sets up an event handler that adds any received DOM mutation data to the list.

Next, the test ensures that it is possible to start and enable DOM mutation monitoring without throwing an exception. It then opens the WelcomeForm, sets an attribute on one of its labels, and waits for a DOM mutation event to be fired and added to the list. The test then verifies that the received mutation data matches the expected attribute name and value.

The test then unsubscribes from the DOM mutation event by removing the event handler and sets another attribute on the same label. It waits for a specified timeout and asserts that no new mutation events were fired and added to the list. Finally, it disables DOM mutation monitoring and stops event monitoring, ensuring that these operations do not throw exceptions.

Should_BePossibleTo_PinScript_AndUnpinIt

The test method, named Should_BePossibleTo_PinScript_AndUnpinIt, tests the functionality of the PinScript() and UnpinScript() methods in a JavaScriptEngine class. The JavaScriptEngine class is used to execute JavaScript code and is being tested here to ensure that it can correctly pin and unpin a script.

The test begins by calling GetElementXPath.GetScript() to retrieve a JavaScript script. The script is then pinned using the PinScript() method of the JavaScriptEngine class. The ExecuteScript() method is then called on the pinned script to retrieve an XPath value for a UI element on a WelcomeForm object. The retrieved XPath value is then asserted to be not empty and equal to an expected value.

The test then verifies that the script can be unpinned using the UnpinScript() method and that attempting to execute the unpinned script using the ExecuteScript() method results in a JavaScriptException. Finally, the test checks that the Reset() method of the JavaScriptEngine class can be called without throwing an exception.

Overall, this test checks that the JavaScriptEngine class can correctly pin and unpin scripts, execute pinned scripts, and reset the monitoring state without errors.

Should_BePossibleTo_SubscribeToJavaScriptConsoleApiCalledEvent_AndUnsubscribeFromIt

This test checks the functionality of subscribing and unsubscribing to an event that is raised when a JavaScript console API is called.

The test starts by defining a console API script and creating an empty list to store any messages that are captured by the event handler. The JavaScriptConsoleApiCalled event is then subscribed to using the eventHandler function.

The test then asserts that starting the event monitoring process with StartEventMonitoring() does not throw an exception.

Next, the console API script is executed using a browser object from the AqualityServices library. A wait condition is then set to ensure that the apiCalledMessages list has increased in size, indicating that the event handler has been called and the message has been captured.

The test then asserts that the count of messages in apiCalledMessages has increased.

The event handler is then unsubscribed from the JavaScriptConsoleApiCalled event and the console API script is executed again. Another wait condition is set to ensure that the number of messages in apiCalledMessages does not increase any further. The test then asserts that the number of messages in apiCalledMessages is the same as it was before the second execution of the console API script, indicating that unsubscribing from the event was successful.

Overall, this test checks that subscribing and unsubscribing to the JavaScriptConsoleApiCalled event works as expected, and that messages from the event are properly captured and stored.

Should_BePossibleTo_SubscribeToJavaScriptConsoleApiCalledEvent_AndUnsubscribeFromIt

This test checks the functionality related to adding, subscribing, and removing script callback bindings.

The test begins by defining a script string and a script name, then sets up an event handler to capture events related to script callbacks. The test asserts that it is possible to add an initialization script and start event monitoring without throwing any exceptions.

Next, the test attempts to refresh the page with an alert and handle the alert using the Accept action. This is done to trigger the initialization script. The test then asserts that it is possible to add a script callback binding without throwing any exceptions.

To confirm that the script callback binding is working, the test asserts that an alert triggered by the initialization script is not handled when the binding is in place. The test also waits for the script callback event to fire and confirms that the event handler captured the expected script name.

The test then refreshes the page and confirms that another event was noticed and that the script callback binding can be read from the JavaScript engine.

The test then removes the script callback binding, confirms that it was successfully removed, and then adds it back again to confirm that it can be added again. The test then clears all script callback bindings and asserts that the list is empty.

Finally, the test unsubscribes from the event handler, refreshes the page, and confirms that no new events were noticed.