
Replacing lines of code using RegEx.
“I call this a tip from heaven” by Ana :)

One of these days, we we’re doing changes to a legacy project that was built long ago, a project that no one updated or maintained in a few years. You can imagine the amount of changes that needed to be done, the structure of the project, folders, data, drivers, locators, waits …
There was a need to update the structure and lines of code, we certainly did not want to do this one line at the time, as we are talking about a project with thousands of lines.
There was a change / request to replace lines of code like these:
...
size.isDisplayed();
size1.isDisplayed();
size2.isDisplayed();
size3.isDisplayed();
size4.isDisplayed();
size5.isDisplayed();
lifexp.isDisplayed();
lifexp1.isDisplayed();
lifexp2.isDisplayed();
lifexp3.isDisplayed();
lifexp4.isDisplayed();
lifexp5.isDisplayed();
charac.isDisplayed();
charac1.isDisplayed();
charac2.isDisplayed();
charac3.isDisplayed();
charac4.isDisplayed();
charac5.isDisplayed();
charac6.isDisplayed();
...
Where you want to use a wait for the element to be displayed to avoid flaky tests. So what do you? how can you accomplish this?
Depending on your OS and IDE (in this case we’re using a Mac) press Cmd + Shift + R( for IntelliJ) or Cmd + Shift + H(for VSCode). This will open the magical Replace in Path window:

We can replace functionality and code structure with regular expressions to automatically make these changes, instead of going one line, file, or module at the time.
In the “Find” field, enter the following regular expression:
(\w+)\.isDisplayed\(\);
In the “Replace” field, enter the replacement expression:
wait.until(ExpectedConditions.visibilityOf($1));
This regular expression will find all occurrences of a method call followed by .isDisplayed(); and replace them with wait.until(ExpectedConditions.visibilityOf(element));, where element is the variable representing the WebElement. For instance:

If the preview looks good, click on “Replace All” to perform the replacements at the project level. The result will be a massive change on all classes that will look something like this:
...
wait.until(ExpectedConditions.visibilityOf(size));
wait.until(ExpectedConditions.visibilityOf(size1));
wait.until(ExpectedConditions.visibilityOf(size2));
wait.until(ExpectedConditions.visibilityOf(size3));
wait.until(ExpectedConditions.visibilityOf(size4));
wait.until(ExpectedConditions.visibilityOf(size5));
wait.until(ExpectedConditions.visibilityOf(lifexp));
wait.until(ExpectedConditions.visibilityOf(lifexp1));
wait.until(ExpectedConditions.visibilityOf(lifexp2));
wait.until(ExpectedConditions.visibilityOf(lifexp3));
wait.until(ExpectedConditions.visibilityOf(lifexp4));
wait.until(ExpectedConditions.visibilityOf(lifexp5));
wait.until(ExpectedConditions.visibilityOf(charac));
...
You can do the same for any other condition, for instance, changing how you are clicking on elements, in the sense of waiting for an element to be ready for a click.
In the “Find” field, enter:
(\w+)\.click\(\);
In the “Replace” field, enter the replacement expression:
wait.until(ExpectedConditions.elementToBeClickable($1)).click();
This is replacing 21 files / 951 entries, in just one single click!

You can also get rid of syntax errors or content you do not need by leaving the “Replace” case empty, for instance:


Another massive change that can be done with one single click ^
This is a strait-forward and small article, but I hope this may be useful to you, as it was for us :)
Remember, the more you share the more you learn. Keep going!