Changing text content into objects in Protractor

I am facing an issue with a span tag that contains JSON text, which I need to convert into an object in Protractor for testing purposes.

{"type":"msax-cc-error","value":[{"Code":22104,"Message":"Card holder is required"},{"Code":22058,"Message":"Card number is required"},{"Code":22109,"Message":"Card type is not supported"},{"Code":22103,"Message":"Expiration year is required"}]}

Is there a way to successfully convert this text into an object in Protractor? I have tried the following code snippet but it does not seem to be working as expected.

it('Submitting should be ignored and prompt appropriate error code', function() {
    //assign the element text to a variable
    var response = element(by.css('.message-box .message-in')).getText();
    expect(response.type).toBe('msax-cc-error');
});

Answer №1

When you utilize the getText() function on an element, it will return the text of that element as a string. To work with this result as an object, you need to convert the string into an object using the JSON.parse() method. Here's an example:

var response;
element(by.css('.message-box .message-in')).getText().then(function(text){
    response = JSON.parse(text);
    expect(response.type).toBe('msax-cc-error');
});

I hope this explanation helps.

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

What is an alternative method to retrieve form data without relying on bodyParser?

When it comes to accessing posted form data without using bodyParser, what alternatives are available? How exactly does bodyParser grant access to the data in req.body? Additionally, I am curious about the inner workings of this process on a deeper level. ...

Leveraging symbols in JSON with Python

I encountered an issue when working with JSON in Python recently, specifically related to special symbols. The problem can be seen in the following code snippet: import json app = { "text": "°" } print(json.dumps(app, ind ...

Sending a JSON object as a string to PHP

I am currently using a JQuery script to manipulate an image. While most functions are working correctly, I am facing issues with returning data to PHP. Despite trying various examples from Stackoverflow, I have not been able to successfully return the data ...

Managing the versions of ViewState and serialized objectsIs this what you

We utilize ASP.NET viewstate to store custom objects (as opposed to only primitive types). With each build, the build number undergoes incrementation. The challenge we face arises during a server update: Visitor loads the page We roll out a fresh build o ...

Evaluating the criteria and presenting two distinct notifications with setCustomValidity

When validating text fields in an HTML form, I am looking to check two conditions. Currently, the code below checks if the phone number entered is in the correct format and displays a message 'Enter a valid mobile number' if it is incorrect using ...

Unveiling class based on JSON attribute name with GSON

The JSON data provided includes information about different classes like BaseClass, ExtendedClass1, ExtendedClass1_1, and ExtendedClass2. There are attributes such as id, sum, total, and expr associated with these classes. { "list": [ { ...

Is there a way to access the state value within the reducer function of createSlice?

Currently, I am utilizing redux-toolkit within my react project. A concern arises in a specific reducer inside the createSlice method where I aim to incorporate an existing array of entities from the state and then merge it with a new array before finalizi ...

Updating state from a React mapping component: A step-by-step guide

I am working on an API that needs data filtering and then I want to place the filtered data into a state export default class ModifyPage_Single extends React.Component { constructor(props) { super(props) this.state = {data:[],idd:" ...

Utilize Angular 2 interceptor to incorporate HTTP requests

Dealing with the 401 response from an interceptor using the HttpClientModule in Angular and JWT authentication can be a bit tricky. When the accessToken expires, it's necessary to use the refreshToken to obtain a new one before making the actual API r ...

The AJAX function fails to trigger the MVC controller method

I'm attempting to enable inline editing by cell, rather than by row, when double-clicking. Although the initial setup is working, it's not updating the record as expected - the "SaveCustomer" call to the controller isn't triggered. Can anyon ...

Injecting randomness into webpage backgrounds with Flickr API

How can I use the Flickr API to display a random photo tagged with "landscape" on my website? I've tried inspecting the HTML using Chrome DevTools, but no photo is showing up. What am I missing? $(document).ready(function() { $.getJSON("https:// ...

Is it possible to populate a ReactJS TreeView with a JSON/array of objects using Material UI?

My data structure consists of an array of objects with values to be displayed in the <TreeView> component: const treeItems = [ { id: uuidv4(), name: 'English', children: [ { id: uuidv ...

Issue encountered with asynchronous execution while utilizing AWS Cognito registration process

I am developing a user management system using Amazon Web Services called Cognito. Everything works fine locally, but I encounter issues when running it on a Wamp server. I cannot seem to pinpoint the cause... could it be due to asynchronous execution? ...

Leveraging jQuery for Adding Text to a Span While Hovering and Animating it to Slide from Left to

<p class="site-description">Eating cookies is <span class="description-addition"></span>a delight</p> <script> var phrases = new Array('a sweet experience', 'so delicious', 'the best treat', ...

Discover the process of iterating through JSON URLs and extracting data within Google Sheets using Apps Script

Utilizing data from the Snapchat API (For more information visit:) and integrating it into Google Sheets using Apps Script. A specific JSON response mandates individual IDs for obtaining precise information. Although my script functions correctly and gen ...

Align the image in the middle using JavaScript for Firebase

I am struggling to display the center of an image within a circle-shaped <img> tag with a border-radius of 50%. The issue arises when trying to display an image fetched from Firebase storage, rather than from a URL. In attempt to solve this problem, ...

The functionality of Ajax with JSON is failing to work properly in Internet Explorer 11

While this ajax code functions well in Firefox, Chrome, Safari, and IE9, it encounters issues in IE 11. $.ajax({ type: req_type, url: req_url, crossDomain: true, cache: false, data: req_data, contentType: "application/json; charse ...

Step-by-step guide on inserting a div element or hotspot within a 360 panorama image using three.js

As I work on creating a virtual tour using 360 images, I am looking to include hotspots or div elements within the image that can be clicked. How can I store data in my database from the event values such as angle value, event.clientX, and event.clientY wh ...

Increase the jQuery level and utilize the .find() method

Currently, I am delving into the realm of jquery and facing a minor hiccup with selectors. Below is the structure of my DOM: <li> <header class="title"> <span>Text</span> <a href="#work-1">My trigger</a> ...

Delivering an Angular 1.x app through Express while granting access to static directories

I have configured my Express server like this: // src/server/server.js var express = require('express'); var app = express(); app.use('/libs', express.static('./node_modules/')); app.use('/app', express.static(&apo ...