DEV Community

AVinoth37
AVinoth37

Posted on

Automation Testing

The difference between Selenium IDE, Selenium WebDriver, and Selenium Grid:

Selenium IDE (Integrated Development Environment):
• It is like a recorder/player for web actions.
• It’s a browser plugin that lets you record your integrations with a website (clicks, typing etc) and then playback those actions as a test script.
• It is great for beginners or quick tests as it doesn’t require coding.
• Limited for complex scenarios or customizing test behaviour.

Selenium WebDriver:
• This is the core functionality of Selenium.
• It’s a collection of libraries (one for each programming language like Python, Java etc.) that allow you to write code to control a web browser.
• It offers more power and flexibility than IDE. You can write complex test logic, handle different scenarios and interact with web elements in detail.
• It requires knowledge of a programming language.

Selenium Grid:
• This is for running tests in parallel across multiple environments.
• Imagine testing your website on different browsers and operating systems simultaneously. Grid acts as a hub that distributes your tests to registered “nodes” (machines with browsers).
• Useful for large-scale testing or reducing testing time.
• Adds some complexity to your testing setup.

Selenium script in Java to open Google and search for "Selenium Browser Driver.":

Script:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GoogleSearch {

public static void main(String[] args) {

 WebDriver driver = new ChromeDriver();

driver.get("https://www.google.com/");

WebElement searchBar = driver.findElement(By.xpath("//textarea[@id='APjFqb']"));


searchBar.sendKeys("Selenium Browser Driver");
Enter fullscreen mode Exit fullscreen mode

}
}

Output:

Image description

What is Selenium? How it is useful in Automation Testing?

Selenium is a free, open-source suite of tools specifically designed for automation web application testing. It allows you to control a web browser through code, simulating user actions and verifying the application’s behaviour.

Here’s how Selenium is useful in Automation Testing:
Increased Efficiency: Selenium automates repetitive tasks involved in testing, saving time and effort compared to manual testing.
Improved Accuracy: Automated tests can be more consistent and reliable than manual testing, reducing the chance of human error.
Faster Feedback: Automation allows you to run tests frequently, catching bugs early in the development cycle.
Cross-Browser Testing: Selenium can run tests on different web browsers, ensuring consistent functionality across platforms.
Regression Testing: Automated tests can be easily re-run to ensure new features haven't broken existing functionality.
Scalability: Selenium scripts can be easily integrated into continuous integration (CI) pipelines for large-scale testing.

Browser drivers used in Selenium:
ChromeDriver: This driver is used to automate interactions with Google Chrome and Chromium-based browsers. It's widely used due to Chrome's popularity and frequent updates.
GeckoDriver (FirefoxDriver): This driver is used to automate interactions with Mozilla Firefox. It comes bundled with the Selenium WebDriver jar file for Java by default.
Microsoft Edge WebDriver: This driver is used to automate interactions with Microsoft Edge. It's necessary for automating Edge with Selenium.
SafariDriver: This driver is used to automate interactions with Apple Safari. It's pre-installed with Safari on macOS High Sierra and later versions. However, you'll need to enable "Remote Automation" in Safari's Develop menu for it to work with Selenium.
InternetExplorerDriver (IE): This driver was used to automate interactions with Internet Explorer. However, Microsoft no longer actively supports Internet Explorer, and it's recommended to use Microsoft Edge instead.

The Steps To Create A Simple Web Driver Script:

1.Set Up Development Environment:
• Install Java Development Kit (JDK). You can download it from https://www.oracle.com/java/technologies/downloads/.
• Choose an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA. These IDEs can simplify project management and code editing.
• Download the Selenium WebDriver libraries. You can use a dependency management tool like Maven or Gradle to handle these libraries.

2. Write the Script:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SimpleWebDriverScript {

public static void main(String[] args) {

    // Create a new WebDriver instance
    WebDriver driver = new ChromeDriver();

    // Open a URL
    driver.get("https://www.google.com"); // Opening google website

    // Get the page title
    String title = driver.getTitle();
    System.out.println("Page Title: " + title);

    // Close the browser
    driver.quit();
}
Enter fullscreen mode Exit fullscreen mode

}

Explanation:
• Import necessary classes: WebDriver for browser interaction and ChromeDriver is for Chrome browser specifically. (We can also use EdgeDriver for Microsoft Edge browser, FirefoxDriver for Mozilla Firefox browser etc. as per user needs)
• Create a WebDriver instance: This creates a new instance of ChromeDriver, launching a chrome browser window.
• Navigate to a URL: Use driver.get() to open the desired website. For example: “https://www.google.com”
• Get the page title: Access the page title using driver.getTitle() and print it to the console.
• Close the browser: Use driver.quit() to close the browser window opened by the script.

3. Run the Script:
• Save the code as a .java file (e.g., SimpleWebDriverScript.java).
• Compile the code using a Java compiler (you can use the command line or your IDE's built-in compiler). The command would be javac SimpleWebDriverScript.java.
• (Optional) Setting Up Classpath with Libraries: If you're not using a dependency management tool, you'll need to include the Selenium JAR files in your classpath. You can download them from https://www.selenium.dev/downloads/ and add them to your project's build path.
• Run the compiled class using java SimpleWebDriverScript.

Top comments (0)