Selenium

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 56

Selenium

Automation tool
Selenium syllabus
 SELENIUM COURSE CONTENTS:
 Automation Testing
 Test Scripts
 Selenium IDE
 Web Elements
 Locators
 HTML
 Xpath
 Handling Listbox
 Find element & Find Elements
 Popup’s
 Java script Popup
 File Upload Popup
 File Download Popup
 New Browser Popup
 Tabbed Browser Popup
 Calendar Popup
 Hidden Division Popup
 Handling Frame
 Mouse Over Operations
 Drag & Drop Operations
 Working with different browser
 Taking Screenshot
 Auto Suggestion
 POI Apache
 Data Driven Framework
 TestNG Framework
 Hybrid Framework
History of selenium
 Jason released the first version of automation
tool called selenium core but it is not read
properly, than he launched 2nd version of tool
that selenium IDE(SIDE) which are also failed.
 Than the developed 3rd version with java code

i.e. selenium RC which was not working


properly, than he decided to do with WEB
then he released 4th version WebDriver with
java.
What is selenium
 Selenium is the free and open source tool,
which is used to automate only on web based
applications
 Selenium components are

◦ Selenium core
◦ Selenium IDE
◦ Selenium RC(remote control)
◦ Selenium web driver
◦ Selenium Grid
Selenium supporting languages
 Selenium supports the following languages
◦ C#
◦ PHP
◦ Pearl
◦ Python
◦ Ruby
◦ Java
◦ Java script
What is selenium web driver
 Selenium is a combination of selenium RC
and Web driver
 It is used to automate the web based

applications developed using java language


 Selenium web driver has three releases

◦ Selenium 2
◦ Selenium 3
◦ Selenium 4
How to Setup the
Selenium Web Driver
Java development tools
 Eclipse
 Netbeens
 Jcreater
 Intelli.j (IDBA)
 Dr.java
 Mgeclipse
 J developer
Eclipse versions
 Auslin
 Bravo
 Callisto
 Europa
 Ganymede
 Galileo
 Helios
 Indigo
 Juno
 Keplar
 Luna
 Mars
We can use any one of those five versions
 Neon for selenium
 Oxygen
Selenium Web driver architecture

Web Driver(I)

Remote Web
Driver(c)

Internet
Firefox Chrome
explorer
Driver(c) Driver(C)
driver(C)
• When ever we use predefined interfaces and classes every word first
letter should be in upper case
• Import statement should be written when ever we use predefined
classes and interfaces
• To write import statement write the class name or interface name
mouse move on the class click on import statement or we can press
ctrl +shift+O
1. How to start browser and how to open the
Google.com
Get Title method
 In order to get the title of the app we use get
Title method and it returns the title of the
application in string format
 Ex:
Wait statement
 Thread. Sleep statement is used to wait for
specified milliseconds
 EX 1000 msec =1 sec
 Sleep method is a static method of thread

class which takes msec arguments


List of Web element
 Tex box
 Check box
 Button
 Password text box
 Radio button
 Drop down or list box
 Sing select list box
 Multi select list box
 Link
 Image
HTML
 HTML: hyper text markup language, which is
used to develop the web element
HTML format
 <html>
 <header>
 “Css”
 “Title”
 </header>
 <body>
 “elements”
 </body>
 </html>
Attribute

<input type = “text” > </input>

Opening tag

Property name : type


Property value : text
Tag name: input
Fire bug
 In order to write the code to perform operation
on the element, we should see the source code
of the element, if we right click on the web
page and select view page source it will display
the complete source of the web page it will be
very difficult for us to get the HTML code or
required element and using inspect element we
can’t see clear code of element because of this
reason, we use a tool called firebug, which is a
add-ON in the Mozilla Firefox browser.
 Installation Steps
Locators
 Locators is an Attribute, Tag name, Text of
element and Expression, which are used by
selenium to identify the element
 Selenium supports following locators
 Name Attributes
 ID
 Class name
Text of the element
 Link text
 Partial link text
expression
 Css selector
 Xpath
HTML Tag name
 Tag name
Compound class name
 If space present in between the class value,
class name locator can’t identify the element.
It throws error during run time
Css Selector
 If name, id, class name are not present in the
HTML code we use css selector locator
 Css  Cascading Style Sheet

 Syntax
◦ Html Tagname[propertyname=‘property value’]
◦ When we need to use the id attribute in css
 Html tagname#id attribute value
◦ When we need to use the class attribute in css
 Html tagname.class attribute value
 Ex:
◦ Input[type=‘submit’]
Xpath
 Xpath in selenium is a xml path used for
navigation through the HTML structure of the
page.
 it is a syntax or language for finding any

element on a web page using xml path


expression.
 xpath can be used for both HTML and XML

documents to find the location of any


element on a web page using HTML DOM
structure.
 Types:
 Absolute Xpath

Absolute Xpath
 It is the direct way to find the element, but
the disadvantage of the absolute xpath is that
if there are any changes made in the path of
the element then that xpath gets failed.
 The key characteristic of xpath is that it

begins with the single forward slash(/) which


means you can select the element from the
root node.
 Ex:
Relative Xpath
 Relativ Xpath starts from the middle of HTML dom
structure. It starts with double forward slash(//).
 It can search elements anywhere on the webpage, means
no need to write a long xpath and you can start form the
middle of html dom structure.
 Relative xpath is always prefereed as it is not a complete
path from the root element.
 Type:
◦ Xpath with attribute
◦ //tagname[@propertyname= ‘propertyvalue’]
◦ Xpath with contains and attribute
◦ //tagname[contains(@propertyname,’propertyvalue’)]
◦ Xpath with text() method

◦ //tagname[text()=‘text of an element’]
◦ Xpath with contains and text().
Different types of browser command

 Get command
 Get title Command
 Get current URL command
 Close command
 Quit command
 Navigation command

◦ Navigate back command


◦ Navigate forward command
◦ Navigate to command
 Refresh command
Get Command
 It is used to open the application , which
takes URL of the application argument
 Ex: driver.get(“URL of the application”);
Get Title command
 It is used to get the Title of the application

Ex:System.out.println(driver.getTitle());
Get current URL command
 It is used to get the URL of the current Web
page

 EX:
Close and quit command
 Close
◦ It is used to close the current window
◦ driver.close();

 Quit
◦ It is also used to close the browser
◦ driver.quit();
Navigation command
 Navigate to previous web page
◦ driver.navigate().back();

 Navigate to the next web page


◦ driver.navigate().forward();

 Navigate To Command
◦ It is used to open the application
◦ Driver.navigate().to(“url”);
 Refresh command
◦ It is used to reload the web page
◦ driver.navigate().refresh();

 WebElement Methods
◦ 1.SendKeys() Method
 It is used to enter the value to the text box, password
text box and multiline text box
◦ 2. Clear method
 It is used to clear the existing value in the text box
 Click method: click()
◦ This method is used to click on the element such as
button, link, check box radio button
 isSelected() method
◦ isSelected() is used to verify whether the specified
radio button or check box is selected or unselected,
if it is selected it returns true value else returns
false value
Escape sequence
 A character preceded by a back slash(\) is an
escape sequence and has special meaning to the
compiler.
 \t insert a tab in the text at this point
 \b inset a backspace in the text at this point
 \n inset newline in the text at this point
 \’ insert a single quote character in the text at
this point
 \” insert double quote character in the text at
this point
 \\  insert a back slash
Web Element type
 Web element type is used to store the
element, it is a return type of find element
type method
 We use web element type to perform multiple

operation on single element the advantages


of web element type is every time we no need
to write findElement() for specified element
Keys enumerations
 It is used to perform key board operations,
which is used only inside the sendKeys()
method
 Keys enumeration has all the keys except A-Z
 A-Z letters directly specified in double

quotes.
Scroll down operation
It is done using keys enumeration operation
 Keys.ARROW-DOWN
 Keys.END
 Key.HOME
Handling list box
 Steps to perform operations on list box
◦ Find the dropdown list using findElement() method
◦ Create instance of select class by specifying the above
dropdown list elements as a argument for select class
constructor
◦ Select the required options present in dropdown list by calling
any on of the following methods
 SelectByIndex(index)
 This method is use to select the required option based on index which
is sequential number, which starts from 0
 Select ByValue(value)
 This method is used to select the required option based on attribute
value
 SelectByVisibleText(String)
 This method is used to select the required option based on the actual
name of the option which is displayed in the application
 Write a selenium code to count total number
of options present in the list box
◦ List<webElement>l1=s1.getOptions();
◦ Get options method used to get all the options
from the list box and all list box elements are
stored in List<WebElement>l1
◦ L1.size() method used to find total no.of options
present in the list box
◦ List<WebElement> used to store group of elements
 Write a selenium code to print the number of
option present in the list box
 Write the selenium code to print last options in
the list box
 Write a selenium code to print current value in
the list box
◦ getFirstSelectedOption();
 This method used to get the current option from the list
box and current option is stored in the web element
reference variable to get name of the option from web
element we use get text() method
 Makemytrip
◦ Get the current value from the list box
 Justrechargeit.com
◦ Print all the options in the list box
 http://www.mymarriage.com/
◦ Find total no.of options present in education list
box
 Face book
◦ Print the last year in the list box
Handling the multi select list box
 In order to select the required option in the
multi select list box we use methods of select
class it will keep already selected option as it
is and it will select the newly specified option
also
◦ In order to un select the option we use following
methods
 deselectByIndex(index)
 deselectBYvalue(value)
 deselectByVisibleText(text)
Actions class
 Handling special keyboard and mouse events are
done using advanced user interactions api. It
contains the actions class that is needed which
executing these events. The following are the
most commonly used key boards and mouse
events provided by the actions class
 Using actions class we can perform following the
operations
◦ Mouse move operations
◦ Drag and drop operations
◦ Right click operation
◦ Double click operation
Drag and drop operation
 Actions class.DragANDDrop(from element, to
element ).build().perform();
 DragAndDrop() method used to drag source

element and place on the target element


 DragAndDrop() method takes two argument,

source and target which is present in actions


class
 EX:
Composite methods
 Performing single operations using multiple
methods is called as composite methods
 Write a selenium code to perform drag and

drop operations without using drag and drop


method
 Write the selenium code to move the

elements in deferent directions


Right click operation()
 In order to right click we use contextclick(),
which takes web element reference variable
argument(e1)
 Context click()Method will display the context

menu, in order to select required option


present on the context menu we use keys
enumerations or shot cute keys using send
keys() method
Handling popup’s
 In order to perform one required type of
operation on the popup, 1st we should
identify the type of the popup we can
categories the popups into following
categories
◦ Alert and confirmation popup
◦ File upload popup
◦ Web notification popup
◦ Download popup
◦ Calendar popup
Alert and confirmation popup
 Alert and confirmation popup will have ok button
and cancel button and alert message and where as
alert popup will have only Ok button and alert
message
 Alert and confirmation popup and alert popup are
called as java script popup
 Driver. SwitchTo.Alert() method used to change
controls from browser to alert and confirmation
popup in order to click on ok button on the alert
popup we use accept() method, to click on cancel
button we use dismiss() method
 To get alert message we use get text() method.
File upload popup
 File uploading by using robot class
◦ Robot r1=new Robot();

 Note: in file upload popup the popup window


is window based application we can’t able to
use selenium for window based application so
we can using sendKeys Method inside the
sendKeys we are using the path of the file
Robot class
 It is used to perform Keyboard operation
 Note: by using java we can perform window

based popup operation for upload file by


robot class.
◦ keyPress() method
◦ KeyRelease() method
 In order to upload the file using sendKeys()
method Or Robot class
 sendKeys() method used to upload the file by

specifying the file path name


Web notification popup
 When ever we work in some application web notification
popup appears on the web page
 In order to display the popup we have change true into
false for
“dom.webnotification.enable.preference.preference name”
 Using set preference() method at firefox profile class set
preference() takes 2 argument
 Keys.preference name
 Value. Preference value(true/false)
 In order to copy preference name type about configure
then press enter
 Firefox profile f1= new Firfox Profile();
 f1.set preference (“dom.webnotifications.enabled;false”)
File download popup
 Web driver can’t perform any operation on
file download popup in order to handle file
download popup we programmatically change
the settings called preference of the firefox
profile which takes mime type (multipurpose
internet mail extension)
browser.helperApps.neverAsk.
saveToDisk
 FirefoxProfile p1 = new FirefoxProfile();
 p1.setPreference("browser.helperApps.neverAsk.saveToDisk","ap
plication/zip");
 System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\
Downloads\\geckodriver-v0.24.0-win32\\geckodriver.exe");
 DesiredCapabilities dc = new DesiredCapabilities();
 dc.setCapability("marionatte", false);
 FirefoxOptions opt = new FirefoxOptions(dc);

 opt.setProfile(p1);
 WebDriver driver = new FirefoxDriver(opt);
 driver.get("https://www.seleniumhq.org/download/");
 driver.manage().window().maximize();
 driver.findElement(By.linkText("3.141.59")).click();

You might also like