Se#4 - Handling Browser | WebDriver Methods
What is covered in this article?
- How to launch browser
Common actions on browser -
- Open url
- Close browser
- Navigate Browser
- Retrieve browser info
- Finding Web object/ html elements
- How to handle cookies
- Maximise/Minimise browser
To perform anything on browser first step is to launch browser and open the application url. Selenium provides programmatic way to handle browser using WebDriver interface. Lets first see how to launch browser.
- As we know that each browser has specific driver which know how to handle browser natively. First step is to start browser specific driver which act as local server and accept REST command sent by automation script. Each browser version has its own driver specific to version and platform. You can check version of chrome browser by navigating to Help-> About Google Chrome
Once you know browser version, you can download driver from url - https://chromedriver.chromium.org/downloads
A1 : How to Launch Browser
- The driver which we have downloaded needs to be set as system property in java with key "webdriver.chrome.driver" and value as path where driver is saved. When we create object of ChromeDriver it will use this system property to locate driver and then will start the driver. (System.setProperty("webdriver.chrome.driver",path-of-driver");)
- Next step is to create object of browser driver in this case ChromeDriver.
- ChromeDriver chrome = new ChromeDriver (); // this will start driver on available port which will listen to automation request & launch chrome browser
- As we know WebDriver is parent of ChromeDriver, as per Java principle parent class reference can hold the child class object, so we can also write above line as - WebDriver driver = new ChromeDriver();
- Standard Practice - Using WebDriver reference to handle the browser is recommended way as you can refer to other browsers using same reference variable.
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/selenium-learning/src/main" +"/resources/chromedriver7");
WebDriver driver = new ChromeDriver();
When we execute above code, it will start chromedriver using system property on available port as below and will also launch browser.
Below are process which got started for chromedriver -
A2: Open Url
After launching browser we need to open url of application. There are two methods which can be used to load url on current browser window.
- void get(String url)
- void to(String url) // driver.navigate.to(url)
As we know that automation commands are getting converted into REST service in this case it is HTTP POST request with URI /session/{session id}/url and request body as {"url": "https://www.appurl.com"}.
Exception - url must be fully qualified else it will throw exception org.openqa.selenium.InvalidArgumentException: invalid argument
A3: Close Browser
To close browser below two methods can be used.
- void close() - Close the current window, quitting the browser if it's the last window currently open.
- void quit() - Quits this driver, closing every associated window.
A4: Navigate Browser
To perform navigation on browser like clicking on forward, backward icon on browser, there is nested interface in WebDriver called WebDriver.Navigation which has below methods declared-
- void back()
- void forward()
- void refresh()
- void to(String url)
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.co.in");
driver.navigate().refresh();
driver.navigate().back();
driver.navigate().forward();
A5: Retrieve browser info
Below are the methods provided by WebDriver interface to get details about browser like current url, title of the web page, page source.
- String getCurrentUrl()
- String getPageSource()
- String getTitle()
A6: Finding Web object/ html elements
One of most common operation performed on browser is to interact with html element like text field, radio button, hyperlink. WebDriver provides two methods to get access of html element on web applications.
- WebElement findElement(By by) - Find the first WebElement using the given method. Exception - NoSuchElementException - If no matching elements are found
- List<WebElement> findElements(By by) - Find all elements within the current page using the given mechanism or an empty list if nothing matches.
WebElement - WebElement is an interface which represent an html element on page and provide methods to perform action on html element. In Selenium, there are no specialised object like textfield, button, hyperlink, radiobutton instead all element are represented only as WebElement.
By - By is an abstract class which provides mechanism to locate element on web page.
WebElement btnGoogleSearch = driver.findElement(By.name("btnK"));
Note - Will discuss about locator strategy and WebElement in upcoming articles.
A7: How to handle cookies
In some testing scenarios we need to perform action on browser cookies like adding cookie, deleting cookie which can performed from browser menu as below -
To perform such action, Selenium provided interface WebDriver.Options which is nested interface defined under WebDriver. It has below methods-
- void addCookie(Cookie cookie)
- void deleteAllCookies()
- void deleteCookie(Cookie cookie)
- Set<Cookie> getCookies()
driver.manage().deleteAllCookies(); // driver.mange() - Return Option interface
A8: Maximize/Minimize browser
Webdriver has interface WebDriver.Window which has method to change the browser size and set to maximise, minimize and fullscreen. To access method of this interface we need to first get Option interface using driver.manage() and then call window() method which is part of Option interface and return type of this method is Window as below
- WebDriver.Window window() // Note that this method is declared in WebDriver.Options
Below are methods of WebDriver.Window interface-
- void fullscreen()
- void maximize()
- void minimize()
- void setSize(Dimension targetSize) //Set the size of the current window.
- void setPosition(Point targetPosition) //Set the position of the current window.
driver.manage().window().maximize(); driver.manage().window().minimize();
Code - https://gist.github.com/sksingh329/6eb769149498c71be2f364c8008dd856
Very useful