Selenium WebDriver Methods

Selenium WebDriver Methods

Selenium WebDriver methods are used to perform operations on Browser and Web Elements(Objects, Ex: Links, Buttons, Edit boxes, Radio buttons, Check boxes etc...)

Using Element Locators and WebDriver methods we can create Test cases.

1) get() Method
Description:
It opens specified url in the Browser window.

Syntax:
driverObject.get("URL")

Example:
driver.get("https://yahoo.com/");
--------------------------------------------
2) getTitle() Method
It returns title of the Browser

Syntax:
String StringName = driverObject.getTitle();

Example:
String Title = driver.getTitle();
System.out.println(Title);
-----------------------------------------
3) getPageSource() Method
It returns HTML page source

Syntax:

String stringName = driverObject.getPageSource();

example:
String Pagesource = driver.getPageSource();
System.out.println(Pagesource);
-------------------------------------------
4) getCurrentUrl() method
It returns current url of the Browser

Syntax:

String stringName = driverObject.getCurrentUrl();

Example:

String url = driver.getCurrentUrl();
System.out.println(url);
--------------------------------------------
Browser navigation methods
5) navigate().to() Method
Loads a new web page in the current browser window.

Syntax:

driverObject.navigate().to("url")

Example:

driver.get("https://google.com/");
driver.navigate().to("https://yahoo.com/");
---------------------------------   
6) navigate().back() Method
It moves a single item back in the browser history.

Syntax:

driverObject.navigate().back()

example:

driver.get("https://google.com/");
driver.navigate().to("https://yahoo.com/");
driver.navigate().back();
--------------------------------------
7) navigate().forward() Method
It moves a single item forward in the browser history.

Syntax:

driverObject. navigate().forward()

Example:

driver.get("https://google.com/");
driver.navigate().to("https://yahoo.com/");
driver.navigate().back();
driver.navigate().forward();
------------------------------------------------
8) navigate().refresh() method
It refresh the current web page

Syntax:

driverObject.navigate().refresh()

Example:

driver.get("https://google.com/");
driver.navigate().refresh();
--------------------------------------------------------
9) close() method
It closes the focused browser

Syntax:

driverObject.close();

Example:

driver.close();
----------------------------------
10) quit() method
It closess all browsers that opened by Webdriver during execution.

Syntax:

driverObject.quit();

example:

driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
--------------------------------------------
Close Browser

UFT

Browser("title:=Google").Close

Selenium

driver.close();
------------------------
11) findelement() Method
It finds the first element within the current page using the given locator.

Syntax:

driverObject.findelement(By.locator("value").method();

WebElement elementName = driverObject.findelement(By.locator("value")
elementName.Method();

Example:

driver.findElement(By.id("gb_70")).click();

Or
   
WebElement button = driver.findElement(By.id("gb_70"));
button.click();
----------------------------------------
12) sendkeys()
Enters a value into Edit box

Syntax:

driverObject.findelement(By.locator("value").sendkeys("value");

example:

driver.findElement(By.id("Email")).sendKeys("India");

Or

WebElement editbox = driver.findElement(By.id("Email"));
editbox.sendKeys("India");   
----------------------------------------
13) clear() method
It clears the value from an Edit box

Syntax:

driverObject.findelement(By.locator("value").clear();

Example:

WebElement editbox = driver.findElement(By.id("Email"));
editbox.sendKeys("India");   
editbox.clear();

Or
driver.findElement(By.id("Email")).sendKeys("India");
driver.findElement(By.id("Email")).clear();
------------------------------------------------
14) click() method
It clicks an element

syntax:

driverObject.findelement(By.locator("value").click;

Example:

driver.findElement(By.id("next")).click();
--------------------------------------------------
15) isEnabled()
it checks weather the element is in enabled state or not?

Syntax:
boolean variableName = driverObject.findelement(By.locator("value").isEnabled();

Example:
boolean a = driver.findElement(By.id("next")).isEnabled();
System.out.println(a); //true
----------------------------------------
16) isDisplayed()
It checks if the element is displayed or not?

Syntax:
boolean variableName = driverObject.findelement(By.locator("value").isDisplayed();

Example:
boolean a = driver.findElement(By.id("next")).isDisplayed();
System.out.println(a); //true
-----------------------------------
17) manage().window().maximize()
Syntax:
driver.manage().window().maximize();

Example:
driver.get("https://gmail.com/");
driver.manage().window().maximize();
-------------------------------------------------
18) isSelected()
It checks if the check box or Radio button is selected or not?

Syntax:
boolean variableName = driverObject.findelement(By.locator("value").isDisplayed();
-----------------------------------------------------

0 comments:

Post a Comment