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:


// To open a URL
driver.get("https://www.flipkart.com/");

2. How to get currently opened URL in browser?

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

Syntax:


// To get currently opened URL in browser
String currentURL= driver.getCurrentUrl();
System.out.println("Currently opened URL is:"+currentURL);

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:


// To get html source of currently loaded page in browser
String pageSource= driver.getPageSource();
System.out.println("Page source of URL: "+ currentURL+" is: \n" +pageSource );

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:


// To get title of currently loaded page in browser
String CurrentTitle= driver.getTitle();
System.out.println("Title of currenlty loaded page: "+CurrentTitle);

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:


// To get handle of currently focused window
String CurrentlyFocusedWindow= driver.getWindowHandle();
System.out.println("Handle of Currently focused window: "+CurrentlyFocusedWindow);

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:


// To get handles of all windows opened by current session
Set HandlesOfAllWindowsOpenedInCurrentSession= driver.getWindowHandles();
System.out.println("Count of windows opened by current session: "+HandlesOfAllWindowsOpenedInCurrentSession.size());
System.out.println("hanles of all windows:");
for(String handle: HandlesOfAllWindowsOpenedInCurrentSession)
System.out.println(handle);

7. How to close only currently focused window?

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

Syntax:


// To close the currently focused window
driver.close();

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

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

Syntax:


// To close all windows opened by current session 
driver.quit();

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

Concept: By using findElement(By locator) method.

Syntax:


// To find one element on currently loaded webpage
WebElement flipkartLogo= driver.findElement(By.xpath("//img[@title='Flipkart']"));
System.out.println(flipkartLogo);

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:


// To find multiple similar elements on currently loaded webpage
List allLinks= driver.findElements(By.xpath("//a"));
System.out.println("Total elements found: "+allLinks.size());

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:


// To do full screen
driver.manage().window().fullscreen();

12. How to maximize window?

Concept:By using maximize() method.

Syntax:


// To do maxmize
driver.manage().window().maximize();

13. How to get position of window?

Concept:

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


// To get position of window relative to the upper left corner of the screen in term of X and Y co-ordinates
Point position= driver.manage().window().getPosition();
System.out.println("Position of window is: "+position);

14. How to get co-ordinates of window?

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

Syntax:


// To get individal co-ordinates of window
System.out.println("X co-ordinat is"+position.getX());
System.out.println("Y co-ordinat is"+position.getY());

15. How to get size of current window?

Concept: By using getSize() method.

Syntax:


// To get size of current window
Dimension size= driver.manage().window().getSize();
System.out.println("Size of current window:"+size);

16. How to get width and height of window?

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

Syntax:


// To get height and width of window
System.out.println("Width of window: "+ size.getWidth());
System.out.println("Height of window"+size.getHeight());

17. How to set position of window?

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

Syntax:


// To set new position of window
driver.manage().window().setPosition(new Point(300,300));

18. How to set size of window?

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

Syntax:


// To set new size of window
driver.manage().window().setSize(new Dimension(400, 500));

19. How to delete all cookies of current domain?

Concept: Using deleteAllCookies().

Syntax:


// To delete all cookies of current domain
driver.manage().deleteAllCookies();

20. How to delete specific cookies?

Concept: Using deleteCookieNamed().

Syntax:


// To delete some specific cookie
driver.manage().deleteCookieNamed("Some cookie name");

21. How to set implicit wait?

Concept: Using implicitWait() method.

Syntax:


// To set specific implicit wait time
Timeouts implicitWait= driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

22. How to set page load time?

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

Syntax:


// To set maximum pageload time
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

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:


// To set script time for execution
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

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:


// To open a URL through naviagate
driver.navigate().to("https://www.google.com");

25.  How to navigate to previously opened web page?

Concept: by using back() method.

Syntax:


// To navigate to previous page if any
driver.navigate().back();

26. How to navigate to next page?

Concept: Using forward() method.

Syntax:


// To navigate to next page of any
driver.navigate().forward();

27. How to refresh a web page?

Concept: Using refresh() method.

Syntax:


// To refresh the page
driver.navigate().refresh();

28. How to switch to an alert?

Concept: By using switchTo() method.

Syntax:


// To switch to any alert
driver.switchTo().alert();

29. How to switch to default content?

Concept: By using defaultContent() method.

Syntax:


// To switch to either the first frame on the page, or the main document when a page contains iframes.
driver.switchTo().defaultContent();

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

Concept: By using frame(int index) method.

Syntax:


// To switch to a frame using frame index
driver.switchTo().frame(2);

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

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

Syntax:


// To switch to a frame using frame name or ID
driver.switchTo().frame("name or ID");

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

Concept: By using frame(WebElement element) method.

Syntax:


// to select a frame using its previously located WebElement.
driver.switchTo().frame(" WebElement locator");

33. How to chnage focus to parent context?

Concpet: using parentFrame() method.

Syntax:


// To Change focus to the parent context.
driver.switchTo().parentFrame();

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

Syntax:


// To know the browser name of given driver
Class className= driver.getClass();
System.out.println(className.getSimpleName());

 

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

9 thoughts on “All about WebDriver: Methods And Its Usages

  1. Thanks for a nice exoplanation. Can you explain the difference between maximize() and fullScreen() methods.The scenarios where we need to use?

    1. You can perform maximize and full screen on a browser manually. You will notice difference. In maximize, you will see browser header while in FUll screen you will not see.

Leave a Reply to Prakash Kumar Malakar Cancel reply

Your email address will not be published. Required fields are marked *