Mastering Selenium WebDriver Methods for Effective Test Automation Strategies(Part 1)
- Neetu khatri
- Oct 24
- 4 min read
WebDriver Methods
1.get(String url)
It used to navigate to an application
It takes String as an argument
Returntype is void
We neeed to pass fully qualified url amazon.com----> https:/ /www.amazon.in/
If we don't pass fully qualified url we get InvalidArgumentException
2.getTitle()
It is used to capture title of the webpage Returntype is String
It is used for validation purpose
Title is the tab name shown in the browser
We have to store it in a variable and print it
3. getCurrentUrl()
It is used to capture the title of a webpage
Returntype is String
It is used for validation purpose
We have to store it in a variable and print it
4. getPageSource ()
It is used to capture the source code of a webpage
Returntype is String
We have to store it in a variable and print it
5. close()
It is used to close the browser window where the driver control is present
Return type is void
It will not stop the server
6. quit()
It is used to close all the windows which are opened by selenium
Returntype is void
It will stop the server
Note: -
-close() and quit() should always be the last line of the code Otherwise, we get an exception saying
close ( )- ->11NoSuchWindowException11 / NoSuchSessionException 11
quit () - ->11 NoSuchSessionException11
7. manage()
It is used for managing purposes
- -> Returntype is Options
[Options is an Interface in Selenium]
It is used to manage
i)window - - -> window() ii)timeouts - - - -> timeouts() iii)cookies- -- -> cookies()
i)window()
- - - - -> Returntype is Window[Interface in Selenium]
maximize()
minimize()
fullscreen()
getSize ()
setSize(Dimension)
getPosition()
set Position (Point)
------------------------
1.maximize()
It is used to maximize the browserwindow driver.manage().window().moximize();----) Second Line of Code
2.minimize()
It is used to minimize the browser window driver .manage()w. indow(). minimize();
3.fullscreen
It is used for opening the web page in Full screen Browser title,url and other parts will be hidden
driver. manage(). window(). fullscreen();
Note ·- Returntype for maximize(),minimize(),fullscreen() is void
4.getSize()
It is used return height and width of webpage
It returns the size in the form of pixel format
Returntype ls Dimension [Class in selenium]
5.setSize(Dimension)
It is used to set the size of the browser
Here we need to Create Object of Dimension -> width
driver. manage() .window(). setSize(new Dimesion(int width, int height));
6.getPosition()
It returns the cursor position of the window
Returntype is Point [class in selenium]
driver .manage() .window(). getPosition();
position. getX();getX getY position. getY{);
7.setPosition()
It is used to set the position of the window
Here we create object of Point
driver.manage().wlndow().setPosition(new Point(lnt x, int y));
8.Navigate()
It is used to navigate to other application or within the application
Returntype of navigate ls Navigation [Interface in Selenium]
It provides us 5 methods
i)to(String url)
ii)to(URL url)
iii) forward()
iv)bock()
v)refresh()
i} to(String url)
This is another way of navigating to on application without using get()
It takes string as on argument
We need to poss fully qualified url
Returntype is void
In earlier versions of selenium to(Strlng url)
was not able to wait until the complete webpoge ls loaded, But in recent versions it is calling get() internally
driver. navigate(). to(String url);
ii)to(URL url}
It is also used to navigate to on application without using get()
We need to create an object of Url ond we need to poss url in to it driver. navigate(). to(new URL(String url)};
iii)back()
iv)forward()
v)refresh()
---> It ls used to perform forward,backward and refresh actions in a webpage driver .navigate(). back();
driver .navigate(). forward();
driver .navigate(). refresh();
- - -> Returntype for to(String url), to (URL url}, back(), forward(), refresh() is void
9. getWindowHandle()
It is used to capture the window ID/Session Id of a webpage where driver control is present (Parent Window)
Returntype is String
String id"' driver.getWindowHandle();
System. out. println(id);
Program:
public class GetWindowHandle {
public static void main(String[] args) { WebDriver driver := new ChromeDriver(); driver. manage(). window();
driver. get(" https: //www.flipkort.com/");
String id= driver.getWindowHondle(); System. out. println(id);
10. getWindowHandles()
It is used to Capture all window ID of the webpages that are opened by our selenium
Returntype is Set<String>
Set<String> ids " driver. getWindowHandles();
System. out. println(ids);
Program:
public class GetWindowHandles {
public static void main(String[] args) throws Throwable { WebDriver driver = new ChromeDriver();
driver .manage(). window() .maximize();
driver. get( •https= //demowebshop. tricentis. com/"); Thread.sleep(2000);
driver. findElement (By. linkText( "Facebook")). click(); Thread.sleep(1000);
Set<String> ids = driver. getWindowHandles();
System. out. println(ids);
10.switchTo()
It used to switch the driver control to i)window() > ..,/
frame()
alert()
Returntype of switchTo() is Targetlocator[Interface in Selenium]
driver. switchTo(). window()
Program:
public class SwitchTo {
public static void main(String[] args) throws Throwable {
WebDriver driver "'new ChromeDriver(); driver .manage() .window(). maximize();
driver. get(• https ://demowebshop. tricentis. com/"); Thread. sleep(2000);
driver. findElement (By. linkText( "Facebook")). click(); driver. findElement (By. linkText( "Twitter")). click();
/ /Window id of Parent window
String parentid"' driver.getWindowHandle(); System. out. println(parentid);
//all window ids
Set<String> allids = driver. getWindowHandles(); System. out. println(allids);
// Removing parent id
all ids. remove(parentid); System. out. println(allids);
// Switching the driver control and close the child window for (String windowid allids) {
driver. switchTo() .window(windowid); Thread. sleep(2000);
driver. close();
}
}
In continuation of next blog u will be having examples for WebDriver Methods in Part 2 --->


