Protractor: A guide to testing the function of window.print()

I'm currently experimenting with testing the printing functionality of a button, as shown in the code snippet below:

it('print document', function(){
    element(by.id('print-button')).click();
    expect(window.print());
});

I am interested in testing the browser's print dialog box. Can someone provide guidance on how to achieve this? Thank you!

Answer №1

The Selenium framework does not have control over the browser's print dialog, as it is outside of its scope. Trying to handle this using Protractor or Selenium alone may not provide a reliable solution.

Instead of testing the browser's print dialog directly, you can verify if the window.print function is triggered upon clicking the print button by overriding window.print method:

browser.setScriptTimeout(10);

var printButton = element(by.id('print-button'));

var result = browser.executeAsyncScript(function (elm, callback) {
    function listener() {
        callback(true);
    }

    window.print = listener;
    elm.click();
}, printButton.getWebElement());

expect(result).toBe(true);

For more information, refer to the following resources:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to add emoticons to a div element

I am currently developing a small chat application and I would like to incorporate emojis into it. My goal is to allow users to click on an emoji, which will then appear in the text area where they type their message. When a user clicks on "select," I want ...

Tips on incorporating a class into a div generated within a contenteditable container

Take a look at this sample code: <div> Text.. <div id="editable-editor" contenteditable="true">Some Text Here...</div> </div> If you hit enter inside the #editable-editor after Some Text, it will generate a <div>Here...& ...

"Upon refreshing the page, the onclick event ceases to

What I'm hoping to achieve: I have a div that is used to display user statistics. When a user is clicked on, the details of that user are loaded into the div using an ajax request. Every 10 seconds, the div refreshes and displays the stats of all use ...

Selecting a chained dropdown option using jQuery in CodeIgniter

I'm struggling with jquery and ajax, particularly in implementing a select box functionality. I am using CI and below is my code. I want the data in another select box "category" to change dynamically based on the selection in the "brand" select box. ...

Executing the next function in JavaScript only after the completion of the previous function call

Edits: http://jsfiddle.net/vol7ron/wQZdM/ I have included a fiddle to visually demonstrate the issue I am facing and what I am trying to achieve. The objective is to populate the sub-selects with the second option value. Explaining the Dilemma: It' ...

Troubleshooting pathing issues with Three.js and the useGLTF hook

Currently, I am working on a basic project using next.js and trying to explore three.js. However, I have encountered a simple yet challenging issue that I can't seem to solve. In the screenshot attached, you can see my project structure. Within Sculp ...

Customizing Magnific Popup: Changing showCloseBtn and closeOnBgClick settings during display

Is there a way to customize the behavior of an open instance of a magnific popup? I want to have different settings for when the popup is closable and when it should be prevented from closing. It appears that these options are only available during initial ...

Customize Display Settings for Django Application Using Python Selenium

In my Django app, one of the View functions utilizes a script that creates a Selenium Webdriver. The server is currently running on CentOS with both a headless and VNC server operational. The problem I am encountering is an error message saying 'Erro ...

Is it possible to link fields with varying titles in NestJS?

Currently, I am developing a NestJS application that interacts with SAP (among other external applications). Unfortunately, SAP has very specific field name requirements. In some instances, I need to send over 70 fields with names that adhere to SAP's ...

What is the best way to include my PHP session variable within my JavaScript code?

i have a dynamic table that the enables to perform CRUD operations on a database. after logging in the user is directed to index.php, here a table is displayed with values stored in the database table "ajaxtable". i have joined "ajaxtable" table and "membe ...

Implementing dynamic authorization headers in axios with vue.js

I am currently working on a Vue.js application that heavily relies on API calls, utilizing axios to make these requests. I have implemented a similar pattern found at this link. Essentially, I have created a service that will be shared across all componen ...

What could be causing the error when trying to use a PUT request with fetch in Vue?

Whenever I attempt to send a PUT request, an error pops up on the console. export function putUserData() { fetch(`${url}/user/${getCookie("ID")}`, { method: "PUT", headers: { "Authorization": `B ...

A React component will consistently display the most recent value of the props passed to

Child Component export class MultiSelectDrawer extends React.Component { componentDidMount(){ window.onpopstate = ()=>{ console.log(this.props.id); } } render() { const {input, id, label, showActionDrawer, toggleActionDrawer, i ...

Activate when every single pixel within an element is see-through

I've designed a web page that includes two canvas elements stacked on top of each other. The purpose of this setup is to allow me to "erase" the top canvas and reveal an image loaded into the bottom canvas. So far, this functionality has been working ...

Update an API call to switch from promises to observables, implementing axios

Recently, I have been experimenting with using rxjs for API requests in a React application and this is the approach that I have come up with. What are your thoughts on this method? Are there any best practices that you would recommend following? If you ...

determine the height of a div using jquery calculations

If a div is positioned relative and contains child divs that are positioned absolute, the parent div will have no set height. However, if the contents of the div change dynamically, there should be a way to calculate and adjust the height of the parent acc ...

Changing the position of an image can vary across different devices when using HTML5 Canvas

I am facing an issue with positioning a bomb image on a background city image in my project. The canvas width and height are set based on specific variables, which is causing the bomb image position to change on larger mobile screens or when zooming in. I ...

Obtaining Camera Orientation in THREE.js: A Comprehensive Guide

I am currently working on a 3D game project using THREE.JS and the Web Audio API. I encountered an issue where I want to link the web audio Listener orientation with the camera, which is constantly changing its position and direction. My main concern is w ...

Implementing a Node-RED flow to interact with MongoDB using a JavaScript object

As part of a school exercise and my personal project, I am working on setting up a NodeRed server that retrieves weather data, stores it in MongoDB, and retrieves it for future searches on the same location. My NodeRed server is attempting to perform a re ...

I encountered a connection reset error when running tests with two different sets of test data using the @test annotation and closing the driver with @afterTest

Encountering a connection reset error and closing the 2nd browser instance when passing 2 different test data in the @test annotation and executing driver.close() in the @afterTest method. public class HomePage extends base { //WebDriver driver; @BeforeTes ...