Final Report Selenium
Final Report Selenium
Selenium is an open source automation testing tool.It is used exclusively for web based
applications.You can work on multiple operating systems using selenium.
1.SeleniumHistory
Selenium came into existence in 2004 when GUY named Jason Huggins was testing an
internal application at ThoughtWorks.
Selenium is a set of different software tools each with a different approach to supporting
test automation.
1.1 Selenium Components:
Selenium IDE
Selenium Remote Control
Selenium WebDriver
Selenium Grid
Lets have a look on each of these components:
Selenium IDE
Selenium IDE is a complete integrated development environment (IDE) for Selenium
tests. It is implemented as a Firefox extension, and allows recording, editing, and
debugging tests. It was previously known as Selenium Recorder.
Scripts are recorded in Selenese, a special test scripting language for Selenium.
Selenese provides commands for performing actions in a browser (click a link, select an
option), and for retrieving data from the resulting pages.
It is not only just play back tool, It can records user actions as they are performed and
then exports them as reusable script in one of many programming languages that can
be later executed.
Drawbacks:
As it will come with only Firefox addin.What if you want to test your application which
works only in Internet explorer or some any other browser?
Selenium IDE is not suitable when you want to built a robust frameworks.Selenium IDE
doesnt provide iteration or conditional statements for test scripts.
Drawbacks.
As it is entirely use JavaScript to talk to browser, it leads to significant weakness. Every
browser impose very strict security rules on the JavaScript being executed to protect the
users from malicious scripts
There is no support for Android and IOS Platform
Server need to be started every time to run a program.
Lot of Limitations when a Application has Rich API with dynamic elements
Native keyboard and mouse events cannot be handled in efficient manner
Webdriver:(Selenium 2.0)
This brand new automation tool provides all sorts of features, including a more cohesive
and object oriented API as well as an answer to the limitations of the old
implementation.It supports the WebDriver API underlying technology,along with the
Selenium underlying technology.
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:
AJAX-based UI elements.
Architecture
best possible way but has the downside that new browsers entering the market will not
be supported straight away like we can with RC.
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
The Selenium Server launches and kills browsers. In addition to that, it interprets
and executes the Selenese commands. It also acts as an HTTP proxy by
intercepting and verifying HTTP messages passed between the browser and the
application under test.
Client libraries that provide an interface between each one of the programming
languages (Java, C#, Perl, Python and PHP) and the Selenium-RC Server.
Selenium RC Vs WebDriver
Selenium RC
Selenium WebDriver
Step 5 : Go to Libraries Tab and select all the JAR's that we have downloaded. Add
reference to all the JAR's of Selenium WebDriver Library folder and also selenium-java2.42.2.jar and selenium-java-2.42.2-srcs.jar.
Step 7 : Now right-click on the package and select 'New' >> 'Class' to create a 'class'.
Step 8 : Now name the class and make it the main function.
Step 10 : 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.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class webdriverdemo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.calculator.net/");
//Maximize the browser
driver.manage().window().maximize();
// Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
// Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();
// Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10");
// Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");
// Click Calculate Button
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[2]/td/input[2]")).click()
;
Description
driver.get("URL")
To navigate to an
application.
element.sendKeys("inputtext")
element.clear()
select.deselectAll()
select.selectByVisibleText("some text")
driver.switchTo().window("windowName")
driver.switchTo().frame("frameName")
driver.switchTo().alert()
driver.navigate().to("URL")
driver.navigate().forward()
To navigate forward.
driver.navigate().back()
To navigate back.
driver.close()
driver.quit()
driver.refresh()
As Webdriver directly talks with browser we can overcome the limitations of JavaScript
security model which we have face with Selenium Core in RC
Selenium-WebDriver supports the following browsers along with the operating systems
these browsersare compatible with.
Selenium Grid
Selenium Grid allows you to run your tests in parallel, that is,different tests can be run
at the same time on different remote machines. s. Also, if you must run your test suite
on multiple environments you can have different remote machines supporting and
running your tests in them at the same time. In each case Selenium Grid greatly
improves the time it takes to run your suite by making use of parallel processing
Java
C#
Ruby
Pyton
PHP
Pearl
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Step 2 : Go to "Downloads" section and select "JDK Download".
Step 4 : Select the appropriate installation. In this case, it is 'Windows 7-64' bit. Click
the appropriate link and save the .exe file to your disk.
Step 5 : Run the downloaded exe file to launch the Installer wizard. Click 'Next' to
continue.
Step 7 : The installer is extracted and its progress is shown in the wizard.
Step 8 : The user can choose the install location and click 'Next'.
Step 9 : The installer installs the JDK and new files are copied across.
Step 10 : The Installer installs successfully and displays the same to the user.
Step 11 : To verify if the installation was successful, go to the command prompt and
just type 'java' as a command. The output of the command is shown below. If the Java
installation is unsuccessful or if it had NOT been installed, it would throw an "unknown
command" error.
Step 5 : To configure the workspace, select the location where the development has to
take place.
Step 2 : The add-on installer is shown to the user and it is installed upon clicking the
'Install' button.
Step 3 : After installing, we can launch the plugin by navigating to "Web Developer"
>> "Firebug".
Step 4 : FirePath, a plugin that works within Firebug, helps users to grab the 'XPath' of
an element. Install FirePath by navigating to "https://addons.mozilla.org/enUS/firefox/addon/firepath/"
Step 5 : The add-on installer is shown to the user and it is installed upon clicking the
'Install' button.
Interface:
A Java interface defines a set of methods but does not implement them. A class that
implements the interface agrees to implement all of the methods defined in the
interface.
Localhost Execution
In the following example, we will take a screenshot after calculating the percentage.
Ensure that you give a valid path to save the screenshot.
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebdriverDemo
{
public static void main(String[] args) throws IOException
{
WebDriver driver = new FirefoxDriver();
// Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch website
driver.navigate().to("http://www.calculator.net/");
// Maximize the browser
driver.manage().window().maximize();
Output
Upon executing the script, the screenshot is saved in the 'D:\screenshots' folder with
the name 'screenshots1.jpg' as shown below.
All Links
One of the common procedures in web testing is to test if all the links present within the
page are working. This can be conveniently done using a combination of the Java foreach loop and the By.tagName("a") method. The WebDriver code below checks each
link from the Mercury Tours homepage to determine those that are working and those
that are still under construction.
package practice_webdriver;
import java.util.List;
import
import
import
import
import
java.util.concurrent.TimeUnit;
org.openqa.selenium.*;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.support.ui.ExpectedConditions;
org.openqa.selenium.support.ui.WebDriverWait;
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
}
}
package practice_webdriver;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ImageLink {
public static void main(String[] args) {
String baseUrl = "https://www.facebook.com/login/identify?ctx=recover";
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
//click on the "Facebook" logo on the upper left portion
driver.findElement(By.cssSelector("a[title=\"Go to Facebook Home\"]")).click();
//verify that we are now back on Facebook's homepage
Reading a Table
There are times when we need to access elements (usually texts) that are within HTML
tables. However, it is very seldom for a web designer to provide an id or name attribute
to a certain cell in the table. Therefore, we cannot use the usual methods such as
"By.id()", "By.name()", or "By.cssSelector()". In this case, the most reliable option is to
access them using the "By.xpath()" method.
So anything we do after this will execute only in that particular Firefox browser session.
tests
All the test cases inside <test> tag of testing xml file will run parallel.
classes
All the test cases inside a java class will run parallel
methods
instances
Test cases in same instance will execute parallel but two methods of two different in
will run in different thread.
The attribute thread-count allows you to specify how many threads should be allocated
for this execution.
Complete Example: In this Example three test cases will run parallel and fill login data
in http://demo.guru99.com
Complete project will looks like:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestGuru99MultipleSession {
@Test
public void executSessionOne(){
//First session of WebDriver
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto guru99 site
driver.get("https://facebook-com-p.surfly.com/www/ST/m2zs6ylgcRUGPu");
//find user name text box and fill it
driver.findElement(By.name("uid")).sendKeys("Driver 1");
}
@Test
public void executeSessionTwo(){