Receiving the most recent data in a protractor examination as a text string

My goal is to fetch an input value for a specific operation in protractor. I am attempting to make an ajax request using protractor and need to assign a unique value (referred to as groupCode) to a JSON object that will be sent to the server.

Initially, I tried setting up a hidden input field that could be manipulated. Here are my attempts:

<div style='hidden' >
    <input 
        id="group-sendgrid-hidden-input" 
        ng-model='groupCode' 
        value='{{groupCode}}' 
        ng-init='groupCode=dangdangdang'
    >
</div>

I also attempted to display the model value:

<div style='hidden' >
    <input 
        id="group-sendgrid-hidden-input" 
        ng-model='groupCode' 
        value='{{groupCode}}' 
        ng-init='groupCode=dangdangdang'
    >
   {{groupCode}}
</div>

I can confirm that the value updates correctly in the Angular console. This means that $scope.groupCode resolves to 'dangdangdang'. Therefore, the issue lies elsewhere. I aim to retrieve the groupCode string in a protractor test using the following methods:

Here are some of the approaches I've taken:

var groupCodeModel      = element(by.model('groupCode'));
var groupCodeBinding    = element(by.binding('groupCode'));
var placeholder         = groupCodeBinding.getText();

Additionally, I tried getting the value directly like this:

var groupCode = element(
    by.id('group-sendgrid-hidden-input')
).getAttribute('value');


// Later on, use it like:
var sendgridData = {envelope: 'what', test: groupCode};

The problem arises as the value of groupCode never seems to become a string no matter what strategy I utilize.

  1. I'm unable to console.log(groupCode);
  2. This fails:
    var sendgridDataString = JSON.stringify(sendgridData);

Each attempt results in something like this:

{ ptor_: 
    { controlFlow: [Function],
      schedule: [Function],
      getSession: [Function],
      getCapabilities: [Function],
      quit: [Function],
      actions: [Function],....

Although the desired data is present, I struggle with extracting it as I am relatively new to protractor. Despite the challenges, I find protractor technology fascinating. Thank you for any assistance.

Edit:

I have experimented with the following solutions too:

var groupCode = element(by.id('group-sendgrid-hidden-input')).evaluate('groupCode').then(function(groupCode){

        console.log('test: ' + groupCode);
        return value;
});

and also:

var groupCode = element(by.id('group-sendgrid-hidden-input')).getAttribute('value').then(function(groupCode){

        console.log('test: ' + value);

    return value;
});

In the latter approach, groupCode returns:

{ then: [Function: then],
    cancel: [Function: cancel],
    isPending: [Function: isPending] }

It still proves challenging to obtain a simple string like groupCode = 'just some words', which is necessary for passing to another function.

While I believe achieving this is possible, the process is becoming frustrating. I will persevere and continue studying the documentation.

Answer №1

getAttribute() along with various other methods in protractor yield a promise, which must be resolved:

element(by.id('group-sendgrid-hidden-input')).getAttribute('value').then(function (value) {
    console.log(value);
});

For a better understanding of promises, refer to these helpful resources:

Answer №2

Exploring this topic further, we provide insights and address the initial query in a related discussion over at another question I posted regarding the implementation of the solution mentioned here. The project is nearing completion, with just one anticipated condition left to be incorporated into the it test.

Issues Encountered When Attempting to Retrieve String Value from Element in Protractor Test

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

Error in Angular Universal: KeyboardEvent is not defined

I have inserted the word "domino" into the server.ts file and updated the webpack.server.config.js as follows: module: { rules: [ { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { pattern: '&bs ...

Comparing JSON objects with JavaScript models: A guide

Currently I'm working with angular 2 and I have an array of data. data: MyModel[] = [ { id: 1, name: 'Name', secondName: 'SecondName' } In addition, I have created the interface MyModel: interface MyModel { id: number, nam ...

Converting HTML to PDF: Transform your web content into

I have a couple of tasks that need to be completed: Firstly, I need to create a functionality where clicking a button will convert an HTML5 page into a .PDF file. Secondly, I am looking to generate another page with a table (Grid) containing data. The gr ...

Unable to retrieve input using jquery selector

I am attempting to detect the click event on a checkbox. The Xpath for the element provided by firebug is as follows, starting with a table tag in my JSP (i.e. the table is within a div). /html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div/div[2]/table ...

What could be causing my LESS files not to compile properly in grunt?

I've successfully installed npm and ran npm init. Additionally, I've installed the following packages using npm: grunt grunt-contrib-less grunt-contrib-watch jit-grunt --save-dev My Gruntfile.js configuration looks like this: module.exports = f ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

Is it possible to efficiently iterate through map keys in axios using Vue.js?

Currently, I am working on Vue.js exercises that I have created. I am successfully populating a table with data from a JSON API, but I have encountered a challenge. The API contains multiple keys with similar names (strIngredient1, strIngredient2, strIngre ...

Searching for specific data within an embedded documents array in MongoDB using ID

While working with mongodb and nodejs to search for data within an embedded document, I encountered a strange issue. The query functions as expected in the database but not when implemented in the actual nodejs code. Below is the structure of my data obje ...

Object displaying no visible illumination

Recently, I've been experimenting with this project, and after some trial and error, I've managed to get things working to some extent. As a novice in JavaScript, I'm unsure if the issue I'm facing has a simple solution. The problem is ...

How to show a button within a table cell using AngularJS?

The AngularJS application developed by my company was recently handed over to me for further development. Currently, I am working on integrating a navigation button in a table cell that will allow users to navigate to custom pages of their choice. Within ...

Switching visual representation that appears upon clicking the dropdown menu

Issue with Duplicating Dropdown and Image Change const image = document.querySelector('.item__img'); const checkbox = document.querySelectorAll('.imgOption'); function handleChange() { let imgsrc = this.getAttribute("data-value ...

Modifying icon color upon button click in Vue 3: A quick guide

I'm currently implementing Vue 3 into my project. Within the database, there are a total of 10 individuals, each of which should have their own card displayed. Users have the ability to add any person to their list of favorites. Below is the code snip ...

Filling a non-serializable object using Json.NET

Need help with populating an object from a JSON string in a test scenario. The target object includes the Query property: public string Query { get; set; } I'm trying to achieve this: var target = ...; JsonConvert.PopulateObject(target, "{ 'Qu ...

Seeking assistance with web development?

I have a vision for a website that will allow players to store and display a list of games they have played on their profile. The player can add games to the list as they are completed, creating a comprehensive record of their gaming history. To achiev ...

NativeScript element isn't showing up

Being relatively new to application development with NativeScript, I find myself in a situation where I need assistance in finding a solution. Drawing from my experience with PHP, I am now looking to create a template for each "page" of the application. Th ...

Converting "require" to ES6 "import/export" syntax for Node modules

Looking to utilize the pokedex-promise for a pokemonapi, however, the documentation only provides examples on how to require it in vanilla JavaScript: npm install pokedex-promise-v2 --save var Pokedex = require('pokedex-promise-v2'); var P = new ...

npm-bundle encounters an issue with Error: ENOENT when it cannot find the file or directory specified as 'package.json'

npm-bundle is throwing an error that says Error: ENOENT: no such file or directory, open 'package.json' in my NodeJs project. It works fine if I manually create test.js and package.json, then run npm install followed by npm-bundle. However, when ...

"Enhance Your Angular App: Implementing Custom Fallback Languages for Specific Locales

Is there a way to create language translations that can support different versions of English with a common fallback option? Here is an example setup: locales/en_AU.json locales/en_US.json locales/en.json locales/fr_BE.json locales/fr_FR.json locales/fr. ...

Can you explain the distinctions among “assert”, “expect”, and “should” in the Chai framework?

Can you explain the variations between assert, expect, and should? How do you know when to utilize each one? assert.equal(3, '3', '== turns values into strings'); var foo = 'bar'; expect(foo).to.equal('bar' ...

Tips for automatically refreshing a Next.js application following an update in an external library

I have a monorepo containing two applications: The first is a Next.js web app The second is a UI library using Tailwind CSS and Microbundle Currently, the only way I can get the web app to recognize changes made in the UI library is by following these st ...