FOLLOWING ARE THE BROWSER COMMANDS IN SELENIUM WEBDRIVER
- Get()
- getTitle()
- getCurrentUrl()
- getPageSource()
- close()
- quit()
Let me explain you with an example
package bc.com;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserCommands {
// Create a reference of the Webdriver
interface
WebDriver driver;
// @Test is
testNG Annotation
@Test()
public void
browserCommandsTestMethod() {
// Create a new Instance of the Chrome driver
driver = new FirefoxDriver();
String url =
"http://www.facebook.com";
/*
get(String arg0) : void - This method Load a new web page in the current browser window. Accepts String as a parameter and returns nothing.
Launch the facebook Website via driver.get(arg0);
/*
driver.get(url);
/*
getTitle() : String – This method fetches the
Title of the current page. Accepts nothing as a parameter and returns a String
value.
*/
String title
= driver.getTitle();
System.out.println("The
title of the WebPage is " + title);
/*
length() : Storing Title length in the int variable and length
method calculates the length of the title.
*/
int
titleLength = driver.getTitle().length();
System.out.println("The
length of title is : " + titleLength);
/*
getCurrentUrl() : String – This method
fetches the string representing the Current URL which is opened in the browser.
Accepts nothing as a parameter and returns a String value.
*/
String
actualUrl = driver.getCurrentUrl();
// Comparing actual and expected url
if
(actualUrl.equals(url)) {
System.out.println("Verification
Successful - The correct Url is opened.");
}
else {
/*In case of Fail, you like to print the
actual and expected URL for the record purpose
/*
System.out.println("Actual URL
is : " + actualUrl + "Expected Url is : " + url);
}
/*
getPageSource() : String – This method
returns the Source Code of the page. Accepts nothing as a parameter and returns
a String value.
*/
String
pageSource = driver.getPageSource();
// For finding the length of the page source
int
pageSourceLength = pageSource.length();
System.out.println("The length
of the page source is : " + pageSourceLength);
// Launching another website gmail along with
facebook in the same browser
String url2
= "http://www.gmail.com";
driver.get(url2);
/*
close() : void – This method Close only the
current window the WebDriver is currently controlling. Accepts nothing as a
parameter and returns nothing.
*/
driver.close();
/*
quit() : void – This method Closes all windows
opened by the WebDriver. Accepts nothing as a parameter and returns nothing.
/*
driver.quit();
}
}
EXECUTING TESTS VIA XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Browser Commands Suite">
<test name="Browser Commands All Commands Test">
<classes>
<class name="bc.com.BrowserCommands" />
</classes>
</test>
</suite>
Output:
The title of the WebPage is Facebook - Log In
or Sign Up
The length of title is : 28
Verification Failed - An incorrect Url is
opened.
Actual URL is :
https://www.facebook.com/Expected Url is : http://www.facebook.com
The length of the page source is : 59472
===============================================
Browser Commands Suite
Total tests run: 1, Failures: 0, Skips: 0
No comments:
Post a Comment