Introduction
- Selenium is a suite of software tools that is used for test automation
- It is an open source suite of tools mainly for functional and regration test automation
- Selenium can be used for various operating system like Windows, Linux, Mac etc.
- It supports various browsers like Mozilla Firefox, IE, Google Chrome, Safari, Opera etc. However, Selenium IDE supports Mozilla Firefox only.
- Selenium supports various programming languages like Java, C#, PHP, Python, Ruby and PERL
- Selenium came into the market in 2004. Later in 2006, Google launched Selenium web driver
- In 2008, Selenium team decided to merge selenium webdriver with selenium Remote Control (RC) and released Selenium 2.0
- Selenium 1 consists of Selenium IDE + Selenium RC + Selenium Grid
- Selenium 2 consists of Selenium IDE + Selenium RC + Selenium Webdrivers + Selenium Grid
Selenium Tool Suite
Selenium is a suite of software tools consists of
- Selenium IDE
- Selenium RC
- Selenium Web drivers
- Selenium Grid
Selenium IDE
It is Firefox plugin used to create and test software. Features of the Selenium IDE includes
- Create Test Cases and Test Suites
- Edit Test Cases
- Execute Test Cases, Test Suites
- Debug Test Cases
- Enhance Test cases
- Export Test Cases to other supporting languages (Java, Ruby, Python etc…)
- Selenium IDE test case default format is .html
Few drawbacks of Selenim IDE includes
- It supports Mozilla Firefox browser only
- It does not support data driven testing
- It is not suitable for complex test case design
Selenium RC
Selenium RC stands for Selenium Remote Control which is outdated by now.
Selenium WebDriver
- This sends commands directly to the web browser and retrieves results. It is a programming interface to create and execute test cases.
- Selenium WebDrivers supports Java, C#, PHP, Python, Ruby, Perl
- Selenium WebDriver supports various operating systems like Windows, Linux, Mac etc
- For the execution of test cases in different browsers, the WebDriver changes but the test cases remain same.
Selenium architecture is given below
Selenium Grid
- Selenium Grid can execute testcases in multiple browsers in multiple operating systems in parallel.
- Selenium Grid is used only for test case execution but not for test case design.
Example
The below sample program opens https://university.mongodb.com/ URL and login using correct credential and incorrect credential
Required software
- Mozilla Firefox
- Eclipse
- JDK 1.8
- Selenium jars frm maven repository.
Steps to Write Selenium Code
- Write a maven project. You can follow my another blog to create maven project.The structure will look like below
- Add selenium and junit dependencies in pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ashish.selenium</groupId>
<artifactId>SeleniumTesting</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<springframework.version>4.1.6.RELEASE</springframework.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
- Write a test suite SeleniumTestSuite.java
package demo;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
MongoLoginTest.class
})
public class SeleniumTestSuite {
}
- Write a test case to opens https://university.mongodb.com/ URL and login using correct credential and incorrect credential. MongoLoginTest.java
package demo;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
@RunWith(JUnit4ClassRunner.class)
public class MongoLoginTest {
// WebDriver driver = null;
@Before
public void before() {
// driver = new FirefoxDriver();
// driver.get("https://university.mongodb.com/");
}
@Test
public void successfulLogin() {
WebDriver driver = new FirefoxDriver();
driver.get("https://university.mongodb.com/");
WebElement signin = driver.findElement(By.xpath("//a[@class='nav-panel-top__a' and @href='#']"));
signin.click();
WebElement email = driver.findElement(By.name("email"));
WebElement password = driver.findElement(By.name("password"));
WebElement submit = driver.findElement(By.name("submit"));
email.sendKeys("ashismo@gmail.com");
password.sendKeys("ashish");
submit.click();
// driver.close();
}
@Test
public void failedLogin() {
WebDriver driver = new FirefoxDriver();
driver.get("https://university.mongodb.com/");
WebElement signin = driver.findElement(By.xpath("//a[@class='nav-panel-top__a' and @href='#']"));
signin.click();
WebElement email = driver.findElement(By.name("email"));
WebElement password = driver.findElement(By.name("password"));
WebElement submit = driver.findElement(By.name("submit"));
email.sendKeys("ashismo@gmail.com");
password.sendKeys("ashish1");
submit.click();
// driver.close();
}
}