WebDriver Commands

Browser Commands of Selenium WebDriver
Selenium Webdriver need to open and close browser. Below are the numbers of commands you can apply on the Selenium opened browser.

Get Command
Method Name: get(String url)
Syntax: driver.get(String url);
Purpose: open new web page in the current browser.
Example:
driver.get(“wwww.google.com”);

Get Title Command
Method Name: getTitle()
Syntax: driver.getTitle();
Purpose: get the title of the current page.
Example:
driver.getTitle();

Get Current URL Command
Method Name: getCurrentUrl()
Syntax: driver.getCurrentUrl();
Purpose: get the URL of the page currently loaded in the browser.
Example:
driver.getCurrentUrl()

Get Page Source Command
Method Name: getPageSource();
Syntax: driver.getPageSource();
Purpose: get the source of the last loaded page.
Example:
driver.getPageSource();

Refresh Command
Method Name: refresh()
Syntax: driver.navigate().refresh();
Purpose: refresh the current browser.
Example:
driver.navigate().refresh();

Close Command
Method Name: close();
Syntax: driver.close();
Purpose: close the current window of the browser, if it the last window it will close the browser.
Example:

Quit Command
Method Name: quit()
Syntax: driver.quit();
Purpose: quit the browser and all the opened windows in the browser.
Example:
driver.quit();

Example
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserCommands {
public static void main(String[] args) throws InterruptedException {

// Create a new instance of the Firefox driver
WebDriver driver driver = new FirefoxDriver();

//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Launch the Google Website
driver.get(“http://www.google.com”);

// Storing Title name in String variable
String sTitle = driver.getTitle();

// Storing Title length in Int variable
int iTitleLength = driver.getTitle().length();

// Printing Title name on Console
System.out.println(sTitle);

// Printing Title length on console
System.out.println(iTitleLength);

// Storing URL in String variable
sTitle = driver.getCurrentUrl();

// Storing URL length in Int variable
iTitleLength = driver.getCurrentUrl().length();

// Printing URL on Console
System.out.println(sTitle);

// Printing URL length on console
System.out.println(iTitleLength);

// Refreshing current page
driver.get(driver.getCurrentUrl());

// Storing Page Source in String variable
String sPageSource = driver.getPageSource();

// Storing Page Source length in Int variable
int iPageSourceLength = driver.getPageSource().length();

// Printing Page Source on console
System.out.println(sPageSource);

// Printing Page SOurce length on console
System.out.println(iPageSourceLength);

//Closing browser
driver.close();
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s