All about WebDriver: Methods And Its Usages

Hello Folks,

I am here again with new topics. In this post, we will learn all basic methods of WebDriver and its usage. The main purpose of this post is to make you familiar of methods of WebDriver. We will see in details as well in upcoming posts.

1. How to open a URL in browser?

Concept: To open a URL, we use get(String URL) method of WebDriver.

Syntax:

2. How to get currently opened URL in browser?

Concept: To get current url , we use getCurrentUrl() method which returns a String.

Syntax:

3. How to get page source of currently loaded page in browser?

Concept: To get page source of currently loaded page in browser, we use getPageSource() method which returns a String.

Syntax:

4. How to get title of currently loaded page in browser?

Concept: A page title, or title tag, is the main text that describes a web page. To get title of currently loaded page in browser, we use getTitle() method which return a String.

Syntax:

5. How to get handle of currently focused window?

Concept: A window handle is a unique identifier that Windows assigns to each window created. To get handle of currently focused window, we use getWindowHandle() which returns a String.

Syntax:

6. How to get handles of all Windows opened by current session?

Concept: To get handles of all window opened by current session, we use getWindowHandles(). It returns a Set of String objects. Set is a collection interface which does not allow duplicates. To get unique handles only, it returns Set.

Syntax:

7. How to close only currently focused window?

Concept: To close currently focused window, we use close() method.

Syntax:

8. How to close all windows opened by current session?

Concept: To close all windows opened by current session, we use quit() method.

Syntax:

9. How to locate an element in a currently loaded page?

Concept: By using findElement(By locator) method.

Syntax:

10. How to locate multiple similar type of elements in a web page?

Concept: By using findElements(By locator) method. It returns a List. List allows duplicates and keeps insertion order.

Syntax:

Note: When we use findElement() method and it finds more than one element for the same locator, it returns first element found as a result. Internally, findElement() also calls the findElements() method and when findElements() return a List of Webelements, findElement() takes get(0) i.e first element in List and returns.

11. How to do window full screen?

Concept: By using fullScreen() method.

Syntax:

12. How to maximize window?

Concept:By using maximize() method.

Syntax:

13. How to get position of window?

Concept:

Syntax: It is values of X co-ordinates and Y co-ordinates.

14. How to get co-ordinates of window?

Concept: By using getX() and getY() methods.

Syntax:

15. How to get size of current window?

Concept: By using getSize() method.

Syntax:

16. How to get width and height of window?

Concept: Using getWidth() and getheight() method.

Syntax:

17. How to set position of window?

Concept: Using setPosition() method which takes arguments of type Point.

Syntax:

18. How to set size of window?

Concept: Using setSize() method which takes arguments of type Dimension.

Syntax:

19. How to delete all cookies of current domain?

Concept: Using deleteAllCookies().

Syntax:

20. How to delete specific cookies?

Concept: Using deleteCookieNamed().

Syntax:

21. How to set implicit wait?

Concept: Using implicitWait() method.

Syntax:

22. How to set page load time?

Concept: By using pageLoadTime() method. When time is up, it will give exception.

Syntax:

23. How to set script time out?

Concept: It is time allowed for a script to run. It will throw exception if script time is over and execution is still in progress.

Syntax:

24. How to open URL using navigate?

Concept: We can use to() method of Navigation package as well. Internally it calls the same get() method.

Syntax:

25.  How to navigate to previously opened web page?

Concept: by using back() method.

Syntax:

26. How to navigate to next page?

Concept: Using forward() method.

Syntax:

27. How to refresh a web page?

Concept: Using refresh() method.

Syntax:

28. How to switch to an alert?

Concept: By using switchTo() method.

Syntax:

29. How to switch to default content?

Concept: By using defaultContent() method.

Syntax:

30. How to switch to a frame using frame index?

Concept: By using frame(int index) method.

Syntax:

31. How to switch to a frame using frame name/id?

Concept: By using frame(String name/id) method.

Syntax:

32. How to switch to a frame using frame its previously located element?

Concept: By using frame(WebElement element) method.

Syntax:

33. How to chnage focus to parent context?

Concpet: using parentFrame() method.

Syntax:

34. You are given a driver and how you will know driver belongs to which browser?

Syntax:

 

Quite a long post. This post is  just to make you familiar because in upcoming posts, if I use it in upcoming posts, you will not be strange to basic methods.. I will cover major concepts separately in upcoming posts.

If you like this post, please like, comment, share and give feedback.

#HappyLearning

#Thanks

Author: Amod Mahajan