Struggling to retrieve the input value using protractor

Currently, I am in the process of developing an application using angularJS and testing it with protractor.

My main issue lies in verifying the value within an input field. Here is the specific input code:

<input id="rank-max-range" class="form-control has-feedback" name="inputMax" type="number" placeholder="Maximum" ng-model="selectedItem.range.max" min="{{selectedItem.range.min}}" max="{{selectedItem.answers.length}}">

Below is the test script that I'm trying to execute:

it('should fill the max range with the given value', function() {
  element(by.id('rank-max-range')).sendKeys(2);
  expect(element(by.id('rank-max-range')).getText()).toEqual(2);
});

However, this test is not functioning as expected. After consulting the documentation, I learned about the getAttribute('value') method. But unfortunately, my input does not have a value attribute.

I am curious if there is a way to validate the content of the ng-model, but so far, I have not found a solution.

If you have any suggestions on how I could successfully run this test, I would greatly appreciate your insight.

Answer №1

Typically, using `getAttribute('value')` should do the trick. `getText()` isn't effective in this case as input elements lack inner text.

expect(element(by.id('rank-max-range')).getAttribute('value')).toEqual(2);

It might be worth logging the result of `getAttribute('value')` to troubleshoot further.

Answer №2

When attempting to retrieve the text with <code>getText()
, it may not work as expected when dealing with an input element, as the value is typically stored within the value attribute once it has been set. In the provided HTML snippet, the absence of the value attribute is due to this being raw source HTML before rendering and setting the value. To properly access the value in this scenario, you should use getAttribute('value'):

var numberInput = element(by.id('rank-max-range'));
numberInput.sendKeys("2");
expect(numberInput.getAttribute('value')).toEqual("2");

To visually confirm that the value is indeed being set, you can output the content by examining the innerHTML property in the console:

numberInput.sendKeys("2");
numberInput.getInnerHtml().then(function (html) {
    console.log(html);
});

If you're interested in verifying the contents of the ng-model, there is a method available for inspection.

You can utilize the evaluate() function to examine the model's value:

expect(numberInput.evaluate('selectedItem.range.max')).toEqual(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

Using ngFor and ngModel: What is the best way to add values to the beginning of the array I am looping through?

I am facing an issue with my array of elements in which users can edit, add, and delete complete array elements. Everything works smoothly until I try to add a value to the beginning of the array using the unshift method. Below is a test that showcases th ...

Dynamic ASP.NET script file

As someone who is brand new to ASP.NET, I am facing a challenge with a JavaScript file where I want to input values dynamically upon page load. In my attempt to test this feature, I used the following code: $ ( function () { alert(' ...

How to update the selected autocomplete item in Vue using programming techniques?

Although I am still learning Vue, consider the following scenario: <v-autocomplete v-model="defaultUser" :hint="`User: ${defaultUser.username}`" :items="users" :item-text="item =>`${item.firstName} - $ ...

How can I attach an event listener to an element that is added dynamically with jQuery?

I added a new div element dynamically using jQuery's on method. However, I'm having trouble getting a listener to work for the newly created element. HTML <input class="123" type="button" value="button" /> <input class="123" type="butt ...

It appears that Three.js MeshPhongMaterial is not influenced by any light sources other than ambient lighting

After experimenting with a function that graphs a pair of arrays as a double-sided surface in threejs, I realized I was facing some issues. Despite my experience using threejs for a few weeks, custom shapes seemed to complicate things. I scoured the intern ...

arrow function implemented in a React hook for handling onClick event

From my understanding, placing an arrow function in the JSX creates a new reference of a new function each time it is triggered. For example: <p onClick={() => handleClick() /> In older versions of React with classes, we could do this: <p onCl ...

The use of jQuery addClass does not produce any changes

I am trying to modify a single attribute in my class using a JavaScript function. .msg_archivedropdown:before { content:""; display: block; position:absolute; width:0; height:0; left:-7px; top:0px; border-top: 10px solid tr ...

The functionality of displaying a loading image when the Upload button is clicked has encountered a glitch and

I am looking to add a feature that displays a loader when a user uploads a file by clicking on a button. The loader should be visible while the data is being processed in the background. To achieve this, I have included a div as shown below: <div id=" ...

How can I create an accordion panel that can be toggled using

I've been working on creating an accordion panel using CSS with a touch of JavaScript. Everything seems to be functioning correctly except for the toggling of the panels. Essentially, when one panel is clicked, I want all other open panels to close. ...

Using JavaScript to compare two dates results in no output

I am trying to compare dates in JavaScript using the following format: Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time) Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time) var ExpiryDate = userAccount.ExpiryDate(); var datetoday = new Date(); ...

Is there a way to ensure that res.render() is only triggered after the steps mentioned above have been completed?

My goal is to ensure that the completion of the for loop happens before the execution of the res.render() function synchronously. Currently, the res.render() function is running before the for loop is completed. I have tried using a setTimeout function t ...

Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself: center: function() { var top=Math.max($window.height() - $modal.outerHeight(),0) / 2; var left=Math.max($window.width() - $modal.outerWidth(),0) / 2; $modal.css({ ...

Is there a way to retrieve the value of an element by clicking on its corresponding button using MUI framework?

I am working on a dashboard feature where I need to update the status by clicking on it. However, I am facing an issue with changing the state value upon clicking. Here is my component: <MenuItem> <Button fullWidt ...

The ng-repeat directive fails to automatically refresh itself when a new item is added via a dialog box

As a beginner in AngularJs, I am currently working on CRUD Operations using $http and ASP.NET MVC. Here is what I have done so far: I utilize ng-repeat to display a list of "Terms". There is a button that triggers a dialog box for inserting new "Ter ...

Adorning a function with a technique that implements $http

I am attempting to enhance a service using a different method. However, the issue I'm facing is that this method utilizes $http, which cannot be injected into the angular.config block because it has not been initialized yet. I considered using $injec ...

Is it possible to execute TestCafe tests using TypeScript page objects that have not been utilized?

While working with TestCafe, I am implementing tests using the Page Objects pattern. I have already written some page objects in advance, even before their actual usage, as I am familiar with the page and know what to anticipate. However, when attempting ...

Is there a way for me to retrieve and save the user identification number from the URL during a Selenium test?

As I work on creating Selenium tests using the Selenium IDE to ensure the smooth registration process of my Drupal site, I am faced with a challenge due to heavy reliance on the rules module. Specifically, some of the tests revolve around user account reg ...

collecting JavaScript objects directed to console.log

I attempted capturing the JavaScript console log using the method mentioned here on Stack Overflow. However, I am facing difficulty in retrieving the object along with a text message. Below is the code snippet that I have been working with: var obj={resul ...

Using local variables from an external HTML file within an AngularJS directive template

Just making sure I am wording my question correctly, but I have not been able to find any information on this specific topic. Imagine I have an AngularJS directive that looks something like this: angular.module( 'example', [] ).directive( ...

Child emitting to parent triggers an endless loop with a basic counter increment

I am having an issue with a fill in the blank question. I need to emit a signal back to the parent component each time an input is correct, so that when all inputs are correct, I can display a success message. My approach was to use a simple counter to in ...