Skip to main content

Top 5 Selenium Webdriver Coding Tips to Improve Quality



SeleniumWebdriver is a powerful automation tool for testing web applications. It’s an open source library bundled with a rich set of features. Using it for testing a website is just like simulating the actions of a real user.

All these abilities together make it a perfect tool for automation. But you need proper guidance to get the best out of it. In this blog post, we’ve assembled some of the best and Top 5 Selenium Webdriver coding tips for software testers.




Tip-1: Best Method To Create Webdriver Instance.

It’s one of the most common Selenium Webdriver coding tips which you can’t avoid to use.

1.1- Use the <Factory> design pattern to create objects based on browser type.

1.2- Extend the below code or use it as is in your projects.

public class DriverFactory {

private WebDriver driver = null;

    public static WebDriver getBrowser(String browserType) {
        if (driver == null) {
            if (browserType.equals("Firefox"))

            {
                driver = new FirefoxDriver();

            } else if (browserType.equals("Chrome"))

            {
                driver = new ChromeDriver();

            } else if (browserType.equals("IE"))

            {
                driver = new InternetExplorerDriver();

            }
        }
        return driver;
    }


Tip-2: Simple Method To Check If An Element Exists.

2.1- You should use <findElements> instead of <findElement>.

2.2- <findElements> returns an empty list when it doesn’t find any matching elements.

2.3- You can use the code given below to check for the presence of an element.

Boolean isItemPresent = driver.findElements(By.testLocator).size() > 0

Tip-3: Avoid Exception While Checking An Element Exists.

3.1- The code in the previous tip may lead to <NoSuchElementException>. Also, it could be a bit slower in some cases.

3.2- Webdriver has a Fluent wait feature. We can use it to check the element.

3.3- It gives the ability to ignore any exception and allows to test the <WebElement>. 

Let’s see how to achieve what’s said here.


/* 1- Test if an element exists on the page.
2- Ignore the no element found exception.
*/
By element = By.xpath(".//*[@id='demo']/p");
Wait < WebDriver > wait = new FluentWait < > (driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions
.presenceOfElementLocated(element));


Tip-4: Wait For A Page With JavaScript(JS) To Load.

4.1- It’s quite common working with pages bloated with JavaScript. The challenge is how to make sure the page has finished loading.

4.2- Here, we are giving a solution that works. This code will help you setting

Latest Updated Selenium Topics 


wait.until(new Predicate < WebDriver > () {
@Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
});

Tip-5: Take A Screenshot Using Webdriver.

5.1- It’s easy to capture a screenshot of any error using Webdriver.

5.2- Use the below Webdriver code. It saves the screenshot to the current project directory.


WebDriver driver = new FirefoxDriver();
driver.get("http://www.techBeamers.com/");
// Store the screenshot in current project dir.
String screenShot = System.getProperty("user.dir") + "\\screenShot.png";
// Call Webdriver to click the screenshot.
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// Save the screenshot.
FileUtils.copyFile(scrFile, new File(screenShot));

Footnote:

I wish the above post would have been able to hit the right chord with you. I’ve tried hard to touch upon all basic, advanced and premium Selenium Webdriver coding tips.

I believe these quickies would not only solve your problems but help in upgrading your Selenium Webdriver coding skills.


So, help me reach this post further to benefit every other individual who’s striving to learn automation. Don’t just leave without a like, share, or comment.

Selenium Tutorials for Beginners


Comments

Popular posts from this blog

Top 10 Selenium Tester Responsibilities

Roles n Responsibilities of any automation tester depends upon many factors like team size, budget of project, availability of resources etc it may vary every now n then. But I have tried to highlight them here. Obviously, test automation is not just programming. If you do not have enough skills (yet) to automate focus on other things you can contribute on this Top 10 Selenium Tester Responsibilities Top 10 Selenium Tester Responsibilities: Understanding  Selenium Test Life Cycle Knowledge in Selenium Tools such as Selenium IDE, Selenium RC, Selenium WebDriver & Selenium Grid Good Knowledge in Understanding Selenium Commands – Selenese Idea in Test Case Prioritization Better knowledge in  Designing the Test Data Properly Check the remaining   5 Selenium Tester Responsibilities  

Installation of Selenium IDE - Step by Step Guide for Beginners

Installation of Selenium IDE – Thanks for the reviews for my previous posts, as per the request I had received from beginners here I’ll explain about the Installation of Selenium IDE, Please check the previous tutorials about Basics of Selenium and Top 5 FAQs in Selenium and Components of Selenium  before go forward. Here is the step by step guide to Install Selenium IDE. What you need? The following are the stuffs, you need to install Selenium ·         An Internet connection ·         Mozilla Firefox Step 1: Using your Mozilla Firefox browser open this official Selenium IDE download link: http://www.seleniumhq.org/download/ Step 2: ·         Click on “Allow” if security alert displays while downloading.        Restart the browser once all the files are Installed Successfully          ...

Selenium vs QTP - Comparison between Selenium and QTP

Selenium vs QTP – Top 5 Comparison between Selenium and QTP            Selenium vs QTP which is the better automation framework, which one to choose is the question which strikes mostly all software testers. Here are the comparison highlights between Selenium and QTP. Here is the solution; Comparison between Selenium and QTP: Tool Download: Selenium:  It is an open source tool which is ultimately free of cost and does not requires any license. Can able to download online – Selenium Installation Guide .            QTP:  It requires license to use the tool and it is very expensive. Test Applications:            Selenium:  It is used to test web based applications.            QTP:  QTP can be used to test both web based application and client server applications. Integr...