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

The functionality of AbortController seems to be malfunctioning when used in conjunction with ajax and

I am having trouble canceling the API when leaving a page, despite using an AbortController. Here is my code snippet: import { fetchSingleList } from '@/api/scada' data() { return { abortController: new AbortController() } } beforeDestroy ...

The Unexpected Flash: Troubleshooting an AJAX Request Gone Awry in CompoundJS

I am attempting to utilize the flash('error', 'error text') function in order to notify the webpage of any errors that may occur during an ajax request. The ajax request accesses an action that involves working with a database, making i ...

Is it possible to prevent a webpage from being refreshed?

I need help with an HTML page that puts the worker on pause time which will be subtracted from his/her day job. The issue is that when he/she refreshes the page, the timer starts from the beginning automatically. I want it to continue without resetting. Is ...

Why is the parameter value becoming null during an Ajax call?

I am passing a parameter from HTML to JSP, but when I try to retrieve the value in JSP, it returns null. Can someone help me figure out what's wrong with my code and provide any suggestions? The value is successfully displayed in an alert using JavaS ...

Record of Recaptcha actions is not showing up in the reports

My method of running the recaptcha script is as follows: let data = { action: 'homepage' }; grecaptcha.ready(() => { grecaptcha.execute('RECAPTCHASITEKEY', data).then((token) => { recaptcha_token = token; }); ...

Exploring the possibilities of integrating Redis with loopback-MongoDB is a fascinating journey

Looking into implementing caching for an API and came across Redis. After reading the documentation provided by Loopback here, I am still unsure how to properly connect and utilize it within Loopback. Below is my datasource.json configuration: { "db": ...

What is the best way to display a component based on a certain condition?

There are two instances where the Items component needs to be displayed. However, in one instance an additional component, Filters, is required while in another it is not. The Filters component is nested inside Items. When attempting to implement this, ...

Parsing Problem---Is there a Parsing Error?

My code includes a function that calculates the total of cells and then displays it in a textbox: function UpdateTotal() { var total = parseFloat('0.0'); $("#<%= gvParts.ClientID %>").find("tr").not(".tblResultsHeader").each(funct ...

Enhance the data structure by including extra fields post JSON de-serialization using play-json-extensions

I have a scenario where my case class consists of more than 22 parameters. case class Model(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, ...

Is there a JavaScript alternative to wget for downloading files from a specified url?

"wget http://www.example.com/file.doc" can be used to download the file to the local disk. Is there an equivalent way to achieve this in JavaScript? For example, let's look at the following HTML snippet. <html> <head> <script langu ...

Deciphering Arrays of JSON Information

I need to extract data from a JSON object. Currently, I am using the code snippet below to decode it: $json_array = (array)(json_decode($response)); After decoding, the JSON array contains the following data: I am specifically interested in the details s ...

What are the drawbacks of starting with Angular CLI?

Contemplating whether to incorporate Angular CLI into my upcoming project has been on my mind. My main motivation for considering it is to bypass the complexities of setting up a new project and instead focus on mastering the new version of Angular while c ...

Issue with retrieving data using $_GET in jQuery select2

I am facing a unique situation. Here is the code snippet for select2: $("#tags").select2({ multiple: true, placeholder: "Please enter tags", tokenSeparators: [',', ' '], initSelection : function (element, callback) { ...

Text values slider bar

Currently in the process of designing a permissions screen that would greatly benefit from the inclusion of a slider bar. The goal is to make it easy for users to set permissions for different sections of the site. By utilizing a horizontal slider bar with ...

The error message states: "Python encountered a TypeError where a 'str' object does not allow item assignment when working with JSON files."

Here is the code snippet I am working on: import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data[ john ][ user ] = 'hey' This is the cont ...

Updating the CSS: Using jQuery to modify the display property to none

I am facing an issue with displaying an element that is defined as display:none in CSS. I tried to use the .show() function in jQuery, but it's not working as expected. Here's the code snippet: CSS .element { position: absolute; display: no ...

The Shell Application is not refreshing React HtmlElement Micro Front end

I am currently facing an issue when trying to inject the following React MFE into another Angular shell application. The MFE loads successfully the first time, but if it is hidden or removed from the DOM and then reloaded, it fails to load. Could you plea ...

What is the proper way to call $digest() in AngularJS

My task is to measure the duration of $digest cycles within my Angular application for performance monitoring purposes. What I ideally want to do is capture the time just before starting the $digest() function and again just after it has executed. I have ...

How can jQuery help me load a lengthy webpage with various backgrounds that change according to the vertical scroll value?

I have been given a design that is 960px wide and approximately 7000px tall, divided into five segments stacked vertically at random points. There is a fixed sidebar that scrolls to each segment when a navigation link is clicked. The layout includes slider ...

Utilize HighCharts to seamlessly implement multiple series with the help of AJAX requests

I am facing an issue with plotting the performance results on HighCharts using AJAX. The problem lies with the .addSeries API call not functioning correctly. I need assistance in determining if my approach is correct. $(function () { $('#chart3&a ...