Test Automation - How to configure ID to avoid dynamic parts

I’ve searched here and I’ve found just one answer regarding the issues regarding test automation and the dynamic nature of ID that are generated by Select2.
AFAIK at my company, we are using Select2 integrated with Bootstrap and for some Select2 uses we have got same findings done here -> https://medium.com/@kcmueller.

I’m preparing a webinar regarding test automation using TestCafe & TestCafe Studio and when trying to use this -> Select2 Github ChangeLog Page

as a playground, I’ve seen that on select with option groups id are created in what seems a random/dynamic way, making impossible any kind of automation that does not depends on finding/clicking things by position.

In addition, I’m not able to find documentation that explains how the ID of the main select2 element is generated. In Your page id is s2id_e1 for a plain select with id e1.

Then I’m very confused, because I’m not able to find documentation that will allow me to understand what is the algorithm used, if algorithm changes according the select characteristics: single selection, multiple selection, option groups.

Hope my explanation is clear.
Any hint?
Any documentation?
Any option other to develop some kind of adapter, as I’ve read in the only post I’ve found regarding testautomation?

Thanks a lot

here the link to the post in this forum: https://forums.select2.org/t/how-do-i-change-the-id-attribute

What are you trying to validate with your automated tests? I am trying to determine if there is another way to achieve the same validation without having to rely on the dynamically generated ID values that Select2 creates.

Thanks for your interest.
IMHO it’s not what I trying to validate, but I need to create the Page Object Model to be able to manage the Select2, and it seems that there is no way to be sure of how the ID is created.
I’m working on preparing a talk on Test Automation using Test Cafè for the Into The Box 2020 event and I’ve done some tests using the Gitlab user preferences pages to change the user language, and the way I need to interact with the select2 is different to the way I need to use to interact with sites like
automotix.net or mazdaus.com

It’s clear that from the point of view of an application developer that wants to use the Select2, the logic behind the building of the ID can be not important or interesting, but when you work on creating Page Object Models for test automation, it will be very useful to have this knowledge, and other information like the whole set of elements that are created by the Select2 (classes, containers, etc).

Hope my needs are now a little bit clear.

You are correct that the ID values generated by the Select2 plugin are not meaningful by themselves; they’re simply unique opaque values that Select2 tracks behind the scenes to coordinate the Select2 widget UI with the underlying HTML <select> element.

I am not familiar with the Page Object Model (do you mean Document Object Model?). If you need to generate a particular sequence of ID values for a Select2 widget you are instantiating within a test case, you should be able to specify data you want to populate the Select2 when you instantiate the Select2 in your test page. You can then specify the ID values your automated tests will be looking for. Look at the Select2 data format documentation for details.

As far as understanding the whole set of elements that are created by Select2, they are not officially documented but it’s easy to inspect an instantiated Select2 in a web page to see all of the created elements.

But again, I question the need for your automated tests to know about most of those elements; your should be able to assume that the Select2 itself works correctly. If you need to operate the Select2 to simulate specific user actions (selecting particular values and validating how the application responds to those selections), your test script can do that programmatically using the Select2 API without having to know the internal structure of the Select2 control itself.

If I’ve misunderstood what you need to do within your automated test, please let me know. I have experience setting up automated UI testing with Karma/Jasmine, and I’ve written automated tests in that framework that exercise Select2 widgets (albeit wrapped in an AngularJS wrapper component). So I know it is possible to write automated UI tests that work with Select2, but I’m not familiar with the testing framework you’re using, so I don’t know exactly what you would need to do.

Hi, thanks again for your collaboration.
I suppose I was not clear enough when talking about ID. I would like to understand what is the rule used to generate the ID of the Main Select2 container.
I need to do the test in the way as much as similar ad done by the user, then I do not like a lot the idea of managing the Select2 via the API. I’ve considered it but I still trying to understand how to access the API from test cafe.
Unfortunately from my point of view there is a hole in the Select2 documentation in the topic of the Select2 ID generation.

I’m going to update you if I found more information


The method used to generate the ID is not documented because it’s not something end users need to be concerned with. However, the source code is available on the GitHub repo if you want to see how it’s actually generated. I would caution against this, however. Since it is undocumented, it is subject to change, which would break your tests. Your test should not depend on the internals of the Select2 code. That’s why I recommended you use the Select2 API. The API allows you to select items exactly as an end user would do (i.e., by their text values).

I think you’re making a lot of extra work for yourself if you want to dive into the Select2 code to understand how the IDs are generated, and in the end it might turn out that they’re purely random (i.e., the generated ID values have no algorithmic relationship to the item values in the dropdown). But if you want to undertake that effort, then go for it, and good luck.

Thanks for your help. Surely using time for digging undocumented things is not a wise thing to do, that’s why I’would like some minor details by the Select2 author.