Selenium WebDriver Commands

Selenium WebDriver Commands

Selenium WebDriver Methods / Commands are used to perform Operations on Web Elements

Using Element Locators and WebDriver Commands we create Test Cases.

Element Locators - To recognize / Indentify / Locate Elements

WebDriver Commands - To Perform Operations on Elements
------------------------------------------------------------
Important WebDriver Commands / Methods

1) get()
2) getTile()
3) getCurrentUrl()
4) navigate().to()
5) navigate().back()
6) navigate().forward()
7) navigate().refresh()
8) close()
9) quit()
----------------------
10) findElement()
11) sendkeys()
12) clear()
13) click()
----------------------
14) isDisplayed()
15) isEnabled()
16) isSelected()
----------------------
17) getText()
----------------------------------
1) get()

Description: Opens a specified URL in the Browser window.

Syntax:

driverObject.get("URL");

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com/");
----------------------------------
2) getTitle()

Description: Returns the Browser Title

Syntax:

String stringName = driverObject.getTitle();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
String browserTitle = driver.getTitle();
System.out.println(browserTitle);
----------------------------------
3) getCurrentUrl()

Description: Returns current url of the Browser

Syntax:

String stringName = driver.getCurrentUrl();

example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
String browserURL = driver.getCurrentUrl();
System.out.println(browserURL);
----------------------------------
Browser Navigation Methods

4) navigate().to()

Description: Loads a new Page in the Browser window

Syntax:

driver.navigate().to("URL");

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.navigate().to("http://www.rediff.com/");
----------------------------------
5) navigate().back()

Description: Moves a single item back in the Browser history

Syntax:

driver.navigate().back();

Example:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
Thread.sleep(3000);
driver.navigate().to("https://in.yahoo.com/");
driver.navigate().back();
----------------------------------
6) navigate().forward()

Description: Moves single item forward in the Browser history

Syntax:

driver.navigate().forward();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
Thread.sleep(3000);
driver.navigate().to("https://in.yahoo.com/");
Thread.sleep(3000);
driver.navigate().back();
Thread.sleep(3000);
driver.navigate().forward();
----------------------------------
7) navigate().refresh()

Description: Refreshes the current web page

Syntax:

driver.navigate().refresh();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.navigate().refresh();
----------------------------------
8) close()

Description: It closes the focused browser

Syntax:

driver.close();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.close();
----------------------------------
9) quit()

Description: Closes all Browsers that opened by WebDriver during execution

Syntax:

driver.quit();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
----------------------------------
10) findElement()

Description: Finds the first element within the web page using given locator.

Syntax:

driver.findElement(By.ElementLocator("value")) - Partial Code

driver.findElement(By.ElementLocator("value")).WebDriverCommands();

Or

WebElement elementName = driver.findElement(By.ElementLocator("Value"));

elementName.WebDriverCommand();

Example:

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

driver.findElement(By.id("Email")).sendKeys("India123");
Thread.sleep(3000);
driver.findElement(By.id("Email")).clear();


Or
WebElement editbox = driver.findElement(By.id("Email"));
editbox.sendKeys("India123");

WebElement editbox = driver.findElement(By.id("Email"));
editbox.sendKeys("India123");
Thread.sleep(3000);
editbox.clear();
----------------------------------
11) sendkeys()

Enters a value into Edit box

Syntax:

driver.findElement(By.ElementLocaror("Value")).sendkeys("Value/input data");

Or

WebElement elementName =driver.findElement(By.ElementLocaror("Value"));
elementName.sendkeys("Value/input data");

example:

driver.findElement(By.id("Email")).sendKeys("India123");
Or
WebElement editbox = driver.findElement(By.id("Email"));
editbox.sendKeys("India123");
----------------------------------
12) clear()

Clears the value from Edit box

Syntax:

driver.findElement(By.ElementLocaror("Value")).clear();

example:

driver.findElement(By.id("Email")).clear();
----------------------------------
13) click()

Clicks an Element (Click Button, click Link, select Radio button, Select/Unselect check box...)

Syntax:

driverObject.findElement(By.ElementLocator("Value")).click();

Example:

//Click Method/Command on Button

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("next")).click();

//Click Method/Command on Radio Button

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=3c0h0jqup9tub8k53vq36mjam6");
driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[2]")).click();

//Click Method/Command on Link

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
driver.findElement(By.linkText("Images")).click();

//Click Method/Command on Check Box
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
driver.findElement(By.xpath("html/body/input[2]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("html/body/input[2]")).click();
-----------------------------
Click Command/Method Operations

i) Click a Button
ii) Click a Link
iii) Select a Radio Button
iv) Select a Check box
v) Unselect a Check box
----------------------------------
14) isDisplayed()

Checks if the element is displayed or not? in the current web page.

Syntax:

boolean variableName = driver.findElement(By.ElementLocator("Value")).isDisplayed();

Example:

boolean a = driver.findElement(By.id("next")).isDisplayed();
System.out.println(a);

Or
System.out.println(driver.findElement(By.id("next")).isDisplayed());
----------------------------------
15) isEnabled()

Checks if the element is in Enabled state or not?
Returns boolean / Logical value (true/false)

Syntax:

boolean variableName = driver.findElement(By.ElementLocator("Value")).isEnabled();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
boolean b = driver.findElement(By.id("Email")).isEnabled();
System.out.println(b);
driver.close();
----------------------------------
16) isSelected()

Checks if the Element is selected or not?

Syntax:
boolean variableName = driver.findElement(By.ElementLocator("Value")).isSelected();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
System.out.println(driver.findElement(By.xpath("html/body/input[2]")).isSelected());
driver.findElement(By.xpath("html/body/input[2]")).click();
System.out.println(driver.findElement(By.xpath("html/body/input[2]")).isSelected());
driver.close();
----------------------------------
17) getText();

Returns the specified Element's Text value

syntax:

String stringname = driver.findElement(By.ElementLocator("Value")).getText();

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
String pageHeader = driver.findElement(By.tagName("h1")).getText();
System.out.println(pageHeader);
driver.close();
------------------------------------------------------------

0 comments:

Post a Comment