Before delving into the issue at hand, I must mention that my experience with selenium-webdriver has mostly been within the context of wrapper/dsl frameworks (such as Capybara for Ruby) or newer automation frameworks like Cypress/Playwright.
Recently, while examining some older code, I came across what appeared to be a complex sequence of selenium commands involving finding elements and waiting. The structure resembled callback hell, which is quite different from the selenium samples I have encountered in the past. Here is a snippet from the chain of commands:
driver.sleep(1000).then(function(a) {
driver.findElement(locators.baseURL)
.sendKeys("www.widgets.com");
driver.sleep(1000).then(function(b) {
driver.findElement(locators.someKey)
.sendKeys("AVALIDKEY");
driver.sleep(1000).then(function(c) {
driver.findElement(locators.somethingElse)
.sendKeys("SOMETEXT");
driver.sleep(1000).then(function(d) {
driver.findElement(locators.anotherLocator)
.click();
driver.sleep(6000).then(function(e) {
etc......
})})})})})
This pattern continues for quite some time. Admittedly, despite my familiarity with selenium, this particular usage seems unusual to me. It's worth noting that this code dates back around 4-5 years.
My main inquiry revolves around whether this intricate coding approach is standard practice for selenium. With selenium lacking built-in auto-waiting features akin to cypress or playwright, is there room for improvement in streamlining such sequences? Could this possibly be a contributing factor to why selenium might not be as popular these days?
In scenarios where numerous commands are executed sequentially, how does one effectively manage element waiting times? Does selenium offer support for async/await methods?
It is my understanding that selenium was designed to appear synchronous while functioning asynchronously under the surface. Therefore, is this convoluted script an accurate representation of how selenium scripts "should" be formulated?
Additionally, wouldn't the cumulative sleep durations result in lengthy test execution times? Or does selenium optimize waiting periods in some way?