selenium training

Post on 22-Nov-2014

4.145 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

© 2010 LogiGear Corporation. All Rights Reserved

Selenium OutlineFor LCTPII

Hieu Pham – Dung Huynh – Tuan Tran

© 2010 LogiGear Corporation. All Rights Reserved

• Introduction to Selenium• Introduction to Selenium IDE• Introduction to Selenium RC• How Selenium works• How to install Selenium IDE• Using Selenium IDE• Create and Run a test in Selenium IDE• Useful Tools in Selenium IDE • Introduction to TestNG framework• Introduction to OOP in Java• How to setup TestNG in Eclipse IDE• Creating a simple test in TestNG• Running a test in TestNG• Data-driven test in TestNG

Outline of Sections

© 2010 LogiGear Corporation. All Rights Reserved

Selenium Overview

Selenium is a suite of tools to automate web-app testing across many platforms.

• Selenium can work on any Javascript supported browsers.

• Selenium can be controlled by many programming languages and testing frameworks.

• Selenium can simulate a user navigating through pages and then assert for specific marks on the pages

© 2010 LogiGear Corporation. All Rights Reserved

Selenium Features

• Tests run directly in browser• Support multiple browsers• Intelligent field selection by using IDs, Names, XPath,

Dom, CSS,…• Easy to convert to many formats (HTML, Java, C#,…)• Can be used for unit-testing, regression testing, smoke-

testing, integration and acceptance testing.• Since Selenium is an Open Source tool, there are no

Licensing costs and can be customized as per user request

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to Selenium IDE

What is the Selenium IDE ?

(Integrated Development Environment)

• Selenium IDE is an integrated development environment for Selenium scripts.

• It is implemented as a Firefox extension, and allows you to record, edit, and debug tests.

• Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run.

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to Selenium RC

What is Selenium Remote Control (RC)?

• Selenium RC is a client/server system that allows you to control web browsers locally or on other computers, using almost any programming language and testing framework.

• A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.

• Client libraries for your favorite computer language.

• Selenium Core is the original Javascript-based testing system.

© 2010 LogiGear Corporation. All Rights Reserved

How Selenium Works• Using Selenium IDE to record

test script, then export to appropriate programming languages

• Edit test script on Programming IDE (Ex: Eclipse)

• Using Selenium RC to run test script on Browsers (Firefox, IE, Chrome, Safari,…)

• Selenium Grid extends Selenium RC to distribute your tests across multiple servers, saving you time by running tests in parallel

© 2010 LogiGear Corporation. All Rights Reserved

How to install the Selenium IDE

• Go to Selenium download page, find and install the latest selenium IDE extension for Firefox http://seleniumhq.org/download/

• Select Install Now. The Firefox Add-ons window pops up, first showing a progress bar, and when the download is complete, displays the following windows.

© 2010 LogiGear Corporation. All Rights Reserved

How to install the Selenium IDE

• Restart Firefox. After Firefox reboots you will find the Selenium-IDE listed under the Firefox Tools menu

© 2010 LogiGear Corporation. All Rights Reserved

How to install the Selenium IDE

• Opening the IDE

To run the Selenium-IDE, simply select it from the Firefox Tools menu. It opens as follows with an empty script-editing window and a menu for loading, or creating new test cases.

• What is needed machine wise to setup the tool ?

– Firefox 2.0 +

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• Menu: The Options menu

allows the changing of settings.

The Help menu is the standard Firefox Help menu; only one item on this menu–UI-Element Documentation–pertains to Selenium-IDE.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

- Toolbar:

The toolbar contains buttons for controlling the execution of your test cases, including a step feature for debugging your test cases. The right-most button, the one with the red-dot, is the record button.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

- Toolbar(cont.) :

Apply Rollup Rules: This advanced feature allows repetitive sequences of Selenium commands to be grouped into a single action.

Record: Records the user’s browser actions. Record: Records the user’s browser actions.

Step: Allows you to “step” through a test case by running it one command at a time. Use for debugging test cases.

Record: Records the user’s browser actions.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• Test case Pane:– Your script is displayed in the test case pane. It has

two tabs, one for displaying the command and their parameters in a readable “table” format.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• The Command, Target, and Value entry fields display the currently selected command along with its parameters

• If you start typing in the Command field, a drop-down list will be populated based on the first characters you type; you can then select your desired command from the drop-down.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• Some popular Selenium commandsCommand Target Description

Open URL (Ex: http://www.google.com ) Navigate to a page

Click Control id Click on a control base on id

Type Textbox id Type onto a textbox base on id

verifyTextPresent String of text Check the existing of the text in the page

assertTitle String of text Check the title of the page

Select Id of dropdown, select value Select an item in a drop down list

Check / Uncheck Check box id Check or Uncheck an check box base on id

waitForElementPresent Element id Test case will stop and only continue when element present in the page

chooseOkOnNextConfirmation/ chooseCancelOnNextConfirmation

Choose OK/ Cancel when a confirmation prompts appear

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• Log/Reference/UI-Element/Rollup Pane Log: When you run your test case, error messages and

information messages showing the progress are displayed in this pane automatically, even if you do not first select the Log tab.

© 2010 LogiGear Corporation. All Rights Reserved

Using the Selenium IDE

• Log/Reference/UI-Element/Rollup Pane

Reference: is the default selection whenever you are entering or modifying Selenese commands and parameters in Table mode. In Table mode, the Reference pane will display documentation on the current command.

© 2010 LogiGear Corporation. All Rights Reserved

Creating a Test in Selenium IDE

• How to write test in Selenium IDE– With using recorder: Open Selenium IDE in FireFox

© 2010 LogiGear Corporation. All Rights Reserved

Creating a Test in Selenium IDE

• How to write test in Selenium IDE– With using recorder(cont.)

Choose HTML format

Notes: Notice that the red button is pressed mean it’s ready to record test case

© 2010 LogiGear Corporation. All Rights Reserved

Creating a Test in Selenium IDE

• How to write test in Selenium IDE

– With using recorder(cont.)

Here is an example test:

TC name: Verify the user can login to yahoo mail successfully.

Steps:

1. Open new tab in FireFox and navigate to Yahoo Mail page(http://mail.yahoo.com )

2. Enter username and password(example Username: kimlgr1@yahoo.com , Password: logigear)

3. Click Sign In

Scripts will be generated in Selenium IDE

VP: Verify the main page is loaded successfully.

© 2010 LogiGear Corporation. All Rights Reserved

Creating a Test in Selenium IDE

• How to write test in Selenium IDE (cont.)

Guideline for verification: To verify that main page is

loaded successfully, we could check for existing of Sign out link.

Right click Sign out link Choose Show all available

commands Choose verifyElementPresent

link = Sign Out

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• How to run a test?

• How to debug a test?

• Review results

• Exercise for Selenium

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• How to run a test? The IDE allows many options for running your test

case. Run a Test: 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: You can 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.

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• How to run a test? (cont.)Start from the Middle: You can tell the IDE to begin

running from a specific command in the middle of the test case. This also is used for debugging.

Run Any Single Command: Double-click any single command to run it by itself. This is useful when writing a single command.

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• How to debug a test? Debugging means finding and fixing errors in your test case.

This is a normal part of test case development.

o Breakpoints and Startpoints: o To set a breakpoint, select a command, right-click, and from the context

menu select Toggle Breakpoint. Then click the Run button to run your test case from the beginning up to the breakpoint

o To set a startpoint, select a command, right-click, and from the context menu select Set/Clear Start Point. Then click the Run button to execute the test case beginning at that startpoint.

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• How to debug a test?o Stepping Through a Testcase:

To execute a test case one command at a time (“step through” it), follow these steps:Start the test case running with the Run button

from the toolbar.Immediately pause the executing test case with

the Pause button.Repeatedly select the Step button.

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• Review Results:– After test run

successfully, we can view report result in Log panel like as picture

© 2010 LogiGear Corporation. All Rights Reserved

Running a Test in Selenium

• Exercises for Selenium:A simple exercise:– Open http://www.google.com in Firefox– Open Selenium IDE, Tools > Selenium IDE– Make sure that Selenium is in recording mode– Type in “LogiGear”– On the search results verify that “LogiGear” is one of the search results on the

first page– Save the test case as “Google_ide”– Run the saved test case back again using the IDE– View the results in the IDE

© 2010 LogiGear Corporation. All Rights Reserved

Exercise one

Search with Google: The recorded steps in the pane. The result is in the IDE

© 2010 LogiGear Corporation. All Rights Reserved

Locate by ID

Locate by Name

Locate by XPath

Locate by CSS

Locate by link Text

Locate by DOM

Locate Element in Selenium IDE

© 2010 LogiGear Corporation. All Rights Reserved

• Pattern table

• How to Use in Selenium IDE

Regular Expression in Selenium

Command Target Value

click link=regexp:Film.*Television Department  

verifyTitle regexp:.*Film.*Television.*  

PATTERN MATCH

. any single character

[ ] character class: any single character that appears inside the brackets

* quantifier: 0 or more of the preceding character (or group)

+ quantifier: 1 or more of the preceding character (or group)

? quantifier: 0 or 1 of the preceding character (or group)

{1,5} quantifier: 1 through 5 of the preceding character (or group)

| alternation: the character/group on the left or the character/group on the right

( ) grouping: often used with alternation and/or quantifier

© 2010 LogiGear Corporation. All Rights Reserved

Useful Tools for Selenium

• Useful tools for locating elements in Firefox– Firebug (for locating element id, name)– FirePath (for locating Xpath, CSS of an element)

© 2010 LogiGear Corporation. All Rights Reserved

Useful Tools for Selenium

• Tools are used with Selenium:

– Eclipse

– Cubic Test

– IntelliJ IDEA

– Python

– .Net…

© 2010 LogiGear Corporation. All Rights Reserved

Useful Tools for Selenium

• Other programming languages are available for Selenium:Java: use either JUnit or TestNG as the test engine.

Some development environments like Eclipse have direct support for these via plug-ins

C# : The .NET Client Driver works with Microsoft.NET. It can be used with any .NET testing framework like NUnit or the Visual Studio 2005 Team System.

© 2010 LogiGear Corporation. All Rights Reserved

Useful Tools for Selenium

• Other programming languages are available for Selenium:Python: Py-unit is the test framework to use for

Python

Ruby: Selenium-IDE generates reasonable Ruby, but requires the old Selenium gem.

© 2010 LogiGear Corporation. All Rights Reserved

Exercise two

• Using IDE and necessary tools to record and locate element using Xpath, DOM, CSS

• Login Yahoo Mail using this account logigearmail@yahoo.com / logigear1

• Verify user is login success• Logout of Yahoo Mail• Verify user is logout success

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to OOP(*)(*) in Java

What is an Object?

• An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life (*) : Object Oriented Programming

• Consider a bicycle, for example:

•Notes: For more information relate to Java statements, variables…please refer to http://download.oracle.com/javase/tutorial/java/index.html

Method(behavior)

Field(state, property)

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to OOP in Java

What is a Class ?

• A class is a blueprint or prototype from which objects are created

• In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to OOP in Java

What is Inheritance?

• Inheritance provides a powerful and natural mechanism for organizing and structuring your software

• Object-oriented programming allows classes to inherit commonly used state and behavior from other classes

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to OOP in Java

What is Interface?

• An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to OOP in Java

What is Package?• A package is a namespace for organizing classes and interfaces in a logical manner. Placing your

code into packages makes large software projects easier to manage

Use Import to use package

© 2010 LogiGear Corporation. All Rights Reserved

Introduction to TestNG

• TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use

• TestNG is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc...

• TestNG features: – Annotations.

–Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).

–Test that your code is multithread safe.

–Flexible test configuration.

–Support for data-driven testing (with @DataProvider).

– Support for parameters.

– Powerful execution model (no more TestSuite).

– Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).

– Embeds BeanShell for further flexibility.

–Default JDK functions for runtime and logging (no dependencies).

–Dependent methods for application server testing.

© 2010 LogiGear Corporation. All Rights Reserved

Setup TestNG in Selenium

• Download and install eclipse IDE from http://www.eclipse.org/downloads/

• Run eclipse, Go to Help/ Install New Software…

• Install TestNG by copy this url into Work with textbox http://beust.com/eclipse

• Check TestNG• Click Next and do as the

instructions

© 2010 LogiGear Corporation. All Rights Reserved

Setup TestNG in Selenium

• Go to http://seleniumhq.org/download/ and download Selenium RC

• Extract the downloaded file.

© 2010 LogiGear Corporation. All Rights Reserved

Create Project

• Open Eclipse, Go to File/New/Project to create new project

• In New Project windows, choose Java/ Java Project and click Next

© 2010 LogiGear Corporation. All Rights Reserved

Create Project

• Enter project name

• Choose Java Runtime Environment and click Next

© 2010 LogiGear Corporation. All Rights Reserved

Create Project

• In the New Java Project, click Add External JARs…

• Choose path to Selenium RC folder and add selenium-java-client-driver.jar and selenium-server.jar

© 2010 LogiGear Corporation. All Rights Reserved

Add TestNG Library

• To add TestNG Library, navigate to eclipse /plugins / org.testng.eclipse_5.14.3.20101203_1327/ and choose eclipse-testng.jar

© 2010 LogiGear Corporation. All Rights Reserved

Configuration Report output folder

• Go to Window/ Preferences

• Change Output directory : /Reports

• Apply and click OK

© 2010 LogiGear Corporation. All Rights Reserved

Create Package

• Right click “src” folder• choose New/ Package

• Type name of the package

© 2010 LogiGear Corporation. All Rights Reserved

Project structure

• Create all package as the following template

• BatchFiles: contain test suite file testng.xml

• Database: contain data file in excel format

• Functions: Contain java class of some common functions

• Interfaces:

• TestCases: Contain the test case

• TestCases.[name]: the sub folder of the package, contain the test cases relating to a module.

© 2010 LogiGear Corporation. All Rights Reserved

Run Configuration

• Go to Run/ Run Configuration…

© 2010 LogiGear Corporation. All Rights Reserved

Run Configuration

• Browse for file and run with below choices:

• Class: Run a Java Class that contain one or more methods

• Method: Run a single test method

• Groups: Run a group of test method

• Package: Run a Package of Class

• Suite: Run testng.xml file(Test Suite)

© 2010 LogiGear Corporation. All Rights Reserved

Exercise 1

• Setup Eclipse and install TestNG• Open Selenium IDE and record a simple google search test• Convert code into TestNG• Create a TestNG Project in Eclipse follow the framework• Copy created code in Selenium IDE into TestNG File• Edit and Run the test

© 2010 LogiGear Corporation. All Rights Reserved

Exercise 2

• Login Gmail using logigearmail@gmail.com / logigear1• Check for text present• Logout Gmail

• Login Yahoo Mail using logigearmail@yahoo.com / logigear1• Check for text present• Logout Yahoo Mail

© 2010 LogiGear Corporation. All Rights Reserved

Data-driven testing with Selenium and TestNG

• Download java excel Api from the following link http://sourceforge.net/projects/jexcelapi/files/jexcelapi/

• Extract and add file jxl.jar into TestNG Project Library

© 2010 LogiGear Corporation. All Rights Reserved

Data-driven testing with Selenium and TestNG

• Create a TestNG project in Eclipse follow the framework.• We will start with a simple Login Yahoo using data provider

© 2010 LogiGear Corporation. All Rights Reserved

Data-driven testing with Selenium and TestNG

• Create a excel file for example data.xls using this format and name of the sheet is datapool

testdata username passwordkimlgr1@yahoo.com logigearlogigearmail@yahoo.com logigear1

testdata

© 2010 LogiGear Corporation. All Rights Reserved

Data-driven testing with Selenium and TestNG

• Use this code to read file from excel file.

• Parameters

• xlFilePath: path to the excel file, in the example is “data.xls”

• sheetName: name of the sheet, in the above example is “datapool”

• tableName: name of the data table, in the above example is “testdata”

© 2010 LogiGear Corporation. All Rights Reserved

Data-driven testing with Selenium and TestNG

• Write a DataProvider name DP1 and call the getTableArray function, pass the parameter

• Edit the LoginYahoo method to receive the table data

Name of the data provider

Table column name

Pass parameters

© 2010 LogiGear Corporation. All Rights Reserved

Exercise 3 – Data Driven Test

• Let’s take our simple first example, but this time, instead of having a static search string, we are going to go to multiple sites and verify that the site is what comes up

• So, in this example, we need to create a spreadsheet or data for the IDE

• In the spreadsheet, we will create four sites, yahoo, google, msn, and ask

• The test will go to each site and verify an attribute on the page that identifies it as the site – for example, we might use the window tag as verification that we have gone to Yahoo, or we can use different verification methods to insure that we are at the right site

• The test will then loop through each of the data elements, go to the site and then verify through verification the site

© 2010 LogiGear Corporation. All Rights Reserved

Exercise 4

• In this exercise we are going to do a couple of things• The exercise should be a simple test, another google search for

example, but this time we are going to add active timing and look at javascript

• The active timing should have two examples, one sleep and one dynamic wait

• We want to record the script and then go inside the code to add the timing, in the first example, we use a static sleep and in the second exercise, we add a dynamic sleep.

• I believe the method is called either click or clickAndwait method, one waits, while the other goes on

• The select command and click commands are some of the basic building blocks for IDE, and we need to show in this exercise how to use these but more importantly, we want to give the tester a view into the code that is being generated by the recorder

• http://seleniumhq.org/docs/03_selenium_ide.html#building-test-cases

© 2010 LogiGear Corporation. All Rights Reserved

The End

Thank you for your attention!

If you have any question/concern/unclear/suggest, please contact to us:

o Hieu Pham : hieu.pham@logigear.como Dung Huynh : dung.huynh@yahoo.como Tuan Tran : tuan.quoc.tran@logigear.com

top related