Selenium Webdriver is a automation testing tool which is used for testing the web applications. Opening and closing a browser is the very first test step in Selenium which a tester would like to do. The 'Commands' refers to what Selenium has to do. So here, we discuss regarding the commands we give to the Selenium in order to perform some specific task.
Get Commands:-
Get commands are used to fetch various information about the web page that we deal with. Below are some basic GET commands which are commonly used while writing the test scripts in Selenium or we must be familiar with:
1) Get Command - " get() ": get() command is used to open a web application that you have given.
2) Get Title Command - " getTitle() " : getTitle() command is used to get/fetch the title of the current page.
3) Get Current URL Command - " getCurrentUrl() " : getCurrentUrl() command is used to get/fetch the current URL of the browser.
4) Get Page Source Command - " getPageSource() " : getPageSource() command is used to get/fetch the source code of the page.
5) Get Text - " getText() ": This command is used to fetch the text of the element.
The code/snippet goes as below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GetCommands {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); //opens a firefox browser
driver.get("http://www.findnerd.com"); //opens a web application
String title = driver.getTitle(); //fetch the title
System.out.println("Result of 'getTitle':" +title); //prints the fetched title
String url = driver.getCurrentUrl(); //fetch the Current URL
System.out.println("Result of 'getCurrentUrl':" +url); //prints the fetched Current URL
String pagesrc = driver.getPageSource(); //fetch the Page Source
System.out.println("Result of 'getPageSource':" +pagesrc); //prints the fetched page source
WebElement we = driver.findElement(By.xpath(".//*[@id='offshoreindialive']/header/div[1]/div[1]/ul/li[1]/a")); //finds web element using xpath
String text = we.getText(); //fetch the text of the web element
System.out.println("Result of 'getText':" +text); //prints the fetched text of the web element
}
0 Comment(s)