Search⌘ K
AI Features

Optimization Techniques: Part 2

Explore techniques to optimize Selenium WebDriver test scripts by using ternary operators to reduce code length, relying on element IDs for multi-language tests, and implementing environment variables to dynamically manage test configurations across browsers and servers. This lesson helps you create more readable and efficient automation scripts.

Avoid if-else block

Usually, programmers write test scripts in a similar fashion to application coding. Though there is nothing with this approach but the simpler and more concise code yields easier to read test scripts, which ultimately helps in debugging. Therefore, it advised writing one line of test statements for one user operation. A good example of this would be the use of a ternary operator ? :, which cut shorts 7 lines of code statements to only 3 as demonstrated below:

Javascript (babel-node)
//7 lines
driver.findElement(By.id("notes")).getText().then(function(notesText){
if (refNo.contains("VIP")) { // Special
assert.equal("Please go upstairs", notesText)
} else {
assert.equal("", notesText)
}
});
Javascript (babel-node)
// 3 lines)
driver.findElement(By.id("notes")).getText().then(function(notesText){
assert.equal(refNo.contains("VIP") ? "Please go upstair" : "", notesText)
});

Using TernaryOperator for testing out the website in two languages:

...