Preface
Selenium is an open-source tool that is used for test automation. It is licensed under Apache License 2.0. Selenium is a suite of tools that helps in automating only web applications. This tutorial will give you an in-depth understanding of Selenium and its related tools and their usage.
Before proceeding with this tutorial, you should have a basic understanding of Java or any other object-oriented programming language. In addition, you should be well-versed with the fundamentals of testing concepts.
Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently. Let us now understand each one of the tools available in the Selenium suite and their usage.
Selenium IDE
The Selenium-IDE (Integrated Development Environment) is an easy-to-use Firefox plug-in to develop Selenium test cases. It provides a Graphical User Interface for recording user actions using Firefox which is used to learn and use Selenium, but it can only be used with Firefox browser as other browsers are not supported.
However, the recorded scripts can be converted into various programming languages supported by Selenium and the scripts can be executed on other browsers as well. The following table lists the sections that we are going to cover here.
* Download Selenium IDE
* Selenium IDE Features
* Creating Selenium IDE Tests
* Selenium IDE Script Debugging
* Inserting Verification Points
* Selenium Pattern Matching
* Selenium User Extensions
* Different Browser Execution
Environment Setup
In order to develop Selenium RC or WebDriver scripts, users have to ensure that they have the initial configuration done. Setting up the environment involves the following steps.
Download and Install Java
We need to have JDK (Java Development Kit) installed in order to work with Selenium WebDriver/Selenium.
Download and Configure Eclipse
Prepare Eclipse IDE to develop Selenium WebDriver. (More)
Configure FireBug and FirePath
To work with Selenium RC or WebDriver, we need to locate elements based on their XPath or ID or name, etc. In order to locate an element, we need tools/plugins. (More)
Configure Selenium RC
Now let us look at how to configure Selenium Remote control. We will understand how to develop scripts with Selenium RC in later chapters, however for now, we will understand just the configuration part of it.
Step 1 : Navigate to the Selenium downloads section http://www.seleniumhq.org/download/ and download Selenium Server by clicking on its version number as shown below.
Step 2 : After downloading, we need to start the Selenium Server. To do so, open command prompt and navigate to the folder where the downloaded JAR file is kept as shown below. To start the server, use the command 'java -jar <<downloaded jar name >>' and if java JDK is installed properly, you would get a success message as shown below. Now we can start writing Selenium RC scripts.
Or if your want to use Docker to launch Selenium Service, below is an example:
Configure Selenium WebDriver
Now let us look at how to configure Selenium WebDriver. We will understand how to develop scripts with Selenium WebDriver in later chapters, however for now, we will understand just the configuration part of it.
Step 1 : Navigate to the selenium downloads section http://www.seleniumhq.org/download/ and download Selenium WebDriver by clicking on its version number as shown below.
Step 2 : The downloaded file is in Zipped format and one has to unzip the contents to map it to the project folder.
Step 3 : Import the jar files into project
Remote Control
(Source) Selenium Remote Control (RC) allows us to write automated web application UI tests with the help of full power of programming languages such as Java, C#, Perl, Python and PHP to create more complex tests such as reading and writing files, querying a database, and emailing test results.
Selenium RC Architecture
Selenium RC works in such a way that the client libraries can communicate with the Selenium RC Server passing each Selenium command for execution. Then the server passes the Selenium command to the browser using Selenium-Core JavaScript commands.
The browser executes the Selenium command using its JavaScript interpreter.
Selenium RC comes in two parts.
RC Scripting
Now let us write a sample script using Selenium Remote Control. Let us use http://www.calculator.net/ for understanding Selenium RC. We will create a sample RC case to open Google page and maximize the browser for testing Selenium Server Service availability.
Step 1 : Start Selenium Remote Control (with the help of command prompt).
Step 2 : Create Eclipse Project, Import Selenium Client Libraries and try below sample code: (Groovy)
- package lab
- import com.thoughtworks.selenium.DefaultSelenium;
- import com.thoughtworks.selenium.Selenium;
- // Instatiate the RC Server
- Selenium selenium = new DefaultSelenium("192.168.140.141", 4444 , "firefox", "http://www.google.com.tw");
- selenium.start(); // Start
- selenium.open("/"); // Open the URL
- selenium.windowMaximize();
- sleep(5)
- selenium.stop();
Webdriver
(Source) WebDriver is a tool for automating testing web applications. It is popularly known as Selenium 2.0. WebDriver uses a different underlying framework, while Selenium RC uses JavaScript Selenium-Core embedded within the browser which has got some limitations. WebDriver interacts directly with the browser without any intermediary, unlike Selenium RC that depends on a server. It is used in the following context:
Architecture
WebDriver is best explained with a simple architecture diagram as shown below.
Selenium RC Vs WebDriver
Scripting using WebDriver
Now it is time to code. The following script is easier to understand, as it has comments embedded in it to explain the steps clearly. Please take a look at the chapter "Locators" to understand how to capture object properties.
- package lab
- import org.openqa.selenium.remote.DesiredCapabilities
- import org.openqa.selenium.remote.RemoteWebDriver
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.*;
- import org.openqa.selenium.firefox.FirefoxDriver;
- DesiredCapabilities capability = DesiredCapabilities.firefox();
- driver = new RemoteWebDriver(new URL("http://192.168.140.141:4444/wd/hub"), capability);
- //Puts an Implicit wait, Will wait for 10 seconds before throwing exception
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- //Launch website
- driver.get("http://www.calculator.net/");
- //Maximize the browser
- driver.manage().window().maximize();
- // Click on "7" from Calculators
- driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[1]/span[1]")).click();
- // Click on Plus from Calculators
- driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[1]/span[4]")).click();
- // Click on "3" from Calculators
- driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[3]/span[3]")).click();
- // Click on "=" from Calculators
- driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[5]/span[4]")).click();
- // Get the Result Text based on its id
- String result = driver.findElement(By.id("sciOutPut")).getText();
- // Print a Log In message to the screen
- System.out.println(" The Result is " + result);
- // Check if the browser is launching and opening google page now
- printf("Sleep 5 sec...\n")
- sleep(5)
- //Close the Browser
- driver.close()
- driver.quit()
Most Used Commands
The following table lists some of the most frequently used commands in WebDriver along with their syntax.
Supplement
* Selenium wiki - Remote WebDriver
* Stackoverflow - Set chrome.prefs with python bindi...g for selenium in chromedriver
* Stackoverflow - RemoteWebDriver Chrome - start maximized
* Stackoverflow - How to build remote Webdriver for Chrome
沒有留言:
張貼留言