Troubles encountered while trying to resolve a promise with browser.findElements in Protractor

Currently, I am working on developing a test in Protractor which involves using Jasmine and TypeScript to click on each link listed within a table. The objective is to display the text of the link being clicked before proceeding. However, I have encountered an issue during this process. Whenever I attempt to utilize .getText() on the element containing all links, it outputs ManagedPromises instead of the expected text. Despite researching similar problems online for solutions, my implemented code closely resembles what was recommended. Below is the snippet of my code:

browser.findElements(By.xpath('//tBody//a')).then((clickLinks) => {
                console.log('made it [' + i + ']');
                console.log('link clicked[' + i + ']: ' + clickLinks[i].getText());
                clickLinks[i].click();
                i++;

                browser.waitForAngular();
                browser.driver.navigate().back();
                browser.waitForAngular();
            });

Answer №1

This explanation delves into the concept of ManagedPromises, emphasizing that the method Elementfinder.getText() doesn't return a simple string as expected, but rather a Promise object. It clarifies that getText() is an async function, which always returns a Promise, serving as a wrapper for a value and providing a useful approach to code management. It suggests taking time to understand Promise Objects.

In addition, it points out that in order to retrieve text from an element, one must use getText().then(Func), presenting an example using

browser.findElements(by.xpath('//tBody//a'))
.

The discussion also highlights the importance of handling async functions correctly, illustrating the difference between executing functions directly versus chaining them with then(). The introduction of the await keyword in javascript simplifies this process within async functions, streamlining the code structure for better readability.

To encapsulate these ideas, a clean example of utilizing waitForAngular() and navigate().back() using async functions and await is provided, showcasing an organized and efficient coding approach. Overall, the explanation aims to clarify concepts and help developers navigate through async operations effectively.

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

Encountering a checked exception during the automation of a web application in Firefox with Selenium WebDriver

import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class shopComLogin { public static void main(String[] args) throws InterruptedException{ //Opening Firefox Browser WebDriver window=new FirefoxDriver(); //N ...

Challenges faced when locating elements in selenium through xpath

I am encountering difficulty in locating the search button using selenium. The same issue persists when I utilize xpath for identification. My objective is to click on the SearchButton element. Presented below is a snippet of the HTML code. I am attempti ...

Exploring the capabilities of the hardware camera in Angular 2

Struggling to implement the tutorial in Angular2. The challenge lies in making navigator.mediaDevices.getUserMedia function properly. The error message indicates that mediaDevices is not recognized on type 'navigator'. Refer to the media capture ...

Managing multiple asynchronous requests through Observables in web development

I am working on an Angular2 website that sends multiple ajax requests using Json Web Tokens for authorization when it is initialized Here are two examples: public getUser(): Observable<User> { // Code block to get user data } public getFriends ...

Issue with ng-maxlength directive

In my Angular 4 Application, I am attempting to implement validation for form inputs. Specifically, I want to restrict inputs with numbers to not accept more than 4 digits. If the input contains more than 4 digits, the form should be marked as invalid. I a ...

"Despite using Java Selenium, the element remains unclicked and no error is displayed

When using selenium webdriver, I encountered a menu that is not a "Select menu" and needs to be clicked on in a normal fashion. wait.until(ExpectedConditions.elementToBeClickable(diagnose_Type)); ((JavascriptExecutor) driver).executeScript("arguments[0].c ...

Accessing files in a maven project's resources folder with selenium WebDriver

I am encountering an issue when trying to open various file types using webdriver, such as .txt and pdf. The files are stored in the resources folder, but I keep receiving a "malformed exception" error message when attempting to open them. Interestingly, ...

Testing attributes and props with React Testing Library

Currently, I am in the process of developing a React application using TypeScript. For creating components, I rely on material-ui and for unit testing, I make use of react-testing-library. Specifically, I am working on designing a wrapper for the Grid com ...

The process of subscribing to a service in Angular

I currently have 3 objects: - The initial component - A connection service - The secondary component When the initial component is folded/expanded, it should trigger the expansion/folding of the secondary component through the service. Within the service ...

Transmit a sequence of keys to the web browser

I'm having difficulty in sending a Shift key command followed immediately by tilde (~). I've attempted various examples, and here's one that I'm currently working on. I am testing the following scenario - selecting a specific image, t ...

Display Material Popup in Angular before user leaves the page

My goal is to display an Angular Material Dialog Box (Popup window) when the user clicks the Chrome Window Close button. The Dialog modal should prompt the user if they want to save changes or cancel. However, the modal only appears for a brief moment and ...

Which wait method is recommended to use with the sendKeys function for an element in Python Selenium?

When using sendKeys to set a value for an element attribute that may load slowly or be obstructed by an animation, what type of wait should be used for the element? Currently, visibility_of_element_located from Selenium's expected_conditions library ...

Using a TypeScript decorator to enhance a class with a new property

I'm in the process of developing a custom decorator that adds a specific property to the target class. Here is my current implementation for the decorator: export type Constructable<T> = new (...args: any[]) => T; export function Validation ...

Ensure that all other components executed prior to initiating the button's mousedown event

Within my formly setup, there is a button with a mousedown event triggering a method named "analyzeClick()" <button *ngIf="show" (mousedown)="analyzeClick()" id={{htmlID}}> {{label}} </button> Additionally, we have input ...

Determine the data type of the property in an object that needs to be provided as an argument to a

I am currently in the process of creating a function that can take in an object with a specific data type, as well as a function that acts on that data type as its argument. const analyze = < Info extends object, F extends {initialize: Info; display ...

In Angular, the object may be null

click here for image Encountering an error message stating that Object is possibly 'null' when utilizing querySelector and addEventListener in Angular. ...

Python/Selenium issue: The website is detecting the automated driver and restricting access

In my experience using Python 2.7 and the Selenium driver for Chrome, I have successfully automated various tasks on different websites, including dealing with iFrames, login pages, and OTPs. Essentially, I am quite familiar with navigating through these e ...

Retrieve fresh information every 30 seconds utilizing the useQuery hook in React

I am utilizing '@tanstack/react-query' to retrieve data in my React + Typescript application. Below is a snippet of my code, where I aim to fetch metric data every 30 seconds import { useQuery } from '@tanstack/react-query'; import ...

PlayWright - Extracting the text from the <dd> element within a <div> container

Here is the structure I am working with: <div class="aClassName"> <dl> <dt>Employee Name</dt> <dd data-testid="employee1">Sam</dd> </dl> </div> I am attempting to retrie ...

ReactJS - Triggering a timeout reset and reactivation with a single button press

When I click a button that has a callback function, I want it to start a timeout with a 5-second delay. If the button is clicked again within that 5 seconds, I want the timer to reset without triggering the timeout handler. The handler should only be calle ...