Wednesday, December 4, 2013

Selenium WebDriver Commands With Example

Selenium WebDriver Commands With Example

  • What is WebDriver? 
WebDriver is a tool for writing automated tests web applications. It aims to mimic the behavior of a real user, and as such interacts with the HTML of the application.

1. What is WebDriver "By" class?
--> Mechanism used to locate elements within a document. 
Direct Known Subclasses:
By.ByClassName, By.ByCssSelector, By.ById, By.ByLinkText, By.ByName, By.ByPartialLinkText, By.ByTagName, By.ByXPath, ByAll, ByChained, ByIdOrName

2. Difference between findElement() and findElements() functions?
--> findElement() : Returns a single WebElement within the current page

Syntax: WebElement findElement(By by)

driver.get("https:www.gmail.com");
WebElement userName=driver.findElement(By.id("username"));
userName.sendKeys("ravismundhe");

-- findElements() : Returns List of WebElements within the current page.

Syntax: WebElement findElements(By by)

driver.get("https://www.matrix.com");
List <webelement> textboxes1=driver.findElements(By.xpath("//input[@type='text']"));
System.out.println("total textboxes "+textboxes1.size());









Wednesday, November 27, 2013

What is Selenium Remote Control


1. What is Selenium ?
--> Selenium is a portable software testing framework for web applications.
The tests can be written as HTML tables or coded in a number of popular programming languages and can be run directly in most modern web browsers.
Selenium operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior.
Selenium can be deployed on Windows, Linux, and Macintosh.
Selenium is used for UAT (User Acceptance Test)

2. Why and When To Automate?
-->Frequent regression testing
Repeated test case Execution is required
User Acceptance Tests
Faster Feedback to the developers
Reduce the Human Effort
Test same application on multiple environments

3. What are the Components of Selenium ?
-->Selenium IDE
Selenium Remote Control
Selenium Grid

4. Explain about Selenium-IDE ? 
-->Selenium IDE is an integrated development environment for Selenium tests.
Operates as a Firefox add-on and provides an interface for developing and running individual test cases or entire test suites.
Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back.
It also has a context menu (right-click) integrated with the Firefox browser, which allows the user to pick from a list of assertions and verification for the selected location.
Offers full editing of test cases.
Although it is a Firefox only add-on, tests created in it can also be run against other browsers by using Selenium-RC & specifying the name of the test suite on the command line.

5. What are the Features of Selenium IDE ?
-->Record and playback
Intelligent field selection will use IDs, names, or XPath as needed
Auto complete for all common Selenium commands
Walk through test cases and test suites.
Debug and set breakpoints
Save tests as HTML, Ruby scripts, or other formats
Support for Selenium user-extensions.js file
Option to automatically assert the title of every page
Roll up common commands

6. What is Selenium-RC (Remote Control) ?
-->Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensible in developing test logic. 
 
For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.

7. Explain about Selenium-Grid ?
-->Selenium-Grid allows the Selenium-RC solution to scale for test suites or test suites to be run in multiple environments. 

With Selenium-Grid multiple instances of Selenium-RC are running on various operating system and browser configurations, each of these when launching register with a hub. When tests are sent to the hub they are then redirected to an available Selenium-RC, which will launch the browser and run the test. 

This allows for running tests in parallel, with the entire test suite theoretically taking only as long to run as the longest individual test.
  
8. What are the Selenese Commands in Selenium ?
c-->licking a link - click or clickAndWait commands
entering values - type command
selecting options from a drop-down listbox - select command
clicking checkboxes or radio buttons - click command
What is Test Runner ?
Test Runner allows you to run the test case in a browser  loaded with the Selenium-Core  TestRunner. Test runner is invoked by clicking the below  Shown button in the IDE.

9. What are the Running Options in Selenium ?
-->Run a Test Case: Click the Run button to run the currently displayed test case.
Run a Test Suite: Click the Run All button to run all the test cases in the currently loaded test suite.
Stop and Start: The Pause button can be used to stop the test case while it is running. The icon of this button then changes to indicate the Resume button. To continue click Resume.
Stop in the Middle: Set a breakpoint in the test case to cause it to stop on a particular command. This is useful for debugging your test case. To set a breakpoint, select a command, right-click, and from the context menu select Toggle Breakpoint.
Start from the Middle: We can set the IDE to begin running from a specific command in the middle of the test case. This also is used for debugging. To set a start point, select a command, right-click, and from the context menu select Set/Clear Start Point.
Run Any Single Command: Double-click any single command to run it by itself. This is useful when writing a single command. It lets you immediately test a command you are constructing, when you are not sure if it is correct. You can double-click it to see if it runs correctly. This is also available from the context menu.   

10. What are the Assertion Statements in Selenium ?
-->assertTextPresent : This will assert if the text is present in the page.
assertText: This will assert if a particular element is having the particular text.
assertTitle: This will assert if the page is having a proper title.
assertValue: This will assert if a Text box or check box has a particular value
assertElementPresent: This will assert if a particular UI Element is present in the page.

11. Explain Selenium WaitFor Commands ?
-->waitForPageToLoad : This command will make the script to wait till the page loads.Syntax is waitForPageToLoad(timeout); Time out is the maximum time the script will wait for the page to load.
waitForAlert : This command will wait for the alert message to appear
waitForTable: This command will wait for the Web table to completely load in the page
waitForTitle: This command will for the page Title to appear on the browser. 
Other waitFor commands : Selenium has several other wait command like waitForText,waitForPopup and so on. These commands are generically called Synchronization commands   

12. What are the Advantages and Disadvantages of Selenium ? 
-->Advantages
Supports C#, Java, PHP, Perl, Phython
Supported on Windows, Linux and Mac OS
Highly active developer community - backed by Google
Powerful methods to locate elements (Xpath, CSS, DOM)
Disadvantages
Challenges running on browsers other than Firefox
Difficulty to automate AJAX
Limitations to automate Applets, MS and Desktop Applications

Wednesday, May 15, 2013

How Selenium RC internally works

How Selenium RC internally works


About Selenium-RC:

The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT.
Client libraries which provide the interface between each programming language and the Selenium-RC Server.

About Selenium Server:

Selenium Server receives Selenium commands from your test program, interprets them, and reports back to your program the results of running those tests

About Client Libraries:

The client libraries provide the programming support that allows you to run Selenium commands from a program of your own design. There is a different client library for each supported language. A Selenium client library provides a programming interface (API), i.e., a set of functions, which run Selenium commands from your own program. Within each interface, there is a programming function that supports each Selenese command.

Fro writing test automation script you can use Selenium-RC with the programming language of your choice then and then you have chosen a language to work with, you simply need to:

1. Install the Selenium-RC Server.
Set up a programming project using a language specific client driver.

2. Install selenium server
The Selenium-RC server is simply a Java jar file (selenium-server.jar), which doesn't require any special installation. Just downloading the zip file and extracting the server in the desired directory is sufficient.

3. Running Selenium Server
Before starting any tests you must start the server. Go to the directory where Selenium-RC’s server is located and run the following from a command-line console.

>java -jar selenium-server.jar

Wednesday, February 29, 2012

Create Test Automation Framework Using Selenium RC / Maven / Java

                                                                        :Test Environment:

  - Apache Maven 2.2.1
  - Java version: 1.6.0_24
  - OS name: "Windows XP" version: "5.2"
  - Eclipse Version: Helios Release  Build id: 20100610-0636
  - Selenium  RC 1.0.2

About Our Test Framework:

    Our test automation framework is Java-based and built on top of JUnit framework. To understand this framework, pre-requisite skills are:
        - Understanding of OOP concepts.
        - What is Class.
        - What is Interface.
        - Ability to write simple programs using Java.
        - Understanding of JUnit .
        - Development with Eclipse.

Selenium-RC (Remote Control) 1.0.2:
    Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensibility in developing test logic. For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.
    Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. This ability to use Selenium-RC with a high-level programming language to develop test cases also allows the automated testing to be integrated with a project’s automated build environment.


Find the link for further details
http://seleniumhq.org/docs/01_introducing_selenium.html


Maven 2.2.1:
    Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Find the link for further details:
http://maven.apache.org/guides/mini/guide-creating-archetypes.html


How to install selenium RC:
     Firstly there is nothing known as installing selenium Remote Control.
RC has to be configured in eclipse Following are the steps:
      1) Make sure java and eclipse are installed in your machine.
      2) Download the selenium -RC 1.0.2 jar files from selenium site.
         (On myt machine selenium rc is downloaded at  C:\selenium-remote-control-1.0.2\selenium-server-1.0.3)
      3) Include the class files in the project classpath
          (Select project in Eclipse. Click file/properties ... write the whole steps here.
          List the name of the jar files here.
                selenium-server-coreless.jar
                selenium-server-sources.jar
                selenium-server.jar
      4) Make sure JUnit is installed in eclipse.
          (How to make sure that?. If it is not installed then how to install it?)

JUnit: What version of junit ?
    JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing framework.

Find the link for further details:
http://java.sun.com/developer/Books/javaprogramming/ant/ant_chap04.pdf

How to include Maven plug in eclipse:
    Installing a Maven Plugin for Eclipse can be a bit tricky if you want to get the whole spectrum of features.

Find the link for further details:
http://www.dotkam.com/2008/02/24/install-maven-plugin-for-eclipse/

Setting up Environment variables:
1. JAVA_HOME = ../java
eg.  it is set as follows on my machine:
C:\Program Files\Java\jdk1.6.0_26

2. M2_HOME = ../maven
eg.: c:/apache-maven-2.2.1

3. PATH = ../java/bin;../maven/bin;
e.g. C:\PROGRAM FILES\JAVA\JDK1.6.0_26\BIN; C:\apache-maven-2.2.1\bin

Importing testing project in Eclipse:
1. Open Eclipse
    - Click on File -> Import -> Maven -> Existing Maven Project -> Click Next -> Select root directory -> Finish.
    - Wait for few second and select imported project and click on Project -> Clean.
 
How to START / STOP selenium server from command line:
    Open command prompt and go to selenium server director and run following command
"java -jar selenium-server.jar -port 4444"
e.g.
cd C:\selenium-remote-control-1.0.2\selenium-server-1.0.3

java -jar selenium-server.jar -port 4444
For STOP:
press CTRL + C from keyboard.


 

Tuesday, February 28, 2012

OOP's Concepts

Object Oriented Programming language.
     1. Abstraction
     2. Encapsulation
     3. Inheritance
     4. Polymorphism


Abstraction: 
                     Hiding unnecessary data from the user details is called abstraction.
                     e.g. TV Remote Buttons.


Encapsulation:
                    Writing operations and methods stored in single class this is called Encapsulation.
                     e.g. Medical Capsules.


Inheritance:
                    The new class is existing from old class, i.e. subclass is existing from super class.
                    e.g. Father and Son relationship.


Polymorphism:
                    A single function or single operator has different character in different place.
                    e.g. Person
                          - Person in home act is husband / Son.
                          - In Office acts Employee.
                          - In public Good citizen.
  

Sunday, November 6, 2011

Creating a Selenium RC test script using following simple steps:

When you start testing using Selenium Remote Control couple of question start running in your mind... :) like what to test?, for this testing what do we require? , etc......
Stop thinking and start writing test script using following simple steps, it will help you to create a simple test script.

Question: What to test?
Answer: Login with valid gmail account and compose a text mail and send it to the same account.

Steps to test:
                  1. Open Gmail login page.
                  2. Verify Gmail login page and "Sign in" panel.
                  3. Enter valid user name and password.
                  4. Click on "Sign in" button.
                  5. Compose text mail and send it to the same gmail account.
                  6. Go to "Sent Item" and verify the mail is revived or not.
                  7. Sign out.

Steps 1:
           Test environment: (A testing environment is a setup of software and hardware)
                  - Physical (hardware)
                  - Logical (software)
                            The first thing is: Script maker should have following software:
                                   - JDK, JRE (which needs to be 1.5 or later).
                                   - Firefox and firebug.
                                   - Eclipses 3.6 (Available on following link)
                                   - Selenium 1.0.2 (Available on following link)
                                     - Junit:
                                               It is a framework for unit testing. When implementing Selenium Test cases using RC, you can implement using Junit. It is a very powerful testing framework and provides us with features like report generation, batch running, skipping tests etc. Selenium along with Junit and ANT can generate very good Selenium test reports in HTML. You can write the code without using Junit as well but Junit provides features which make our task very easy. Features like running multiple test cases in differnt suites in a particular order, test reports, annotations etc. Junit is very famous as compare to TestNG.

Steps 2:
                - Open eclipse
                - Create project web-gmail-testing
                - Create package "selenium ".
                - Create a Java file "GmailLoginTest"

Steps 3:
                Once you have done above steps add external JAR files into created java project "web-gmail-testing", these files are as following:
                1. JUnit jar.
                2. Selenium Remote Control JAR file "selenium-java-client-driver-1.0.2.jar".

Steps 4:
                - Start writing code using following PNG files:
                - Open created java file "GmailLoginTest" and start writing code:
Please refer the following images :) it will help how to inspect actual location of fields, links, text, etc....

IMP:
XPath: Xpath is a language for finding information in an XML document. Xpath is a syntax for defining parts of an XML / HTML document.

Firebug: Firebug is web development tool that facilitates the debugging, editing and monitoring of any web sites CSS, HTML, DOM.

                                           Image 1: Open Gmail login page 


                                           Image 2: After login with valid gmail account.


                                           Image 3: Compose text mail.


                                          Image 4: Verify send text mail into Sent Item.


                                                                    Start coding for above steps using JAVA :

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
  * Class name: GmailLoginTest.java
  **/

public class GmailLoginTest extends SeleneseTestCase {
               
public void setUp() throws Exception {
                super.setUp(); 
                selenium = new DefaultSelenium("localhost", 4444, "*chrome", " https://mail.google.com/mail/");
                selenium.start();type
}

public void testGmailLoginTest() throws Exception {

                selenium.open("https://mail.google.com/mail/");

                /* Verify Google mail login page header bar */
                selenium.isElementPresent("xpath=//div[@class='google-header-bar']");
               
                /* Verify Sign in box and labels and text fields. */
                selenium.isElementPresent("xpath=//div[@class='signin-box']");
               
                /* Verify the User Name label. */
                String userName = selenium.getText("xpath=//strong[@class='email-label']");
                assertEquals(userName, "Username");

                /* Verify the Password label */
                String password = selenium.getText("xpath=//strong[@class='passwd-label']");
                assertEquals(password, "Password");

                /* Verify if Username and Password text fields is available then enter the valid username and password.*/
                if(selenium.isElementPresent("xpath=//input[@id='Email']")) {
                                selenium.type("xpath=//input[@id='Email']", "seleniumremotecontrol");
                }             
                if(selenium.isElementPresent("xpath=//input[@id='Passwd']")) {
                                selenium.type("xpath=//input[@id='Passwd']", "selenium");
                }
               
                /* Click on "Sign in" button. */
                selenium.click("xpath=//input[@id='signIn']"); 
                selenium.waitForPageToLoad("60000");
               
                /* Verify Login user. */
                String loginUserName = selenium.getText("xpath=//span[@id='gbi4m1']");
                assertEquals(loginUserName , "seleniumremotecontrol");

                /* Write a email and send it to own gmail id */
                selenium.click("xpath=//div[@role='navigation']//div[@class='z0']//div");
                pause(7000);

                /* Enter email address. */
                selenium.type("xpath=//textarea[@name='to']", "seleniumremotecontrol@gmail.com");
               
                /* Enter Subject. */
                selenium.type("xpath=//input[@name='subject']", "Auto Email");

                /* Enter body text. */
                selenium.type("xpath=//body[@class='editable  LW-yrriRe']", "Hello Selenium User !!!!!!!!!! :) ");

                /* Click "Send" button. */
                selenium.click("xpath=//div[@class='dW E']//div[@class='dX Cq J-Jw']//div");
                pause(7000);

                /* Verify the send email into send item box. */
                selenium.click("link=Sent Mail");
                pause(10000);

                /* Verify email id whever you used for testing :) */
                String sentItemEmail = selenium.getText("xpath=//tr[@id=':1vc']//td[4]//span[@email='seleniumremotecontrol@gmail.com']");
                assertEquals(sentItemEmail, "seleniumremotecontrol");

                /* Sign Out the gmail. */
                selenium.click("xpath=//span[@id='gbi4m1']");
                pause(2000);
                selenium.click("link=Sign out");
                pause(10000);

                /* After Sign out verify the Google mail login page */
                selenium.isElementPresent("xpath=//div[@class='google-header-bar']");
               
}

public void tearDown() throws Exception {
                selenium.stop();
                super.tearDown();
}

}


                                --------------------------------------------------------------------------------
                                  Second phase is how to run created test script using selenium.
                                --------------------------------------------------------------------------------
                                            1. Open command prompt.
                                            2. Go to selenium server package.
                                            3. run command "java -jar selenium-server.jar -port 4444" and press enter.
                                            4. Go to eclipse open test file "GmailLoginTest.java" file.
                                            5. Go to Run -> Run as JUnit test.

Please feel free to ask any question regarding to selenium remote control........