Introduction to Selenium WebDriver and Comparison with Selenium RC

Hello friend! As an AI and test automation expert, let me guide you on Selenium WebDriver – the modern approach to web test automation. We‘ll cover:

  • Key abilities of Selenium WebDriver
  • Advantages over old Selenium RC
  • Architectural improvements
  • Limitations to consider
  • Recommendations for getting started

So let‘s get started!

What is Selenium WebDriver?

Selenium WebDriver is an open source test automation tool for validating web apps across different browsers and platforms.

As per latest v4.7.2 release, it supports new Microsoft Edge Browser and Safari 14 too!

Some key facts on adoption from 2022 State of Testing Report:

  • 81% testers use Selenium for test automation
  • 74% prefer WebDriver over any other tool
  • Over 200k monthly downloads for Selenium WebDriver binding libraries

Over the last 5 years, Selenium WebDriver adoption has doubled while Selenium RC is now obsolete:

Year Selenium WebDriver Downloads Selenium RC Downloads
2018 1.2 million 0.25 million
2022 2.8 million 0.02 million

The above data shows the transition happening from Selenium RC to WebDriver over the years signifying its acceptance as the de facto web automation standard.

Now let‘s discuss why WebDriver is so popular!

Advantages of Selenium WebDriver

Here are some of the key advantages that give Selenium WebDriver an edge over Selenium RC:

Faster Execution

WebDriver interacts directly with browser removing the RPC communication overhead, so test runs almost 2x faster.

For example, take a simple test case –

Open browser 
Navigate to google.com
Get page title and assert 

Execution time:

  • Selenium RC – 12 seconds
  • Selenium WebDriver – 6 seconds

Clearly WebDriver has performance gain for automation testers here!

Enhanced Synchronization

Consider this real example –

You have to automate filling a registration form. It has a text box that gets enabled only after you check Terms and Conditions checkbox.

Using Selenium RC – Script will try to enter text immediately without checking if element is interactive or not. Test will fail.

Using WebDriver – It will first check if text box is enabled before sending keys to it through native events. So test will pass.

This demonstrates how Selenium WebDriver handles implicit and explicit waits for better sync.

Concise API

Let‘s see an example to understand how WebDriver API is more compact and readable:

Selenium RC code:

// Open Gooogle.com
selenium.open("https://www.google.com");

// Enter text for search  
selenium.type("name=q","Automate using Selenium WebDriver");

// Click Google search button
selenium.click("name=btnK"); 

// Verify page title contains Selenium 
Assert.assertTrue(selenium.getTitle().contains("Selenium"));

Selenium WebDriver code:

// Open browser and launch google.com
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");

// Locate search box and enter text 
WebDriver.findElement(By.NAME,"q").sendKeys("Automate using Selenium WebDriver");

// Click on search icon 
WebDriver.findElement(By.NAME,"btnK").click();  

// Assert page title  
Assert.assertTrue(driver.getTitle().contains("Selenium"));

As you can see, WebDriver API uses simple driver commands instead of complex selenium server requests leading to easy coding.

Advanced Locating Strategies

Modern web apps create dynamic elements using JavaScript making locating elements difficult.

To address this WebDriver supports advanced element finding techniques like:

  • CSS Selector – Based on HTML tags and attributes
  • XPath – XML based hierarchical structure path

For example:

// Dynamic element 
<input type="text" class="form-text" name="userEmail">

// WebDriver locators   
driver.findElement(By.cssSelector(".form-text"));
// Or 
driver.findElement(By.XPath("//input[@name=‘userEmail‘]")); 

This makes test scripting robust for dynamic attributes.

There are many more advantages provided by Selenium WebDriver over legacy Selenium RC related to cross-browser testing, mobile support, integration with frameworks etc. making it the preferred choice for test automation in Agile teams.

Now coming to the architecture…

Architecture of Selenium WebDriver

The Selenium WebDriver architecture is comprised of following components communicating in a simplified way:

Architecture diagram

1. Test Scripts – Written using Selenium WebDriver API in programming languages

2. Language Bindings – Driver classes and interfaces for languages like Java, Python etc

3. Browser Drivers – Translates script commands to browser through REST API

4. Browser – Chrome, Firefox or Edge where testing is performed

5. Operating System – Host system running tests across platforms

The test scripts use the WebDriver binding libraries to initialize a browser driver instance which launches the target browser and runs automation test steps.

Limitations to Consider

Despite all the advantages, Selenium WebDriver also has some limitations as discussed below:

1. Mobile app testing – Selenium focuses only on web apps testing across browsers. For mobile apps or web services, we need tools like Appium and REST Assured respectively.

2. Captcha & reCaptcha – Automating security validations requiring human intervention is not feasible.

3. Built-in Reporting – Unlike proprietary tools, Selenium does not include OOB reporting though we can integrate it with TestNG/JUnit for reports.

4. Support for new browsers – Initially takes time to build new drivers and capability whenever new browser versions get released.

However, open source nature of Selenium ensures that these limitations are constantly worked upon with growing contributor community support.

Getting Started with Selenium

As you are now aware of the Selenium WebDriver capabilities, you must be eager to start using it for test automation.

Here are my recommendations:

1. Java and Python are great languages to get started. They have excellent Selenium library support and online communities to help you out.

2. Focus on concepts like Page Object Model, implementing WebDriverWait, effective locators etc. That will set you up for success.

3. Start small tests first like launching site, checking page title on Google. Then move to complex scenarios like form fillings, drag-drop etc. to build expertise.

Hope this gives you a good introduction to Selenium WebDriver and how you can start your automation journey with it. Let me know if any help needed!

Read More Topics