Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying elements on a ReactJS webpage using Selenium, is their a better way?

I am trying to write web driven tests using Selenium on a webpage built with reactjs. I am able to grab my elements by xpath however this is not ideal and very limiting.

  1. If any changes happen to the website the xpath will not work.
  2. all the class names are identical with no other attributes to grab, as well the class names change because they are dynamically built by react.
  3. There are no other attributes to use.
  4. Cant use the CSSSelector because the css name is also built by react.

My main problems keep popping up when I want a specific element, like one I just built or how to manipulate a specific one in a list.

I keep getting stumped on the same problem for each test I try to write, no unique identifiers. I am not familiar with reactJs but is this a common problem with Selenium?

like image 745
Jesse Bell Avatar asked May 03 '18 17:05

Jesse Bell


People also ask

Which is the most efficient way of identifying an element in Selenium?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM. Since IDs are unique for each element on the page, it is considered the fastest and safest method to locate elements.

Is Selenium good for React JS?

js is Easier Than You Think. End to end (e2e) testing is the process of executing a tests scenario against a real browser to test the entire stack of a multi-tier application. The best tool for the job is Selenium, which can pilot any web browser using a standardized API, and is actively maintained.

Which is faster XPath or ID?

Technically speaking, By.ID() is the faster technique because at its root, the call goes down to document. getElementById(), which is optimized by most browsers. But, finding elements using XPath is better for locating elements having complex selectors, and is no doubt the most flexible selection strategy.

Which locator is best in order to automate?

ID Locator IDs are the safest, fastest locator option and should always be your first choice. ID's are supposed to be unique to each element.

What is a selenium webelement?

The Selenium WebElement does just that. Its interface allows you to create variables of the WebElement type, which represent HTML elements on a web page, and also provides methods to interact with these elements. Some of these elements include:

What is selenium and react selenium?

Selenium is an open-source functional testing tool often leveraged to test web applications on multiple browsers and operating systems (platforms). React is an open-source, front-end, JavaScript library often used to build user interfaces or UI elements. It is maintained by Facebook and a community of individual developers and organizations.

What can you learn from selenium?

The majority of most people’s Selenium code involves working with web elements. Ways to identify one or more specific elements in the DOM. Locating the elements based on the provided locator values. A high-level instruction set for manipulating form controls. What you can learn about an element.

What are the methods provided by the webelement class?

The following are some of the methods provided by the WebElement class to interact with textboxes and textareas elements during the execution of a script: boolean isEnabled (): Indicates if an element is enabled or not. java.lang.String getAttribute (java.lang.String name): Gets the current value of a given attribute of an element.


Video Answer


1 Answers

It's common practice to use a data-test attribute on your HTML elements to assist in browser-based testing;, whether it's static or rendered by a JS framework; I would recommend that you request that the developers include these attributes throughout the codebase in order to allow you to easily target the elements you want to test without worrying about class names or id attributes changing.

like image 141
Steakeye Avatar answered Nov 15 '22 21:11

Steakeye