Protractor - retrieving the text from a dropdown menu selection

Currently, I am utilizing protractor for the e2e testing of my application. In the process of updating a record, I am creating an array of new values to compare with the old ones to ensure proper updates.

One of the fields in question is a select menu, and I am aiming to extract the text from the option and add it to the array.

To select the option, I am using:

element(by.id(fieldName6)).click().then(function() {
     element(by.css('#' + fieldName6 + ' option:nth-child(2)')).click();
});

This method is working smoothly. However, now I need to retrieve the text (not the value) of that option to include in the array. I attempted the following approaches:

newValues.push(browser.executeScript("$('#" + fieldName6 + " option:nth-child(3)').text()"));

which resulted in

Promise::17234 {[[PromiseStatus]]: "pending"}
, and

newValues.push(element(by.css('#' + fieldName6)).$('option:checked').getText());

which returned [object Object].

I seem to be stuck at this point. Any assistance or guidance would be highly appreciated.

Answer №1

Selenium and WebDriverJS rely heavily on the idea of promises. The code snippet

element(by.css('#' + fieldName6)).$('option:checked').getText()
will give you a promise that will be fulfilled with the text of an option by the Control Flow.

You can gather promises into the array newValues, but when actual values are needed, the promises must be resolved. A good approach for this is using protractor.promise.all():

protractor.promise.all(newValues).then(function (resolvedValues) {
    console.log(resolvedValues);
});

If you only want to assert the values in newValues, you can directly use expect() - it automatically resolves the promise before checking:

expect(newValues).toEqual(["option 1", "option 2"]); 

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

Initial function call to modify the state does not result in any field updates

As I delve into integrating GraphQL with a React web application for testing and educational purposes, I am humbly admitting to being a newbie in this domain. To provide a brief overview, I am working on a book library application, where we have a compila ...

Guide on extracting data from a JavaScript table using Python

Looking to extract information from a table on this page: . There are a total of 18 pages, but the url remains constant for each page. My usual method involves using BeautifulSoup to scrape data from HTML pages. However, in this case, the required data is ...

The issue arises when attempting to use a JavaScript marker within an array, as it

At the moment, I am in the process of building a website that includes a Google map showcasing my custom markers. Each marker is linked to a specific URL, and the connection is straightforward (as shown with one of my markers below) - var image = 'p ...

Is there a way to delay loading 'div' until a few seconds after the 'body' has been fully loaded?

Is there a way to delay the appearance of the "text-container" div after the image is displayed? I have attempted to achieve this using JavaScript with the following code: <script> window.onload = function(){ var timer = setTimeout("showText()",700 ...

What could be causing setInterval to only run once?

setInterval is only running once for me. I tried using setTimeout and creating a loop inside the function, but it's still giving the same output. I'm working with brackets and using live preview, in case that matters. const buttons = document. ...

Evaluating the similarity between a Guild ID and a matching string

Currently, I am in the process of creating a bot with a unique feature - a config command that enables users to customize specific functionalities within their servers. This customization is facilitated through a straightforward JSON file named config.json ...

Animation glitch when clicking on hamburger menu symbol

I am facing an issue where, in mobile window mode, if I click on the burger icon and then zoom out to a larger resolution and zoom back in to mobile size, clicking on the burger icon does not activate the slide-down menu as expected. You can see the issue ...

Links to Help You Find Your Way

I have recently embarked on restructuring a website that currently utilizes frames and framesets. The new structure adopted is a typical structure.html: <body> <div> <div id="headerNavigation"></div> <div id="PageConten ...

Transforming each element of an observable array by updating their properties with data retrieved from an ajax request using knockout

In my ViewModel "AppViewModel", I am fetching JSON data to create the model. One of the properties, "totalSessions", requires an ajax call to be fetched. While my code runs without errors, the view does not update as expected. var jsonapparray = []; ...

TestNG unable to run through the command line on Ubuntu operating system

Running TestNG through the command line in Ubuntu results in the following error: java –cp "bin:/home/john/selenium-lib/*" org.testng.TestNG src/testing.xml Error: Could not find or load main class –cp src/TestCase */ package Test; import org ...

Error [ERR_MODULE_NOT_FOUND]: Module could not be located in vscode

Issue with VS Code: Module Not Found Error View the image associated with the erroreN.png ...

Determine the originating component in React by identifying the caller

Can you explain how to access the calling component in a React application? function MyCallingComponent() { return <MyReadingComponent/> } function MyReadingComponent() { console.log(callingComponent.state) } ...

Expect a promise to be resolved in the RootCtrl of Angular using $http

One of the functions in my RootCtrl is responsible for calling an http api and returning the result. $scope.checkAccess = function(){ var result = MyService.me(); result.then(function(response){ console.log(response); if (response. ...

Learn the process of synchronously loading forms and data in Angular

Looking to synchronize the loading of data and form... Let's start by reviewing the code: ngOnInit() { if (this.data.fromCalendar) { this.singleTraining(); } }, 200); this.formControl(); } formControl() { this.gib ...

Guide to center-aligning ElementUI transfer components

Has anyone successfully incorporated ElementUI's transfer feature inside a card element and centered it? https://jsfiddle.net/ca1wmjLx/ Any suggestions on how to achieve this? HTML <script src="//unpkg.com/vue/dist/vue.js"></ ...

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(); ...

The Javascript toggle for hiding and showing did not function as expected

I've been attempting to create a JavaScript toggle so that when I click on any of the buttons below, only that particular div is shown and all others are hidden. Unfortunately, my code is not working as expected: function toggle(show,hide) { do ...

Does it typically occur to experience a brief pause following the execution of .innerHTML = xmlhttp.responseText;?

Is it common to experience a brief delay after setting the innerHTML with xmlhttp.responseText? Approximately 1 second delay occurs after xmlhttp.readyState reaches 4. This issue is observed when using Firefox 3.0.10. ...

The setInterval() function is not executing the IF Else statement correctly

I'm having an issue with accessing if else conditions. Currently, both the if block and else block are being called at the same time. I need help in making the if else block work properly. Here is a snippet of my code: const UserCard=(props)=>{ c ...

Sending a json array from PHP

I spent several hours searching for solutions to my problem but couldn't find an answer. I'm trying to perform a search on the same page using jQuery, AJAX, and PHP. Unfortunately, the array from PHP is still returning undefined. Here is the P ...