DEV Community

loading...

Discussion on: Writing Test Cases using Python with Selenium

Collapse
endtest_io profile image
endtest

XPaths do have an advantage, you can use them to locate an element based on the text inside.

//*[contains(text(),'Hello')]

It's great to use both CSS Selectors and XPaths.

And it's also very helpful to learn how to write your own XPaths and CSS Selectors by using attributes which make an element unique.

The XPaths and CSS Selectors that you get from the Chrome Developer Console.

Let's take a look at this example:

<html>
   <body>
      <div class="login-form">
         <input id="mat-input-6" type="text" placeholder="Email">
         <input id="mat-input-9" type="password" placeholder="Password">
      </div>
   </body>
</html>

We can easily spot that the IDs are dynamic, so we have to avoid them.

The basic CSS Selector for the Email input would look like this:

body > div > input[type=text]:nth-child(1)

But we can easily construct something more reliable:

input[placeholder="Email"]

And using Cypress or any other JavaScript-based UI testing library is a very bad idea.

Since those libraries have severe limitations and do not mimic real user behavior.

For example, a JavaScript-based library for testing the UI can easily click on an element which is completely covered by another element.

A real user and a Selenium test cannot do that.

A JavaScript library for testing the UI cannot perform file uploads, cannot manage iframes and cannot manage multiple browser tabs.

Use Selenium or some other Selenium-based solution. That's the way to go.

Thread Thread
steelwolf180 profile image
Max Ong Zong Bao Author

Interesting perspective as I had used protractor and did find that it was quite limited in features to test UI which I usually fall back to using selenium due to me playing around with it in the past by doing data scrapping projects.