WebDriver Methods

Selenium WebDriver Methods
1) get() Method
It opens specified URL in the Browser window

Syntax:
driverObject.get("URL")

Example:
driver.get("http://www.google.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()
It returns the page source

Syntax:
String stringName = driverObject.getPageSource();

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

Syntax:
String stringName = driverObject.getCurrentUrl();

Example:
String CurrentUrl = driver.getCurrentUrl();
System.out.println(CurrentUrl);
--------------------------------------------
Browser Navigation Methods:

5) navigate().to() Method
Loads a new web page in the current browser window.

Synatx:

driverObject.navigate().to();

example:
driver.navigate().to("http://www.yahoo.com");
------------------------------------------
6) navigate().back();
It moves a single item back in the web browser's history

Syntax:

driverObject.navigate().back();
---------------------
Example:

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

Syntax:
driverObject.navigate().forward();

Example:
driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().back();
driver.navigate().forward();//yahoo
--------------------------------------------
8) naviagte().refresh()
It refreshes the current web page

Syntax:
driverObject.navigate().refresh();

Example:
driver.get("http://www.google.com");
driver.navigate().refresh();
--------------------------------------------
9) close()
It closes the foused browser

Synatx:

driverObject.close();

Example:

driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.close();
--------------------------------------------
10) quit()
It closes all Browsers that opened by WebDriver during execution.

Synatx:

driverObject.quit();

example:
driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
--------------------------------------------
11) findElement()
It finds first element within the current page using the given locator and returns single element.

Syntax:

WebElemnt objectname = driver.findElement(By.locator("locator value"));
objectname.method();

Or
driver.findElement(By.locator("locator value")).method();

Example:
WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");

Or
driver.findElement(By.id("Email")).sendKeys("abcd");
--------------------------------------------
12) sendKeys()

Enters a Value in to Text box / Edit box

Syntax:

driverObject.findElement(By.lcator("locator value")).sendkeys("value");

Or

WebElement ObjectName = driverObject.findElement(By.lcator("locator value"))
ObjectName..sendkeys("value");

Example:
driver.findElement(By.id("Email")).sendKeys("abcd");

Or

WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");
--------------------------------------------
13) clear()
It clears the value from a text box or edit box

Syntax:
driverObject.findElement(By.lcator("locator value")).clear();

Example:
driver.findElement(By.id("Email")).clear();
--------------------------------------------
14) click()
It clicks an element

Synatx:
driverObject.findElement(By.locator("locator value")).click();

Or
WebElement ObjectName = driverObject.findElement(By.locator("locator value"));
ObjectName.click;

Examples:
click method on Button Object

driver.get("http:/gmail.com");
driver.findElement(By.id("signIn")).click();
---------------------------
click method on Link Object

driver.get("http:/google.com");
driver.findElement(By.linkText("Gmail")).click();
--------------------------------------------
15) isEnabled()
It checks weather the element is in enabled state or not?

Syntax:

boolean variableName = driverObject.findElement(By.locator("locator value")).isEnabled();

Example:
driver.get("http:/google.com");
boolean i = driver.findElement(By.linkText("Gmail")).isEnabled();
System.out.println(i);
--------------------------------------------
16) maximize()
Syntax:
driver().manage().window().maximize();

Example:
driver.get("http:/google.com");
driver.manage().window().maximize();
--------------------------------------------

17) isSelected()
It checks if the check box or the Radio button is selected or not? and returns Boolean results.

Syntax:

boolean VariableName = driver.findElement(By.locator("locator value")).isSelected();

Example:

boolean a1 = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println(a1); //true
------------------------------------------       
18) isDisplayed()
It checks if the element is displayed or not?

Syntax:

boolean variableName = driver.findElement(By.locator("locator value")).isDisplayed();

Example:

boolean a = driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[2]/a")).isDisplayed();
System.out.println(a);
--------------------------------
19) getText()
It returns text value that present in the web page.

Syntax:

String StringName = driverObject.findElement(By.locator("locator value").getText();

Ex:
WebDriver driver = new FirefoxDriver();   
        driver.get("http:/gmail.com");
        String s = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/h1")).getText();
        System.out.println(s);
        driver.close();
--------------------------------------------
20) implicitlyWait()
Syntax:

driver.manage().timeouts().implicitlyWait(Time in seconds, TimeUnit.Seconds);

Example:
WebDriver driver = new FirefoxDriver();   
        driver.get("http:/gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        String s = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/h1")).getText();
        System.out.println(s);
        driver.close();
---------------------------------------------

0 comments:

Post a Comment