title: nodejs selenium how to avoid driver.sleep before inputing new value
body: <p>I am trying not to introduce indeterminacy in my test code, by avoiding the use of driver.sleep (as illustrated by this blog: <a href="https://medium.freecodecamp.org/how-to-write-reliable-browser-tests-using-selenium-and-node-js-c3fdafdca2a9" rel="nofollow noreferrer">https://medium.freecodecamp.org/how-to-write-reliable-browser-tests-using-selenium-and-node-js-c3fdafdca2a9</a>).</p>
  
  <p>Using a test runner, I am repeatedly running a test case on a login form, repeatedly for about 10 times (without calling <code>driver.quit()</code>), something like below:</p>
  
  <pre><code>async function doLogin(driver, testcase) {
  
    try {
      let username = testcase.input.username;
      let password = testcase.input.password;
  
      let loginInput = await driver.wait(until.elementLocated(By.id('loginId')),2000);
      loginInput = await driver.wait(until.elementIsVisible(loginInput), 20000);
      await loginInput.clear();
      await driver.sleep(1000);
      logger.info("Entering username:%s", username);
      await loginInput.sendKeys(username);
  
      logger.info("Entering password:%s", password);
      let passwdInput = await driver.wait(until.elementLocated(By.id('password')),2000);
      passwdInput = await driver.wait(until.elementIsVisible(passwdInput), 20000);
      await passwdInput.clear();
      await driver.sleep(1000);
      await passwdInput.sendKeys(password, Key.RETURN) ;
    } catch(e) {
      logger.error("Error in doLogin:%s", e.toString());
    }
  }
  </code></pre>
  
  <p>Calling code...</p>
  
  <pre><code>testLogin: async function(testcase) {
      try {
        logger.info("Loading page: %s...", URL);
        await driver.get(URL);
        await doLogin(driver, testcase);
  </code></pre>
  
  <p>The above code is trying to clear the input/password fields, enter new data and click the login button.  Though I have taken care to wait until element located, and until element visible, it seems I am unable to reliably clear the input fields and enter data.  As a result, I see random tests (around 50%) failing due to incorrect username and password.</p>
  
  <p>The inconsistency is overcome by adding a <code>driver.sleep()</code>, after the <code>element.clear()</code>.  So, I still have to depend on <code>driver.sleep()</code>.  </p>
  
  <p>Is there a reliable way to do the automation, without resorting to <code>driver.sleep()</code>?</p>
  
  <p>PS: I am using chrome driver with selenium webdriver 4.0.0-alpha.1</p>