2. WHY DO YOU NEED AUTOMATION?
Automation helps to ease the repetitive tasks of Manual testers. Automation can cover many scenarios
within a little time
However automation does not replace manual testing.
In fact, you have to test software manually before you run automated testing.
Even though manual testing requires much effort, without it you cannot be sure that automation is
possible.
3. WHAT IS SELENIUM?
Selenium is an open source automation test suite of different tools focused to automate web
application.
Selenium consists of the following tools
Selenium Integrated Development Environment (Selenium IDE)
Selenium Remote Control (Selenium RC)
Selenium WebDriver
Selenium Grid
4. WHY WAS SELENIUM INTRODUCED?
Repetitive testing
JavaScriptExecutor
As this is pure JS, requires to be
placed within the application
Selenium RC
Allow the JavaScriptExecutor to be
used by different applications
Selenium IDE
Firefox extension that can
automate through a record-and-
playback feature
Selenium WebDriver
Cross platform(not limited to JS)
and control browser from OS
level
Selenium 2
Selenium 3
Improvements
Selenium Grid
Sending selenium commands to
multiple machines
5. SELENIUM WEBDRIVER
Unlike other selenium tools, WebDriver does not rely on JavaScript for Automation. It communicates
directly to the browser
Selenium WebDriver supports Java. C#, Ruby, Python, JS
Like selenium , WebDriver can only support web based applications. Also, it cannot readily support new
browsers
6. SELENIUM WEBDRIVER WITH JAVA - INSTALLATION
1. Install Java
2. Install Eclipse IDE
3. Download selenium client jar file from https://selenium.dev/downloads/
4. Create a Java project
5. Import the downloaded jar files to your library
7. SELENIUM WEBDRIVER WITH JAVA - BASIC COMMANDS
Getting a web page
driver.get("www.javatpoint.com")
driver.Navigate().to("https://javatpoint.com/selenium-tutorial");
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="text" id="Input1" name="Input tag 1" class="MyClass" value="This is the first input"/>
</body>
</html>
Locating elements (Some of them are listed below)
driver.findElement(By.id("id")) //Input1
driver.findElement(By.name("id")) //Input tag 1
driver.findElement(By.xpath("id")) // //input
driver.findElement(By.cssSelector("id")) //input#Input1
driver.findElement(By.tagName("id")) // input
driver.findElement(By.className("id")) // MyClass
8. SELENIUM WEBDRIVER WITH JAVA – BASIC COMMANDS
Basic Input field commands
driver.findElement(By.id("id")).
sendKeys()
clear()
getText()
click()
Browser events (to use this we have to use driver.navigate().to("<url>"))
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
9. SELENIUM WEBDRIVER WITH JAVA – BASIC COMMANDS
Closing the browser
driver.close();
Close all the browser instances (windows) associated with the driver
driver.quit();
Other commands
Actions
Switches
getWindowHandles()/getWindowHandle()
Handling alerts
10. SELENIUM WEBDRIVER WITH JAVA – WAIT COMMANDS
Implicit wait
Applied for the driver instance
Wait before it throws a "No Such Element Exception".
driver.manage().timeouts().implicitlyWait(<Time to wait>, TimeUnit.SECONDS); // Can be seconds, milliseconds,
minutes etc.
Explicit Wait
Wait for certain conditions (Expected Conditions) to be ;
True within the given time
False when the given time exceeds
Applied for specific element
WebDriverWait wait = new WebDriverWait(<Webdriver variable name> ,<Time to wait in seconds>);
WebElement elementName = wait.Until(ExpectedCondition.visibilityOfElementLocated(By.xpath("<xpath
location>")));
11. SELENIUM WEBDRIVER WITH JAVA – WAIT COMMANDS
Fluent Wait
Wait for a condition, as well as the frequency with which we want to check the condition before throwing an
exception
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
Thread.sleep(<time in ms>)
Helps to sleep / suspend the test execution for the given time. This is not recommended for code publishing.
Can be used for debugging.
12. SELENIUM WEBDRIVER WITH JAVA – ASSERT AND VERIFY
Both Assert and Verify commands are used to find whether a given input is present or not on the
webpage.
When an “assert” command fails, the test execution will be aborted. So when the Assertion fails, all the
test steps after that line of code are skipped.
When a “verify” command fails, the test will continue executing and logging the failure.
13. SELENIUM WEBDRIVER WITH JAVA – ASSERT AND VERIFY
//ASSERTION
Assert.assertEquals(ExpectedTitle, CurrentTitle);
System.out.println("Step after assert") ; //Will not be executed if the above assert fails
//To convert an assertion to verification,
try{
Assert.assertEquals(ExpectedTitle, CurrentTitle);
System.out.println("Verification passed");
}catch (Exception e){
System.out.println("Verification failed");
}
System.out.println("Step after assert") ; //Will be executed if the above assert fails
14. SELENIUM WITH JAVA : SIMPLE CODE EXAMPLE
Open Chrome browser
Navigate to https://www.google.com
Verify the Title
Close the browser
15. SELENIUM WITH JAVA : SIMPLE CODE EXAMPLE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AS {
public static void main(String[] args) {
// The Path to your chrome driver
System.setProperty("webdriver.chrome.driver", "G:chromedriver.exe");
// Creating the driver object
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.google.com";
String expectedTitle = "Google";
String actualTitle = "";
16. SELENIUM WITH JAVA : SIMPLE CODE EXAMPLE
// Navigate to the url using chrome
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
// close Chrome
driver.close();
}
}