Selenium WebDriver Browser Commands

What is Browser Command?

- Browser command are those which are related to Browser only.

Method
A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.

Parameter
It is an argument which is passed to a method as a parameter to perform some operation.
Every argument must passed with the same data type. 
e.g. get(String arg0) : void. This is asking for a String type argument.

Return Type:
We will use methods to do a particular task after completion of task 
if we want to return something to the calling place these return types will be used.
Here Void is the return type. This means it returns nothing.



Now, Let's start with the actual Browser Commands. It will be very easy to understand it.

Different type of Browser Commands.

Get Command
This command is use to open new page in Browser. It require String as parameter. 
Here String is generally url/address of that page. 

//Open Url in Browser
driver.get("https://www.redbus.in/");

Get Title Command
This command is use to get the Title of the current page. It doen't require any parameter and returns String value.

//Get The Title of the page
String GetTitle=driver.getTitle();
System.out.println("The title of the page is:"+GetTitle);

Output: The title of the page is:redBus : Online Bus Ticket Booking, Book Volvo AC Bus Tickets, Reservation

Get Current URL Command
This command returns the Current Url which is currently open in browser. 
It doen't require any parameter and returns String value.

//Get Current Url
String GetCurrentUrl=driver.getCurrentUrl();
System.out.println("The Current Url of the page is:"+GetCurrentUrl );

Output: The Current Url of the page is : https://www.redbus.in/

Get Page Source Command
This method returns the source code of the current page. It 

//Get Source Code 
String GetSourceCode=driver.getPageSource();
System.out.println("Below is the Souce Code:\n"+GetSourceCode);

Close Command
This command close the Browser window. It doesn't require any method neigther it returns any value. If you write any method after close method, It will gives you Exception.

//close Browser Window
driver.close();

Quit Command
This method close browser window along with it will clear instances that you created. Hence it gives you proper message if you still write any method after quit. It doesn't require any method neither it returns any value.

//close Browser Window
driver.quit();

Selenium WebDriver Browser Commands:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class RunBrowserCommand {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

WebDriver driver=new FirefoxDriver();
//Open Url in Browser
driver.get("https://www.redbus.in/");
//Get The Title of the page
String GetTitle=driver.getTitle();
System.out.println("The title of the page is:"+GetTitle);
//Get Current Url
String GetCurrentUrl=driver.getCurrentUrl();
System.out.println("The Current Url of the page is:"+GetCurrentUrl );
//Get Source Code 
String GetSourceCode=driver.getPageSource();
System.out.println("Below is the Souce Code:\n"+GetSourceCode);
//close Browser Window
driver.quit();
}

}





No comments:

Post a Comment