Using TypeScript, extract the value of a Promise from a Page Object

Struggling to retrieve a value from a WebDriver promise in a Protractor solution using TypeScript, the response keeps coming back as undefined.

get nameInput(): string {
    var value: string;
    this.nameElement.getAttribute('value').then(v => value = v);
    return value;
}

In the current scenario, it appears that the function is not properly handling the timing of the promise resolution. To address this, I attempted to refactor the code by changing the return type to WebDriver's promise:

getNameInput(): webdriver.promise.Promise<string> {

    var nameElement = element(by.id('name'));
    return nameElement.getText().then(v => { return v });

}

However, the return value is showing up as Function instead of the actual result stored in v. This issue seems to be related to Jasmine's handling of promises, which behaves differently compared to running the code in JavaScript.

While I understand that executing the promise directly within the expectation may work, my preference is to separate the logic into standalone functions and pass them to expectations without adding promise-related complexity to test cases.

Any suggestions or insights on how to tackle this?

Answer №1

If you want to handle promises in a more efficient way, simply return them instead of resolving them:

getUserName(): webdriver.promise.Promise<string> {
    var nameElement = element(by.id('name'));
    return nameElement.getText();
}

Make sure to retrieve the value returned by getUserName() and handle it appropriately in your test script:

getUserName().then(value => { console.log(value) });

Additionally, keep in mind that you can also use expect() to implicitly resolve the promise:

expect(getUserName()).toEqual("Expected Username");

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

Retrieve information from an XML document

I have some XML content that looks like this: <Artificial name="Artifical name"> <Machine> <MachineEnvironment uri="environment" /> </Machine> <Mobile>taken phone, test when r1 100m ...

In order to address the webdriver.gecko.driver problem in Selenium WebDriver, one must establish the correct path to the driver executable

I am currently attempting to execute the code snippet below: import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Test { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); ...

Getting the toState parameter using the $transitions service in the latest version of the ui-router

In my project, I am currently utilizing ui-router version 1.0.0-alpha.3. Please note that the older events have been deprecated in this version. As a result, I am in the process of migrating from: $rootScope.$on('$stateChangeStart', (event, toS ...

"The implementation of real-time data retrieval through Socket.io is currently not functioning as expected

As a beginner in mean-stack, I tried to implement real-time data following some instructions but encountered errors. Can someone guide me on how to correctly use socket.io? I have provided my code below for fetching users from mongodb, kindly review and co ...

Websites experiencing horizontal scrolling functionalities

I have noticed that in my angular project, the website becomes horizontally scrollable when I log in. This only happens after logging in, and does not occur beforehand. I'm using angular calendars and Bootstrap for styling. What could be causing this ...

Tips on how to cancel a $http request with cache:true when the header is modified

Currently, I am retrieving a remote resource and storing the outcome in cache: $http({ method:'GET', cache:true, url:'...' }); This process works smoothly. However, whenever a user modifies the language in the UI, I automa ...

Java.lang.UnsupportedClassVersionError: org/openqa/selenium/WebDriver is causing an issue due to an unsupported major.minor version 52.0 in Selenium WebDriver

I have the following tools installed: Eclipse eclipse-jee-luna-SR2-win32-x86_64 Java - JRE7 jdk-8u121-windows-x64 Selenium Java Client Libraries - 3.3.1 I am trying to start using Selenium, and I wrote a simple program. However, I encountered an error m ...

Combine values from 2 distinct arrays nested within an array of objects and present them as a unified array

In my current array, the structure looks like this: const books[{ name: 'book1', id: '1', fromStatus: [{ name: 'Available', id: 1 }, { name: 'Free', id: 2 }], t ...

Creating a dynamic ng-options in AngularJS

Why does this code work: <select ng-model="country" ng-options="{{selectOptions}}"> <option style="display:none" value="">Select country</option> </select> The json country list consists of objects with properties {id :... ...

You are receiving a TypeError because the unbound method move_to_element() should be called with an instance of ActionChains as the first argument, but instead it was given a

Currently, I am using the latest version of selenium with Python 2.7.8. When trying to perform a drag and drop action onto a <div id="droppable"> element using jQuery UI, I encountered the following error message: TypeError: unbound method move_to_el ...

Is it possible to utilize a variable within the 'has-text()' function during playwright testing?

With Playwright, I am attempting to locate an element based on the value of a variable For instance: let username = 'Sully' await page.click(`li:has-text(${username})`) However, I encounter the following error: page.click: Error: "has-tex ...

Determining the return type based on an optional generic type in TypeScript

I have created a function that generates an object (map) [key] : value from an array. My goal is to make the value method optional, and if not provided, simply return the item as it is. Here is the code I have written: export default class ArrayUtil ...

experiencing trouble with selenium webdriver unable to locate and interact with a specific button | bootstrap framework

I am currently working with a chrome web-based application that utilizes bootstrap. This presents a challenge when attempting to retrieve the xpath or cssselector of an element using dev tools. Below is the code snippet for a button I am trying to click: ...

Evaluating the loading time of various web components using Selenium

Measuring the load times of different parts within a spinner element is something I want to accomplish. The HTML structure in question is as follows: <div id="loading"> <div id="spinner"> <div class="spinner-icon"></div> ...

Encountered a unique error code "TS1219" in Visual Studio

Recently, I made some changes to the architecture of my UI project and encountered a slew of errors (TS1219 and TS2304). Could the culprint be a poorly configured tsconfig.json file, or is it something else entirely? Despite encountering no issues when dec ...

Eliminate the empty choice for Angular when dealing with dynamic option objects

Looking for assistance with this code snippet: <select ng-model='item.data.choice' > <option ng-if='item.data.simple_allow_blank == false' ng-show='choice.name' ng-repeat='choice in item.data.simple_choices&ap ...

Some websites are experiencing issues with incomplete HTML responses when using Requests & BeautifulSoup or Selenium

When attempting to extract information from various URLs using Requests and BeautifulSoup in Python, I encountered a problem where some websites returned only a partial HTML response without the actual content of the page. Below is the code that failed to ...

Exploring ngModel components with a specified class

In my HTML code, I've assigned many elements with ngModel defined as ng-model = "object.[something]". For example: <div class="form-group row" ng-model="object.askUser"> I use this method to keep my purpose clear for these elements. My questi ...

Python script that sets the default download directory for the Edge web driver

Struggling to update the download location for my Microsoft Edge web driver - seems like I'm missing something. I attempted to replicate settings from Chrome but it's not quite working out as planned. Here's what I have so far: from seleniu ...

What is the best way to utilize the outcome of the initial API request when making a second API call in AngularJS

Looking to utilize the response of a first API call in a second API call. The situation is such that I need to fetch data from the first API and use it in the second API call. I believe a synchronous API call would be necessary. While attempting to imple ...