Showing posts with label Selenium WebDriver. Show all posts
Showing posts with label Selenium WebDriver. Show all posts

Friday 19 February 2016

Introduction to Selenium WebDriver (Video Tutorial)

Selenium is browser automation tool & is used in wide range of web application. It can be implemented with many programming langauages, can run same script across browser & across operating systems.

This video helps you to learn following topics :


  1. Selenium Introduction
  2. Brief History 
  3. Selenium Tool Suite 
  4. Why Selenium 
  5. Note on Browser & Environment Support
  6. Advantage of WebDriver 
  7. Limitations of WebDriver
  8. How to choose the right Selenium Tool for your need 
  9. A short comparison of Selenium with QTP 
  10. Selenium WebDriver Overview 
  11. Define a Test Strategy for Automation 
  12. Pick a Programming Language 
  13. Choose an Editor 

Saturday 3 October 2015

Create selenium WebDriver project in eclipse with Apache maven

The Problem
I often see Selenium practitioners ask that I have downloaded necessary resources to work on Selenium WebDriver. But, What should be project structure, how to manage dependencies, How to do build  and test management etc.

A Solution
By leveraging Apache Maven in our test framework, we can manage entire life-cycle of a test project. It provides support to define project structure, download the project dependencies libraries automatically, build and test management etc. Maven allows us to get all the Selenium libraries files and their dependencies by configuring the pom.xml (POM – Project Object Model) file.

Let’s dig in with an example

An Example
We will use Eclipse IDE and Maven for building the Selenium WebDriver test framework.
  1. Launch the Eclipse IDE.
  2. Create a new project by selecting [File -> New -> Other] from Eclipse Main Menu.
  3. On the New dialog, select [Maven -> Maven Project] as shown in the following screenshot:

  1. Click on [Next], the New Maven Project dialog will be displayed. Select the [Create a simple project (skip archetype selection)] check-box and set everything to default and click on the Next button as shown in the following screenshot: 

  1. On the New Maven Project dialog box, enter base package name (like com.myproject.app) in Group Id and project name (like myproject) in Artifact Id textboxes. You can also add a name and description but it is optional. Set everything to default and click on the [Finish] button as shown in the following screenshot:


  1. Eclipse will create the myproject project with a structure (in Package Explorer) similar to the one shown in the following screenshot:

7.       
           7. The JRE version might change based on the Java version installed on your machine. 
              Right-click on JRE System Library [J2SE-1.5] and select the Properties option from the 
              menu.       
  1. Select pom.xml from Package Explorer. This will open the pom.xml file in the editor area with the Overview tab open. Select the pom.xml tab instead.
  2. Add the WebDriver and TestNG dependencies highlighted in the following code snippet, to pom.xml in the <project> node:
<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.myproject.app</groupId>
         <artifactId>myproject</artifactId>
         <version>0.0.1-SNAPSHOT</version>
<dependencies>
     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.42.2</version>
    </dependency>
   <dependency>
                  <groupId>org.testng</groupId>
                  <artifactId>testng</artifactId>
                  <version>6.8.8</version>
   <scope>test</scope>
   </dependency>
</dependencies>
  <build>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
      <testResource>
        <directory>src/test/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
    </testResources>
   
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
    </build>
/project>

  1. Right click on pom.xml from Package Explorer & Select [Run As -> Maven Install]

Conclusion
If you'd like to see the code and example used in the above project, you can find them here.


Happy Testing!

Saturday 19 September 2015

Java required by Selenium Automation

#Java #TheBuddhaTree #SeleniumWebDriver

After many of requests/questions from our readers that how much Java is needed for working on Selenium WebDriverHere we are listing all the Java topics, which you should know before start working on Selenium WebDriver.

Introduction to Computers and Java
•       Introduction
•       Computers: Hardware and Software
•       Machine Languages, Assembly Languages and High-  Level Languages
•       Introduction to Object Technology
•       Operating Systems
•       Programming Languages
•       Java Development Environment
•       Test-Driving Java Application

Introduction to Java Applications
•       Introduction
•       Write Your First Program in Java
•       Modifying Your First Java Program
•       Displaying Text
•       Adding Integers
•       Memory Concepts
•       Arithmetic
•       Decision Making: Equality and Relational
•       Conditional Operators

Introduction to Classes, Objects, Methods and Strings
•       Introduction
•       Declaring Class with Method and Instantiating Object of a Class
•       Declaring a Method with Parameter
•       Instance Variables, set getter Methods
•       Primitive vs. Reference Types
•       Initializing Objects with Constructors
•       Floating-Point Numbers and double

Control Statements:  Part  1
•       Introduction
•       Algorithms
•       Control Structures
•       if Statement
•       if...else Statement
•       while Statement
•       Compound Assignment Operators
•       Increment and Decrement Operators
•       Primitive Types

Control Statements: Part 2
• Introduction
• Essentials of Counter-Controlled Repetition
• for Repetition Statement
• Examples Using the for Statement
• do...while Repetition Statement
• switch Multiple-Selection Statement
• break and continue Statements
• Logical Operators
• Structured Programming Summary

  Methods
• Introduction
• Program Modules in Java
• static Methods  and static Fields
• Methods with Multiple Parameters
• Method-Call Stack and Activation Records
• Argument Promotion and Casting
• Java API Packages
• Scope of Declarations
• Method Overloading

Array
• Introduction
• Arrays
• Declaring and Creating Arrays
• Arrays Examples
• Enhanced for Statement
• Passing Arrays to Methods
• Multidimensional Arrays
• Limitation of Array

Classes and Objects: In Detail
• Introduction
• Controlling Access to Members
• Referring to the Current Object’s Members with this  Reference
• Overloaded Constructors
• Default and No-Argument Constructors
• Setter and Getter Methods
• Composition &  Enumerations
• Garbage Collection and Method finalize
• static Class Members
• static Import
• final Instance Variables
• Creating & Accessing package

Inheritance
• Introduction
• Superclass and Subclass
• protected Members
• Relationship between Superclass and Subclass
• Constructors in Subclasses
• Class Object

Polymorphism
• Introduction
• Polymorphism Examples
• Polymorphic Behavior
• Abstract Class and Method
• final Method and Class
• Using Interfaces

Exception Handling
• Introduction
• Example: Divide by Zero
• When to Use Exception Handling
• Java Exception Hierarchy
• finally Block
• Obtaining Information from an Exception Object
• Chained Exceptions
• Declaring New Exception Types
• Preconditions and Post-conditions
• Assertions

Strings, Characters and Regular Expressions
• Introduction
• Fundamentals of Characters and Strings
• Class String
• String operation
• Class Character
• Tokenizing Strings
• Regular Expressions, Class Pattern and Class Matcher
  
Files, Streams and Object Serialization
• Introduction
• Files and Streams
• Class File
• Sequential-Access Text Files
• Object Serialization
• Additional java.io Classes

Generic Collections Classes & Methods
• Introduction
• Collections Overview
• Type-Wrapper Classes for Primitive Types
• Autoboxing and Auto-Unboxing
• Interface Collection and Class Collections
• Lists
• Collections Methods
• Sets
• Maps
• Properties Class
• Synchronized Collections
• Un-modifiable Collections


• Abstract Implementations

Saturday 12 September 2015

Selenium Integration with Jenkins

#thebuddhatree #Selenium #WebDriver #Maven #Jenkins

The Problem
Running your Selenium project from CI tools like Jenkins.

A Solution
In order to integrate Selenium project with Jenkins, you need to manually configure Jenkins job. Let's step through how to do it and make sure it's working.

An Example 
Install Maven in Eclipse, after that create a Maven project for your Selenium. To do that 
  1. Open Eclipse
  2. Create a Maven project
  3. Update POM.XML file with required dependency
  4. Write your Selenium WebDriver code
  5. Make sure code is running
  6. Follow below given steps 

Manorama Yearbook 2015 (Book & CD)
Step 1


Step 2



Step 3



Step 4



Step 5



Step 6




Expected Behavior
Scion of Ikshvaku (1st Part in Ram Chandra Series)
When you build your project from Jenkins, here is what will happen -

  • Build is created & Success message is displayed
Conclusion
Keep in mind that your Maven project should be running successful from local machine.

Note - If any doubt/query/suggestion, pls post in forum - Testing Forum

Happy Learning!




SeleniumFramework - A simple web testing framework based on open source technology Selenium WebDriver, TestNG, Java, Apache POI

#Selenium #WebDriver #Maven #TestNG #Framework #POI #Jenkins

This framework is designed to help you get started with Web testing quickly, to grow as needed, and to avoid common pitfalls. it just allows you to manipulate browser.
Who Will Cry When You Die?

Main features:
  • Provides browser automation using Selenium WebDriver
  • Page Object Model is used to design pattern to create Object Repository for web UI elements.
  •  Running test case against multiple set of test data
  • Automation Suite Configuration using Java properties file
  • TestNG.xml file which provides feature to run selective test suite
  • Screenshot capturing capability for failed test scenario (Take screenshots on demand and save on disk.)
  • Uses Apache logger for generating test execution logs
  • Build Tool : Maven
  • Programming language: Java
  • Test Data Management: data stored in Excel worksheet : Apache POI
  • Parallel Execution : Using Selenium Grid to run simultaneous test execution
  • CI Integration : Integrated with Jenkins
  • Exceptions handling : Using Java try/Catch exception handling feature
  • Reports : Custom html reports are generated and available in test-output folder

To download Framework, visit link - Download Here

Note - If any doubt/query/suggestion, pls post in forum - Testing Forum

Sunday 6 September 2015

How to Verify the limit of characters of a textbox

If you already know the char limit, then input the text which is more than the permissible limit. For example if max char limit is 30 then send a text with 31 char length and then fetch the text and count the number of character. It should be 30.

Code example is here which is written considering the limit is 30 char :- 

//Data input should be more than 30 chars
driver.findElement(By.id("textfield1")).sendKeys("Testexample1Testexample2Testexa");

//get count of string length
int actualLimit =driver.findElement(By.id("textfield1")).getText().length();

//Assertion
assertEquals(actualLimit, 30);

Saturday 5 September 2015

Selenium WebDriver : An Overview of Exception Class

List of exception classes in Selenium package (which is "org.openqa.selenium"), which we see as error on daily basis.
  1. ConnectionClosedException
  2. ErrorHandler.UnknownServerException
  3. ImeActivationFailedException
  4. ImeNotAvailableException
  5. InvalidCookieDomainException
  6. InvalidCoordinatesException
  7. InvalidElementStateException
  8. JsonException
  9. MoveTargetOutOfBoundsException
  10. NotFoundException 
  11. ScreenshotException
  12. SessionNotCreatedException 
  13. SessionNotFoundException
  14. StaleElementReferenceException
  15. TimeoutException
  16. UnableToCreateProfileException 
  17. UnableToSetCookieException
  18. UnexpectedTagNameException
  19. UnhandledAlertException 
  20. UnreachableBrowserException
  21. UnsupportedCommandException
  22. WindowsRegistryException

Wednesday 30 July 2014

Selenium: How to handle Stale Element Exception error

Common Causes
A stale element reference exception is thrown in one of two cases:
  1. The element has been deleted entirely or element/page  has been refreshed
  2. The element is no longer attached to the DOM.


Solution
  1. The most simple solution is to use the Java PageFactory, this will create a proxy WebElement that will find the element every time you use it (There is a slim change that the element will be released in the couple of milliseconds between it being found and you performing an action on it, in this case it is suggested to use an explicit wait to wait for the element to become stale before finding it again).
  2. Use try catch to handle this exception and reload the element in catch block.
  3. Add a short sleep between FindElement & action method..


Friday 19 July 2013

How To Take A Screenshot

#SeleniumWebDriver  #TakeScreenshot #TheBuddhaTree

The Problem
When we do nightly test execution, we need historical evidence to analyze & debug script failure. Sometime, it can be challenging to track down the issue that caused a failure using failure message along with stack trace. Especially when you run the test again and it passes.

A Solution
A simple way to gain insight into your test failures is to capture screenshots of AUT at the moment of failure and add the screenshots into test execution result. 

How to Implement using Selenium
Selenium WebDriver provides the TakesScreenshot interface for capturing a screenshot of a web page. This helps in test runs, showing exactly what happened when an exception or error occurred during execution, and so on.


TakesScreenshot interface provides the getScreenshotAs() method to capture a screenshot of the page displayed in the driver instance.  Example code is as follows –


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TakeScreenshotOfApplication {
                WebDriver driver;

    @Test
       public void compareTitle() {
              driver = new FirefoxDriver();

              driver.get("http://google.co.in");

            // compare title & take screenshot of application if it fails
       if (driver.getTitle().contains("facebook")) {
                 System.out.println("Title matched!!");
       } else {
                 System.out.println("Title did not match");
                 takeScreenshotMethod();
            }
        }

        private void takeScreenshotMethod() {
             // code to take screen shot of browser
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

             // file name where image to be saved
             String screenshotpath = "C:\\google.png";
               try {
                       // save image to drive
                        FileUtils.copyFile(scrFile, new File(screenshotpath));
               } catch (IOException e) {
                       e.printStackTrace();
              }
          }

}

Expected Behavior
  1. Load the URL
  2. Take Screenshot
  3. Save Screenshot Image to local drive
Happy Learning