Wednesday 3 February 2016

Selenium webdriver browser commands

SELENIUM WEBDRIVER BROWSER COMMANDS:
WebDriver Browser Commands:
=============================


I Think being a selenium guy every knows the importance of WebDriver.

Lets have a look on the different commands that are available on WebDriver one by one.







1.Get Command:
===================

syntax:public void get(String arg0) :
This method Load a new web page in the current browser window. Accepts String as a parameter and returns nothing.

Url is the website address and use always fully qualified name

2.Get Title Command:
==================

syntax:public String getTitle() :

This method getsthe Title of the current page.

If title present it returns a String value ele it retunrs nothing.

As the return type is String value, the output must be stored in String object/variable.



3.Quit Command:
=================
syntax:public void quit() : – 

This method Closes all windows opened by the WebDriver. and returns nothing.

Close every associated window.



4.Get Page Source Command:
========================
syntax:public String getPageSource() :

This method returns the Source Code of the page.

And it returns String as an output.

5.Get Current URL Command:
=========================
syntax:public String getCurrentUrl() : 

This method fetches the string representing the Current URL which is opened in the browser. and it returns String as an output

Example:

======

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

public class BrowserCommandsDemo {

public static void main(String[] args) {

String expectedTitle = "Gmail";

WebDriver webDriver = new FirefoxDriver();

webDriver.manage().window().maximize();

webDriver.manage().deleteAllCookies();

webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

webDriver.get("https://www.gmail.com");

String actualTitle = webDriver.getTitle();

if (actualTitle.equalsIgnoreCase(expectedTitle)) {

WebElement element =webDriver.findElement(By.id("Email"));

element.clear();

element.sendKeys("rameshat914@gmail.com");
}
String currentUrl=webDriver.getCurrentUrl();

System.out.println("current url is:" +currentUrl);

String pageSource = webDriver.getPageSource();

System.out.println("page source is:" +pageSource);
webDriver.quit();

}
}




No comments:

Post a Comment