Skip to main content

Browser Actions

Web Driver

  • In order to interact with web pages you will need an instance of WebDriver
driver = new SHAFT.GUI.WebDriver();

Upon Executing this line SHAFT ENGINE will detect your desired configuration from the properties files, if you have not set those don't worry, SHAFT has a set of default configurations that will be used and you can always edit configurations .
Execution environment is defaulted to Local i.e tests will be run on your own machine, so SHAFT will use webdrivermanager to auto-detect your operating system and the version of the default browser , searches for the appropriate WebDriver version on your machine and download it if it can't be found,and finally run it which is openning a new browser window.

  • in order to close all running driver instances use
driver.quit();

Browser Actions

The BrowserActions class handles browser actions like navigation and window controls

driver.browser().navigateToURL("https://www.google.com");
  • Navigates to the specified URL if it's different from the current URL, else refreshes the current page.
  • To confirm successful navigation to target URL you can add a string parameter containing text that should exist in the URL after navigation like this:
driver.browser().navigateToURL("https://www.google.com/","google");
driver.browser().navigateBack();

Navigates one step back from the browsers history

driver.browser().navigateForward();

Navigates one step forward from the browsers history

Refresh page

driver.browser().refreshCurrentPage();

Refresh the current page.

Get Current Url

driver.browser().getCurrentURL();

Returns the URL of the current page as a string

Browser Windows' Manipulation

Full Screen Window

driver.browser().fullScreenWindow();

Resizes the current window to become full screen

Close Current Window

driver.browser().closeCurrentWindow​();

Closes the current browser window

Get Window Title

driver.browser().getCurrentWindowTitle();

Returnss the current window title as a string

Maximize Window

driver.browser().maximizeWindow();

Maximizes current window size based on screen size minus 5%

Resize Window

int width = 1440; // specify wanted window width
int height =900; // specify wanted window height
driver.browser().setWindowSize​(width,height);

Resizes the current window size based on the provided width and height

Get Window Size

String windowSize = driver.browser().getWindowSize();

Returnss the current window size as a string

Switching Windows or tabs

String windowHandle = driver.browser().getWindowHandle​(); //store the current window handle
/*
some code that opens a new window
*/

driver.browser().switchToWindow(windowHandle); // switch back to the original window

The method getWindowHandle​() returns a String containing the window handle, which is a unique identifier to that window and is used to move between tabs and windows

Get Page Source​

String pageSource = driver.browser().getPageSource();

Gets the current page source and returns it as a string

Cookies

Adds a cookie to the current browsing context.

driver.browser().addCookie("cookieName", "cookieValue");

Gets a cookie with a given name.

Cookie cookie = driver.browser().getCookie("cookieName");

Get All Cookies

Gets all cookies for the current browsing context.

Set cookies = driver.browser().getAllCookies();

Gets the cookie domain.

String cookieDomain = driver.browser().getCookieDomain("cookieName");

Gets the cookie value.

String cookieValue = driver.browser().getCookieValue("cookieName");

Gets the cookie path.

String cookiePath = driver.browser().getCookiePath("cookieName");

Deletes the cookie data matching with the provided cookie name for the current browsing context.

driver.browser().deleteCookie("cookieName");

Delete All Cookies

Deletes all the cookies of the current browsing context.

driver.browser().deleteAllCookies();

As you skim through the console output you will notice the awesome reporting SHAFT provides for each performed action, and it gets even better, please see the reporting section for more on that.