Angular Textbox get value using querySelector

1 Answer 76 Views
TextBox
Jaime
Top achievements
Rank 2
Iron
Iron
Jaime asked on 16 Jan 2025, 07:25 PM | edited on 16 Jan 2025, 07:43 PM

Hi,

We are writing some end to end tests for our application and we wanted to use our custom testing library which utilizes basic HTML & Javascript functionality. We use this library for all our web applications, regardless of the framework it was written in.

We are currently having issues on how to get the value of a Angular UI Kendo Textbox. We can get the element by doing document.querySelector('#myField') and returns the element...


<input _ngcontent-ng-c1188706083="" kendotextbox="" formcontrolname="my_field" type="text" id="myField" 
qa-target
="user-my-field-input" ng-reflect-name="my_field"
class
="ng-untouched ng-pristine ng-valid k-textbox k-input k-input-md k-rounded-md k-input-solid">

Doing document.querySelector('#myField').innerHTML or document.querySelector('#myField').innerText just gives a value of "".

How do I get the value that was entered in the textbox?

Thanks!

PS. This textbox is part of a form and looks like this in the template

<form [formGroup]="theForm" class="k-form">
        <fieldset class="k-form-fieldset">
            <h4 class="k-pb-4">Some Information</h4>

            <div class="name-wrapper k-pb-3">
                <kendo-formfield>
                    <kendo-label for="myField" text="My Field*"></kendo-label>
                    <input
                        kendoTextBox
                        formControlName="my_field"
                        type="text"
                        id="myField"
                        qa-target="user-my-field-input" />
                    <kendo-formerror>My field is required</kendo-formerror>
                </kendo-formfield>

1 Answer, 1 is accepted

Sort by
0
Accepted
Zornitsa
Telerik team
answered on 21 Jan 2025, 09:00 AM

Hi Jaime,

In general, all components in the Kendo UI for Angular library undergo some extensive testing with both unit and E2E tests as a way to ensure that all of them operate according to the expectations. Testing the components is an essential part of the value that we try to provide to our customers as we try to ensure that all components pass the highest quality standards.

For reference, the developer can obtain our source code to see all the available tests for our components and how they are set up:

In this line of thought, by conducting internal testing for the Kendo components, we intend to save you time by sparing the need to perform additional testing but we understand that this also depends on the particular project requirements.

Nevertheless, to address your question about obtaining the value of the TextBox using querySelector, the developer can target the input element and use the value property to retrieve its value. For example:

const input: HTMLInputElement = document.querySelector('#myField');
console.log(input.value);

Here is also a StackBlitz example demonstrating the results from the above approach:

On a side note, since I can see that the kendoTextBox directive is used on your side instead of the actual <kendo-textbox> component, I want to mention that the kendoTextBox directive is intended to provide styling options for the input elements. The <kendo-textbox> component, on the other hand, should be used for implementing fully functional inputs.

In the scenario when a TextBox component is used, the above-suggested logic for acquiring its value should be adjusted as follows:

<kendo-textbox
   id="myField"
   ...
></kendo-textbox>
const input: HTMLInputElement = document.querySelector('#myField input');
console.log(input.value);

I hope the provided information is helpful. Please let me know if any additional questions on the matter arise.

Regards,
Zornitsa
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jaime
Top achievements
Rank 2
Iron
Iron
commented on 21 Jan 2025, 03:26 PM

This is fantastic Zornitsa! This is what our QA team is looking for.

Thank you so much! Really. aprreciate it.

 

Cheers!

Jaime

Tags
TextBox
Asked by
Jaime
Top achievements
Rank 2
Iron
Iron
Answers by
Zornitsa
Telerik team
Share this question
or