Selenium WebDriver is a freely available and open-source library designed for automating the testing of web applications. I assume that you possess some knowledge of Selenium WebDriver, considering your choice to explore this tutorial.
Initially created in 2004 by Jason Huggins, Selenium gained additional development from his ThoughtWorks colleagues. It provides support for all major browsers, allowing tests to be written in various programming languages, and it is compatible with Windows, Linux, and Macintosh platforms.
Selenium 2, which incorporates the WebDriver test framework led by Simon Stewart at Google (now at Facebook), was released in July 2011. Throughout this book, I use the terms Selenium, WebDriver, and Selenium WebDriver interchangeably.
Selenium offers language bindings for different programming languages like Java, C#, Python, and Ruby. In this book, all examples are presented using Selenium with C# binding. The usage of Selenium in different language bindings is quite similar, making it easy to apply knowledge from one language to another. The subsequent examples include a simple Selenium test script in four different language bindings: C#, Java, Python, and Ruby.
Selenium Language Bindings
Selenium tests can be authored in various programming languages, including Java, C#, Python, and Ruby (considered the core languages). The examples provided in this book exclusively utilize Selenium with C# binding.
As demonstrated in the upcoming examples, the utilization of Selenium across different language bindings shares significant similarities. Once you become proficient in one language binding, transitioning to others becomes quite straightforward. Explore a straightforward Selenium test script in four distinct language bindings: C#, Java, Python, and Ruby.
C#
using System; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support.UI; class GoogleSearch { static void Main() { // Create a new instance of the driver // Notice that the remainder of the code relies on the interface, // not the implementation. IWebDriver driver = new FirefoxDriver();// And now use this to visit Google driver.Navigate().GoToUrl("https://www.google.com"); // Find the text input element by its name IWebElement query = driver.FindElement(By.Name("q")); // Enter something to search for query.SendKeys("Hello Selenium WebDriver!"); // Submit the form based on an element in the form query.Submit(); // Check the title of the page Console.WriteLine(driver.Title); } }
Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class GoogleSearch { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Hello Selenium WebDriver!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); } }
Python
from selenium import webdriver driver = webdriver.Firefox() driver.get("http://www.google.com") elem = driver.find_element_by_name("q") elem.send_keys("Hello WebDriver!") elem.submit() print(driver.title)
Ruby
require "selenium-webdriver" driver = Selenium::WebDriver.for :firefox driver.navigate.to "http://www.google.com" element = driver.find_element(:name, 'q') element.send_keys "Hello Selenium WebDriver!" element.submit puts driver.title