Jump to content

Selenium Webdriver Methods With Examples-Layman Learning


laymanlearning

Recommended Posts

Selenium WebDriver methods with Examples

Selenium ? Selenium is an open source, robust set of tools that supports rapid development of test automation for web-based applications.

 

Layman Learning is now providing globally Online IT Training from Hyderabad,India by the real time IT ... Corporate Training. ... the best of IT support services. www.laymanlearning.com

 

Support; Query? +91-741-626-7887 ... Online Corporate IT training services. OnlineTraining | Corporate Training | Layman Learning.

[email protected]

 

 

Selenium WebDriver methods with Examples

1.Browser Back and Forward (NAVIGATION)

Steps to implement Browser back and forward through Selenium Web Driver

1. Create Driver for any Browser(Mozilla)

2. Go to the URL

3. Navigate to some page in website.

4. Use Selenium code to Navigate Back to Main Page.

CODE: driver.navigate().back();

           driver.navigate().forward();

 

------------------------------------------------------------------------------------------------------------------------------

2.Handling DRAG and DROP

 

Steps to Handle Drag and Drop through Selenium Web Driver

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL

3. Create an Action object for Driver

4. Fetch and create WebElement object for the SOURCE element.

5. Fetch  and create WebElement object for the DESTINATION element.

6.Perform ACTION

  1.Click and Hold the source WebElement

  2.Move to destination WebElement

  3.Release the Element.

----------------

3.Making Single Select in Drop down (Option List)

Steps to make Single Select in Drop down through Selenium Web Driver.

1. Create Driver for any Browser(Mozilla)
2. Go to the URL
3. Fetch the Drop Down element and create an object as WebElement.
4. Create an Select object for the Drop Down Element object.
5. Create a List and collect all Options through Select Object.
6. Create a Iterator object for the List.
7. Get the size of the List.
8. Loop through and check for required element.


------------------------------------------------------------------------------------------------------------------------------

4.Making Single Select in Drop down (By INDEX, VALUE, TEXT)

Steps to make Single Select in Drop down through Selenium Web Driver.

1. Create Driver for any Browser(Mozilla)
2. Go to the URL
3. Fetch the Drop Down element and create an object as WebElement.
4. Convert the Drop Down Element in to Select object.
5. Select by INDEX
6. Select by VALUE
7. Select by VISIBLE TEXT
 

------------------------------------------------------------------------------------------------------------------------------
 

6.Multiple Select List Box Window - DESELECT

Steps to make Deselect in Drop down through Selenium Web Driver.

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL

3. Fetch the Drop Down element and create an object as WebElement.

4. Convert the Drop Down Element in to Select object.

5. Select by Index(Start index) 

6. Select by Index(End index)

 

 

------------------------------------------------------------------------------------------------------------------------------
 

7.iFRAMES - How to handle Frames in Web Driver

Steps to get Source of each iFrame through Selenium Web Driver.

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Make a List containing FRAME web elements of a Web Page.
4. Get the Size of Frames.
5. Loop though and print the Source of each Frame

------------------------------------------------------------------------------------------------------------------------------

 

8.iFRAMES - How to perform action in Frames

Steps to perform Action in iFrame through Selenium Web Driver.

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Fetch iFrame element and create an Web Element object.
4. Using iFrame Web Element object, switch to IFrame.
5. Perform SendKeys/ Click action in iFrame.
 

------------------------------------------------------------------------------------------------------------------------------

 

9.iFRAMES - How to switch to a perticular Frame through index

Steps to switch to perticular iFrame by index through Selenium Web Driver.

 

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Make a List containing FRAME web elements of a Web Page.
4. Get the Size of Frames.
5. Switch to required iFrame through index.

------------------------------------------------------------------------------------------------------------------------------

TABS/New Window - 2

When two browsers are opened and Web Driver need to shift the control from Parent Window to Child Window. 

Please follow the steps mentioned below.
 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Collect Window Handles through Set
4. Create an iterator to iterate through Window Handles.
5. Increment the iterator and store the Window Handle as Parent.
6. Increment the iterator and store next Window Handle as Child.
7. Switch to Child Browser using Child Window Handle.


------------------------------------------------------------------------------------------------------------------------------

TABS/New Window - 3

When second browser is closed/you close it and Web Driver need to shift the control from Child Window to Parent Window. 

Please follow the steps mentioned below.
 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Collect Window Handles through Set
4. Create an iterator to iterate through Window Handles.
5. Increment the iterator and store the Window Handle as Parent.
6. Increment the iterator and store next Window Handle as Child.
7. Switch to Child Browser using Child Window Handle.
8. When Child browser get closed, Switch from Child browser to Parent Window.

------------------------------------------------------------------------------------------------------------------------------

 

 

11. CALENDAR popups

Calendar PopUp - 1

 

Normal Calender(current month) Popup can be handled in the following way.

 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Fetch the Calender element and click to open.
4. Fetch the required date through xpath and click.


------------------------------------------------------------------------------------------------------------------------------

Calendar PopUp - 2 (Customized wait)

 In a Calender if we want to click on future month which is not currently displayed, we need to click on next link until we get the required month.
           This can be done by writing Customized wait. Check for particular date element in each month, if not found move to next month.


------------------------------------------------------------------------------------------------------------------------------
 

12. Drop Down MENU

 

  In order to click on an menu item, first we need to move the mouse over Parent menu, later we can click on any of the Menu child item.

Please follow the steps mentioned below.
 

1. Create Driver for any Browser(Mozilla)

2. Go to the URL
3. Fetch the MENU Parent element and create a WebElement object.
4. Create an Action object for Driver
5. Through Action object, move to Parent element.
6. Give a Delay for menu items to be displayed.
7. Fetch the Child item through xpath and Click on it.
 

 

------------------------------------------------------------------------------------------------------------------------------

 

 

13. Context Click (Right Click)

We can use keyboard keys to Make a Right Click.

 

Please follow the steps mentioned below.
 

1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. Fetch the MENU Parent element and create a WebElement object.
4. Create an Action Object for Driver.
5. Through Action Object, make a Context Click on Menu Parent object.
6. Through Action Object, send keys for ARROW_DOWN/ARROW_UP/Keys.ENTER.

------------------------------------------------------------------------------------------------------------------------------
 

14. JAVA SCRIPT example

We can use java script command to perform actions.

We can write code to fill up the text box through java script.

 

Please follow the steps mentioned below.

1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. Create Java Script executor object for the Driver.
4. Store the Java Script command in a String Variable.
5. Java Script Executor object executes the command in the Variable.
 

------------------------------------------------------------------------------------------------------------------------------

 

15. Multiple Elements

We can count the number of links present in the page. We can also print the link text of each Web link.


Please follow the steps mentioned below.

1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. Fetch elements with tag //a in the entire page, store it in a List.
4. Get the count of Links present in the Page.
5. Loop through the links and print the Attributes 


------------------------------------------------------------------------------------------------------------------------------

16. Other Browser (Internet Explorer)

 

Using Internet Explorer with Web Driver.

Please follow the steps mentioned below.
1. Set System Property for the Driver and give path of the IE Driver.
2. Create an Web Driver Object.
3. Open an URL
 

 

------------------------------------------------------------------------------------------------------------------------------
 

17. Other Browser (Chrome)

 

Using Chrome with Web Driver.

Please follow the steps mentioned below.

1. Set System Property for the Driver and give path of the Chrome Driver.
2. Create an Web Driver Object.
3. Open an URL

------------------------------------------------------------------------------------------------------------------------------
 

18. PROXY settings.

Please follow the steps mentioned below.

 

1. Import Selenium.Proxy

2. Create a Profile object for Firefox

3. Create a string variable with value.

4. Create a Proxy object.

5. Set the values through proxy.

6. Set the proxy preference to proxy object using profile object.

7. Pass the profile object to Firefox Driver.

 

 

------------------------------------------------------------------------------------------------------------------------------

19. Page Onload authentication

Sometimes when you are Automating Web pages, you may come across Page onload Authentication window. This window is not java popup/div. It is windows popup. Selenium directly cannot handle this windows popup.

Hence we use Autoit sowftware tool. Through Selenium we can handle this situation using Autoit.

 

Please follow the steps mentioned below.

1.Download Autoit from the following URl

  ( )

2.Install downloaded software.

3. Open Script Editor

   Start=>ProgramFiles=>AutoIt =>SciTE Script Editor.

4.Open Object Identifier.

  Start=>ProgramFiles=>AutoIt =>AutoIt Window Info.

5.Drag and Drop finder tool in AutoIt Window Info, to the Window you need to     identify.

6.Collect the Title Name of window from (AutoIt Window Info.)

7.Write the Script in the Editor.

----------------------------------------------------AUTOIT CODE-----------------------------------------------------

 

WinWaitActive("Authentication Required")

Send("admin")

Send("{TAB} admin{TAB} {ENTER}")

 

------------------------------------------------------------------------------------------------------------------------------

8.Save the file as default save.(Authentication1.exe)

9.RUN/Compile the SCRIPT, it creates an exe.

10.Mention the exe path in the Program before creation of Driver.

 

 

 

------------------------------------------------------------------------------------------------------------------------------

20. File Download

Please follow the steps mentioned below.
 

1. Create a PROFILE object of Browser.
2. Set Preference, by giving Download destination Directory.
3. Set Preference, by giving Default Folder. 0 => Desktop, 1=>System Default Location, 2 => Indicates a custom Folder Location
4. Set Preference, A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.

After coding the above mentioned steps, now start the driver and click on Download button/link.
1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. Fetch the Download web element and click.
 

 

------------------------------------------------------------------------------------------------------------------------------

21. File Upload

Please follow the steps mentioned below.
 

1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. Store the Source path of file in a variable.
4. Fetch the Upload web element text box and give path using variable.
5. Fetch the upload button and click
 

------------------------------------------------------------------------------------------------------------------------------
 

22. Handling JAVA ALERT

Sometimes you may get alerts as anticipated(through Insert/update/delete operation in database). These may be JAVA alerts.
Please follow the steps mentioned below to handle  Alerts.
 

1. Create Driver for any Browser(Mozilla).

2. Go to the URL.
3. You get an alert asking to click on 'YES' or 'NO' button.
4. First Confirm if it is JAVA alert window.
5. Write a code to switch the control to Alert window.
6. In the Alert window, either ACCEPT by clicking on 'YES'
    or CANCEL by clicking on 'NO'.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...