Selenium Tutorial from beganing...

Page 1

m


Y

m a a Y m a m m a a m a a m a m a m ! a m ! "m m a

# m

a m $ % a m m m a ! m a

a m m a m

a m &

a '( a m )


รท m &

& * รท m

* รท m + , - . '+ - + / 0 * รท m ) & &

m ' & *


Y

m

รท Y +

m

&&&* * m & 1 2 3

& 1 ' & '+ 244 * 4 & 4

!

2 !

' & '+ 244 * 4

4

' &

' &


m 5 m ÷ Object Identification: ÷ Faster: Selenium can identify objects using xpath, So it can directly go to a specific object ÷ Slow: Silk test and QTP follows a hierarchy for locating objects.

÷ mcripting Language: ÷ QTP allow users to write script in VB script only. ÷ Silk test has proprietary scripting language 4TL. ( 4 Test Language ) ÷ But Selenium allows writing script in Java, Ruby, Perl, PHP, C# and Python.

÷ 2rice : ÷ Selenium is an open source tool. ÷ Silk and QTP are not free tools ÷ QTP license cost is around $7500 , Silk test license cost is around $8500 USD


÷ esktop applications: ÷ Ye can¶t automate desktop applications using Selenium. ÷ Ye can automate using QTP and Silk test.

Object Repository: Capturing objects properties is a tough task as we need to prepare the object repository.


m รท ย t has support for multiple operating systems like Yindows, Linux and

Mac รท Cross browser testing can be done easily on browsers like ย , Firefox,

Safari, Netscape etc. รท ย t supports multiple programming Languages like C#, Java, Ruby,

Python .. etc รท ย t can be used for automating Ajax and swift web pages. รท ย t is easy to use, if are familiar with any one of supported programming

language รท ย t is easy to learn รท ย t is free-open source tool รท ย t has matured community forums to get clarifications


Selenium is majorly divided into four components รท Selenium COR รท Selenium ย D รท Selenium RC(Remote Control) รท Selenium Grid


m ÷ Selenium Core is a test tool for web applications. ÷ Selenium Core tests run directly in a browser. ÷ And they run in nternet xplorer, Mozilla Firefox on Yindows, Linux, and Macintosh. ÷ This is deprecated with the advent of Selenium D and Selenium RC. ÷ nitially D has capability for recording test cases only. ÷ n earlier stages executed recorded script using Test runner. ÷ Later D introduced with the execution facility. isadvantages: ÷ Ye cannot record scripts using this. ÷ Ye can run scripts in only HTML format using Test runner.html


m รท Download the Selenium Core zip file (selenium-core1.0.1.zip) and extract the files into a folder. รท Open Command Prompt and then navigate to the path where the files are extracted


m $ %


m $ % á A new ³Selenium Functional Test Runner´ window will be opened.

á Ye need to enter the test suite path in ³Test Suite´ edit box on the left hand side . á Click on ³Go´ and then the screen window changes with the commands.


m $ %

á Now click on ³Run´ option on the right hand side of the window

so that the test case executes successfully.


m ! D means integrated development environment, which is used for record and run the scripts. ÷ Selenium D is add-on for ³Mozilla Firefox´. ÷ The recorded scripts in Selenium D can be viewed in the following supported languages: 1) HTML 2) Java 3) C# 4) P RL 5) Python 6) Ruby 7) PHP ÷

The recorded scripts in Selenium D can be run against other browsers like , Safari, Google, Chrome and Opera by using Selenium RC.

÷

By default the script that is recorded should be saved with the .html extension.

÷ t will keep the user actions as they are performed and store them as a reusable script to play back. ÷ t supports only for Firefox. ÷ Using Selenium D we can record, save, execute and debug the scripts ÷ t can¶t support for Database verifications rror handling and Customize reporting.


m ! The script is saved as .html file so the run options are active Observations: รท Table is active here because the extension is .html รท Run Options are active and we can run the test case now.


m ! "m

m

These are the statements generated during record from Â?D


m ! "0 Observations: รท Table is not available because the format of the script is changed to Java. รท Run options are graded out because the default format for Selenium ย D is HTML.


m ! $ %


# Open Firefox and application you want to test Launch Selenium ย D using tools-Selenium ย D By default, you should be in the recording mode, but confirm it by observing the Red button. By default it will be in the HTML format. Otherwise, go to Options-Format-Select HTML Format. Record some actions and make sure that these are coming on Selenium ย D . During recording if you right click on any element it will show all the selenium commands available. รท You can also edit existing command, by selecting it and editing on the boxes available. รท You can also insert/delete commands by choosing appropriate option after right clicking. รท Choose appropriate run option - i.e walk, run or test runner and review your results. รท The recorded script can be viewed in two options: 1) Table 2) Source รท ย n Table, we can see the script in tabular format. รท ย n Source, we can see the script in html code. รท ย f we want to see the recorded script in other selenium supported languages, Go to Options, then select Format select the desired language. ย rawback รท Biggest drawback of Selenium ย D is its limitation in terms of the browser support (only Mozilla Firefox). รท รท รท รท รท รท


m ÷ Selenium RC comes in two parts i.e. Server and Client libraries ÷ Selenium-RC software comes with seven folders and we need to use

these files to configure RC with the language chosen. 1) selenium-server-1.0.3 -Configuration files for launching the RC server 2) selenium-java-client-driver-1.0.1- Java supported RC files 3) selenium-perl-client-driver-1.0.1-Perl supported RC files 4) selenium-php-client-driver-1.0.1-PHP supported RC files 5) selenium-python-client-driver-1.0.1-Python supported RC files 6) selenium-ruby-client-driver-1.0.1-Ruby supported RC files 7) selenium-dotnet-client-driver-1.0.1-C# supported RC files

÷ Selenium RC uses a server called ³Jetty Server´ for launching browsers


m $ % รท Selenium RC is an Open Source platform and supports only webapplications for automation. รท Ye can develop scripts using any popular programming languages รท Using Selenium RC with a high-level programming language allows us to query database, email test results and use iteration statements to navigate through the result set รท Using RC we can execute scripts in different browsers and platforms


m m m 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. á Command to start server is á java -jar selenium-server.jar

á Command to stop selenium server á http://localhost:4444/selenium-server/driver/?cmd=shutDown


m m m $ % á Ye can do this in our test code using the SeleniumServer class á Ye need to follow below steps á Create an object for SeleniumServer class á Use ³Start´ method for starting server. á User ³Stop´ method for stopping server.

á Sample code for Starting and Stopping selenium server. SeleniumServer server = new SeleniumServer(); server.start(); .. server.stop();

á Yith the server running, we may now use a client also. Selenium selenium = new DefaultSelenium( String seleniumServerHost, int seleniumServerPort, String browserType, String baseURL); selenium.open("http://www.somesite.com/somePage.html"); selenium.stop();


m m m $ %


! m á ˜efault melenium is class for creating browser reference á Variables used in the Default Selenium constructor are: á seleniummerverHost is the where the Selenium Server is running. i.e. hostname á seleniummerver2ort is the port on which the Selenium Server is listening. The default port is 4444. á browserType is the type of browser you want to use for testing. á

Common browser types are *firefox, *iexplore, *opera and etc.

á baseURL is the base URL for the site you are testing.

á Now, just opening a page isnœt all that useful. Ye need to interact with the page. To

do this, we use our selenium clientœs methods with locators. For example: á selnium.click("link=Text For Some Link"); á The string ³link=Text For Some Link´ is a locator.


รท

ย n Selenium, the objects are called as locators and they can be identified using Xpath, DOM or CSS.

รท

รท

6

+ 7 8

รท

3

+ ' #

รท

3 & # 2 * $6 7 8%9

# # *

44

* $6 7 8%9 * $6# 744 :; 71

# 1<8%9

44

44 #

* $6 7

* :=><8 %9

44 ! .

* $6 7 m 8%9

44

* $6 7 / 8%9

44

& '

# 6 '( 8*


m m import com.thoughtworks.selenium.*; // This is a package need to be include for using Selenium API public class SampleScript // This is Class name { public static void main( String args[] ) throws xception // This is main function { // This is the statement for Creating Browser Object DefaultSelenium Brw= new DefaultSelenium ("localhost",4444,"*chrome", "http://"); Brw.start(); // This statement starts the Browser Brw.open("www.google.com"); // This statement loads google.com url Brw.waitForPageToLoad("30000"); // This stmt waits till loading complete web page Brw.windowMaximize(); // This stmt maximizes browser window Brw.type("//input[@name='q']", "valuelabs"); // This stmt enters Valuelabs text in search field Brw.click("//input[@name='btnG']"); // This stmt clicks on google search button } }

//input[@name='qÂľ] Âą Object Â?dentifier for Search Field //input[@name='btnG'] Âą Object locator for Search Btn


m

Below are usefull commands in selenium รท open - opens a page using a URL รท capturemcreenshot - Captures a PNG screenshot to the specified file รท ย heck - Check a toggle-button (checkbox/radio) รท ย lick - Clicks on a link, button, checkbox or radio button รท ย lose ยฑ Clicks on the "close" button in the titlebar of a popup window or tab รท doubleย lick - Double clicks on a link, button, checkbox or radio button รท getEval - Gets the result of evaluating the specified JavaScript code รท getHtmlmource - Returns the entire HTML source รท getLocation - Gets the absolute URL of the current page รท getText - Gets the text of an element รท getTitle - Gets the title of the current page รท getValue - Gets the value of an input field รท goBack - clicks on the "back" button on their browser รท isย hecked - Gets whether a toggle-button (checkbox/radio) is checked รท isElement2resent - Verifies that the specified element is somewhere on the page รท isText2resent - Determines if the specified element is visible รท Refresh ยฑ Clicks on the "Refresh" button on their browser


m $ % รท รท รท รท รท รท รท รท

melect - Select an option from a drop-down selectFrame - Selects a frame within the current window selectWindow - Selects a popup window mtart - Launches the browser with a new Selenium session mtop - nds the test session, killing the browser Type - Sets the value of an input field Uncheck - Uncheck a toggle-button (checkbox/radio) windowMaximize - Resize currently selected window to take up the entire screen


m & รท 2 รท & &

m & & รท รท 0!? @*= * รท m 03 รท % " *( รท '% "( " " *( รท % ( *( รท % ( $ # %


รท '+ ' * # ? & * รท & ' & & +

* รท "AB &"Am 0 ( * รท ( 6 0 C 8


$ %

รท 63 8 ( + 0 & ' รท *


$ %

รท B & 0

+

& (

+ รท 0 m+ ' +"A " A


$ %

รท 63 # 03 C 8 ( ( *


$ %

B & (

+

& ( * D . ""A #

""Am #

""A ! '

& & * D 3 + 6 8*


$ %

รท B & m

+

& + + & #

+* รท

m !

+ ' EEEE$ %


$ %


'( + m & ' &

2 D ! ' ' # * D ' . F #* ˜

D ! & ' ' & '+ ' & 2 244&&&* ' ' *

4 D # & * D & + & ' ' & &*


! ' '

! '( & & & + '( *


! ' '

& ' 3 * G & + '( *


'

R

a ! & ' ' & '+ ' & 2 244 * F * 4 "Gm4 #4 4@,EH4 a # & * a & + & ' ' & &*


' $ %

a & '( + '( & ' + ' 2


' $ %


m ) m ) ' & & + * รท Y ' & * รท m )

+ & '" * รท & + + * รท m )

6m - '8 & + '+ m ) *


- ' . m ") + 2 รท m ) - ' ' + * รท m ) ' - '

*


m ) "m



4


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.