The protractor/webdriver carries out promises in the given order

Here is a snippet of code that extracts text from a web page, selects an input box, clears it, and then enters the extracted text into the input box:

var pText = browser.element(by.css('some_tag')).getText();
var inputbox = browser.element(by.css('[data-aid="login-app-input-username"]'));
inputbox.clear();
// if pText is defined here, everything is ok.
inputbox.then(
    function(e){
        pText.then(
            function(text) {
                e.sendKeys(text);
            });
    });

The issue here is that the sendKeys() function is being executed before the clear() function, even though the code is written in order. Moving the declaration of pText below the clear() function solves the problem.

My question is how can we ensure that the above code runs in the correct order without relying on the position of the pText declaration. It's worth noting that using a promise for sendKeys() is not the solution I am seeking.

Answer №1

Here is a simple solution:

let paragraphText = browser.element(by.css('some_tag')).getText();
let inputField = browser.element(by.css('[data-aid="login-app-input-username"]'));

paragraphText.then(function(text) {
  inputField.clear().sendKeys(text);
}

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

Issue with JQuery Event Listener on Canvas Subelement not functioning

I encountered an issue while trying to implement a custom crop rectangle on a canvas using JavaScript. I created a function that, when called, should add a child node to the existing canvas and use JQuery listeners to draw the rectangle. However, although ...

Pushing state history causes browser back and forward button failure

I'm currently utilizing jQuery to dynamically load content within a div container. On the server side, the code is set up to detect if the request is being made through AJAX or GET. In order to ensure that the browser's back and forward buttons ...

Retrieve the HTML element corresponding to the button that was clicked

How can I use jQuery to retrieve the HTML element containing the button that was clicked? Once a button is clicked, I need to obtain the invite object associated with that button and then send a post request to a specific URL. {% if invites %} {% for ...

Utilize the "incorporate" feature to include any string within an array

I am currently working on improving the search function in my application. This particular search function takes input from a search bar and is designed to handle multiple search terms. For example, it should be able to handle queries like "javascript reac ...

The attempt to email the designated server at smtp.mailtrap.io:465 was unsuccessful

Hello, I am currently using 'maven' in Eclipse. I attempted to send emails using smtp.mailtrap.io but encountered some errors. Can someone help me find a solution? Email email = new SimpleEmail(); email.setHostName("smtp.mailtrap.io"); email.set ...

Master the art of adjusting chart width on angular-chart with the help of chart.js

I am currently using angular-chart along with Angular and chart.js to create multiple charts on a single page. However, I am facing an issue where each chart is taking up the entire width of the screen. I have tried various methods to limit the width based ...

Tips for creating a mobile-friendly 30-day chart with chart.js

I created a chart displaying 30 days of absence data using the react-chartjs-2 library, but it is not responsive. How can I make it responsive? Here's how it appears on a laptop: chart on laptop And this is how it looks on mobile: chart on mobile T ...

What could be causing the malfunction of this Ajax conditional dialog?

Can anyone help me troubleshoot this code? I've been trying to fix it for a while now, making multiple changes, but still can't find the solution. If you have any ideas please let me know, I haven't seen any errors in the console. The fir ...

What is the best way to retrieve an array of objects from Firebase?

I am looking to retrieve an array of objects containing sources from Firebase, organized by category. The structure of my Firebase data is as follows: view image here Each authenticated user has their own array of sources with security rules for the datab ...

Tips for postponing 'not found' error until the data has finished loading in Vue.js

I am accessing the following route: http://site.dev/person/1 The structure of my component is as follows: // PeopleComponent.vue <template> <div> <template v-if="person == null"> <b>Error, person does not exist.</b& ...

Is it possible to achieve a seamless interaction by employing two buttons that trigger onclick events to alternately

I have a specific goal in mind: I want to create two buttons (left and right) with interactive cursors. The left button should initially have a cursor of "not-allowed" on hover, indicating that it cannot be clicked at the beginning. When the right button ...

GPT Open-Source Initiative - Data Ingestion Challenge

Seeking assistance with setting up the following Open Source project: https://github.com/mayooear/gpt4-pdf-chatbot-langchain Encountering an error when running the npm run ingest command: Error [Error: PineconeClient: Error calling upsert: Error: Pinecon ...

Tips for building a material design input form with CSS and Bootstrap

I am working on creating a material design input form using CSS and Bootstrap. The current code I have is not providing the exact result I desire. If you want to view the source code, check out this Code Pen Link Here is the HTML CODE that I am using: & ...

Can you explain the structure of the new options JSON object for the server.register method in hapi.js?

Can anyone explain the new format for the options json object that needs to be passed to the server.register() method in hapijs? This is how I structured my server.register() call. server.register({ register: require('good'), option ...

When JSON data is copied and displayed in Node.js or JavaScript, it appears as a string

I have written a code to copy one JSON file into another JSON file. Here is the code: function userData(){ fs.copyFile( path.join(process.cwd(), "user/access.json"), path.join(process.cwd(), "client/access.json"), ...

What is the best way to assign a return value to a variable in JavaScript?

var robotDeparture = "The robot has set off to buy milk!" var farewellRobot = return robotDeparture; I'm attempting to show the content of the robotLeaves variable using a return statement. Furthermore, I intend to assign this return statement to a v ...

Utilizing Selenium WebDriver with Various Web Browsers

Currently, I am tackling a Selenium test project that requires me to initialize two browsers. Once the setup is complete, the task at hand involves switching between these two browsers. This means I will have [Window1] and [Window2] open simultaneously. ...

JavaScript and the proper way to format the weekdays

Learning programming is new to me, so please bear with me. I am currently utilizing Informer, a reporting website that retrieves data from Datatel, a unidata database. My current task involves working on a computed column that only accepts Javascript and ...

Ways to verify that a ray does not intersect the face further

My approach involves creating cubes and rectangles on my scene with userData that initially have all 6 parameters set to "false". I then cast a ray from each face and if it intersects with another object's face, I set that specific face's paramet ...

Make sure to incorporate certain node_modules folders within Babel 7

My issue involves a dependency located in the node_modules directory that requires compilation through Babel. Despite upgrading my stack, I am unable to get Babel to compile the dependency. Current versions: @babel/core 7.5.4 webpack 2.7.0 Here is my w ...