Skip to main content

Best Practices

Best Practices

The Best Practices section describes techniques that considered a standard way of doing things and allows to maintain quality of the code developed.

Avoid using of Thread.sleep

You should always avoid using of Thread.sleep() method during the RPA implementation because it stops executing the thread during specified time. 

Instead of that, you should use driver wait object when you need to wait for some UI element to be appeared on the screen. Driver wait is flexible tool for control of waiting, ignoring exception, control of pollling interval and etc.

E.g.:

Instead of:

Bad practice
driver.get("...");
Thread.sleep(2000);
driver.find(By.id("id1")).click();
Thread.sleep(2000);

You should do:

Best practice
driver.get("...");
driver.waitForElement(BrowserSearch.id("id1")).click();

Set default driver implicitly wait to 0 seconds

We recommend to always set driver implicitly wait to 0 seconds. As per the documentation of Explicit and Implicit Waits it is clearly mentioned that:

Warning

Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the Driver system. That means they're "baked in" to chromedriver.exe extension that gets installed. Explicit waits are implemented exclusively in the "local" language bindings.

Best practice
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id1")));

Use Page Object design pattern

As common development practice it is recommended to separate business logic from the logic of page of some UI application. In case of RPA, Page Object design pattern is a great fit. Detailed information is available under Page Object Design and Application page.