Failed commitments in Protractor/WebDriverJS

WebdriverIO and Protractor are built on the concept of promises:

Both WebdriverIO (and as a result, Protractor) APIs operate asynchronously. All functions return promises. WebdriverIO maintains a queue of pending promises known as the control flow to ensure organized execution.

Referencing the definition:

A promise is an object that represents a value or the future computation of a value. Initially, every promise is in a pending state and can either be successfully resolved with a value or be rejected to signal an error.

The aspect regarding promise rejection is something that I haven't fully grasped yet and have not encountered in Protractor. Typically, we utilize the pattern of using then() and supplying a function for handling a successfully resolved promise:

element(by.css("#myid")).getAttribute("value").then(function (value) {
    // manipulate the value here
});

The Query:

Is there a possibility that a promise returned by any of the Protractor/WebDriverJS functions may not be successfully resolved and could potentially be rejected? Is it necessary to consider this scenario and address it?

Answer №1

During my usage of browser.wait(), I encountered a scenario involving promise rejection. Allow me to share an example:

var EC = protractor.ExpectedConditions;

function isElementVisible() {

    var el = element(by.css('#myel'));

    // return promise
    return browser.wait(EC.visibilityOf(el), 1000)
    .then(function success() {
        return true; // return if promise resolved
    }, function fail() {
        return false; // return if promise rejected
    });
}

expect(isElementVisible()).toBe(true);
expect(isElementVisible()).toBe(false);

In this case, the success function will be executed if the element is present on the page. Conversely, if the element is not found after 1 second, the fail function will handle the rejection. One key aspect to note is that by providing a callback for rejection, one can ensure consistency in expectations. By anticipating whether the promise will resolve to true or false, it becomes feasible to construct a suite based on this behavior. Failure to include a fail callback could result in an uncaught exception due to timeout, leading to the failure of a specific test while allowing others to proceed. Although Protractor intercepts such exceptions, handling them within the codebase is pivotal to prevent leaks.

Answer №2

One of the advantages of using promises is that you will always receive a response, whether it's data or an error message. This applies even when working with multiple promises in a series, such as those used in Webdriver - you will either get an array of responses or a failure response if one of them fails. How you choose to handle these failed responses is ultimately up to you; personally, I prefer to log them into the console for review. The key decision lies in whether you want to abort the rest of your tests or continue despite the errors.

If you're looking for more information on this topic, check out this informative article:

It seems like what you're currently doing is sufficient, but keep in mind that you're not actively catching any of the errors. If this is something you're concerned about, consider abstracting the call into a function that automatically handles and logs errors for you.

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

Opening the Gmail app from a link using JavaScript

What is the best way to open the Gmail app from a hyperlink? This link opens WhatsApp <a href="https://wa.me/">whatsapp</a> <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1f190f ...

Checkbox enabled Bootstrap image

I am encountering a minor issue where I am attempting to incorporate a simple image that can be toggled on and off by clicking on it. After coming across some bootstrap code online, I decided to test it in my project. Unfortunately, the code does not seem ...

Are the functionalities of twilio-common.js on github equivalent to those of twilio-client.js on their CDN?

Currently, I am integrating the Twilio SDK client from the twilio CDN using this link: //media.twiliocdn.com/sdk/js/client/v1.4/twilio.min.js However, I am interested in importing the package via npm due to some restrictions. The only option I see availa ...

Trouble With OnClick and On/Off Class Script Functionality in Chrome and Internet Explorer

I am working on a page with two iframes and a basic navigation bar. The navigation bar links are set up to target one of the iframes and replace its content when clicked. However, I am having trouble highlighting the currently selected link in the iframe - ...

Listcell XUL with button options

How can I make buttons inside a listcell function in XUL? I am having trouble getting it to work. Here is the XUL code: <listitem id = "1"> <listcell label = "OK Computer"/> <listcell label = "Radiohead"/> <listcell label ...

What is the best approach for filtering a nested array in this scenario?

Here is the response I am getting: let m = [ { name: 'Summary', subListExpanded: false, subList: [ ] }, { name: 'Upload', subListExpanded: false, subList: [ ...

What's the best way to maintain the return type of a function as Promise<MyObject[]> when using forEach method?

I am currently working with a function called search, which at the moment is set up to return a type of Promise<MyObject[]>: export function search(args: SearchInput) { return SomeInterface.performSearch(args) .then(xmlRequest =&g ...

Utilizing dynamic class and color binding features in VueJs?

I need help with implementing a Custom Sort method on my divs to arrange them in ascending or descending order. My query is how can I pre-set the icon color to grey by default, and only change it to black when clicked, while keeping the others greyed out a ...

JSPM encountered an issue with installation from GitHub (404 error), but it is able to successfully install packages from npm

I am encountering a frustrating issue with my Package.json file for a GitHub repository in my organization. Attempting to pull it in via jspm is causing errors. { "name": "tf-modernizr", "version": "1.0.0", "description": "", "main": "modernizr.js ...

Retrieve the text content from a JavaScript alert using WebDriver

Currently, I am utilizing Selenium WebDriver in C# to enhance my knowledge and create automated tests. I recently encountered a particular scenario that has left me puzzled: Imagine you have a website similar to this one: . When attempting to register wit ...

What is the best way to incorporate an ID from a scala template into an AJAX request?

In my application built on the Play Framework 2.3.8, users can input questions and answers. The view class receives a List[Question] which is iterated through using a for each loop to display them: @for(question <- questionList){ <!-- Questions --& ...

Tips for implementing code to function across several images in HTML and CSS

Hey there, I'm currently working on a website project for my friend and I sometimes refer to www.w3schools.com for help with coding. I'm having trouble implementing a gallery feature where users can click on images to view them fullscreen. I foun ...

The function .play() cannot be executed on document.getElementById(...) - it is not a

There is an error in the console indicating that document.getElementById(...).play is not a valid function. import React from 'react'; const musicComponent=(props)=>{ const style={background:props.color} return( <div classN ...

Delaying Variable Assignment in Node.js until Callback Function Completes

I am currently working with Node.js, Express, MongoDB, and Mongoose in my project. One specific task involves fetching the largest id number of a document from the MongoDB database and passing it back to the program. To better organize my code, I moved thi ...

Tips for adding style to a group of SVG elements:

I currently have an array of .svg icons, each with unique properties that I need to customize: <svg width="24" height="24" viewBox="0 0 24 24"> ... </svg> import styled from 'styled-components'; import Github from 'assets/git ...

Issue: Module 'jasmine-core' not found

I set up the following dependencies for testing purposes: "devDependencies": { "jasmine-core": "^2.4.1", "karma": "^0.13.22", "karma-jasmine": "^0.3.7", "karma-phantomjs-launcher": "^1.0.0" } When I execute karma start, I encounter the fo ...

What is the best way to determine the dimensions of a KonvaJs Stage in order to correctly pass them as the height/width parameters for the toImage function

Currently, I am using KonvaJs version 3.2.4 to work with the toImage function of the Stage Class. It seems that by default, toImage() only captures an image of the visible stage area. This is why there is a need to provide starting coordinates, height, and ...

Preventing Canvas Image Disappearance with JavaScript's onload Event

My current issue involves adding an Image to my webpage. However, every time I hit the refresh button, the image disappears. After researching on Stack Overflow, it was suggested to include window.onload=yourDrawFunction() in the code. Despite following th ...

Is it practical to extract the page type/name from the CSS of the selected tab?

My website has multiple tabs on a single page and I want to integrate Google Analytics code. However, since all the tabs are part of the same page, I need a way to track which tab the user is interacting with. Would using CSS to check for the selected tab ...

The beforeEach hook in Mocha.js does not support the bail(false) functionality

Whenever I attempt to initiate my mocha test with the instruction bail(false), I am looking to ensure that the tests do not halt even if an error is encountered in a beforeEach hook. Despite setting this configuration, it seems like it's not working ...