Browser Commands of Selenium WebDriver
Selenium Webdriver need to open and close browser. Below are the numbers of commands you can apply on the Selenium opened browser.
Get Command
Method Name: get(String url)
Syntax: driver.get(String url);
Purpose: open new web page in the current browser.
Example:
view plainprint?
driver.get("wwww.google.com");
Get Title Command
Method Name: getTitle()
Syntax: driver.getTitle();
Purpose: get the title of the current page.
Example:
view plainprint?
driver.getTitle();
Get Current URL Command
Method Name: getCurrentUrl()
Syntax: driver.getCurrentUrl();
Purpose: get the URL of the page currently loaded in the browser.
Example:
view plainprint?
driver.getCurrentUrl()
Get Page Source Command
Method Name: getPageSource();
Syntax: driver.getPageSource();
Purpose: get the source of the last loaded page.
Example:
view plainprint?
driver.getPageSource();
Refresh Command
Method Name: refresh()
Syntax: driver.navigate().refresh();
Purpose: refresh the current browser.
Example:
view plainprint?
driver.navigate().refresh();
Close Command
Method Name: close();
Syntax: driver.close();
Purpose: close the current window of the browser, if it’s the last window it will close the browser.
Example:
view plainprint?
Quit Command
Method Name: quit()
Syntax: driver.quit();
Purpose: quit the browser and all the opened windows in the browser.
Example:
view plainprint?
driver.quit();
Example
view plainprint?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BrowserCommands {
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
WebDriver driver driver = new FirefoxDriver();
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch the Google Website
driver.get("http://www.google.com");
// Storing Title name in String variable
String sTitle = driver.getTitle();
// Storing Title length in Int variable
int iTitleLength = driver.getTitle().length();
// Printing Title name on Console
System.out.println(sTitle);
// Printing Title length on console
System.out.println(iTitleLength);
// Storing URL in String variable
sTitle = driver.getCurrentUrl();
// Storing URL length in Int variable
iTitleLength = driver.getCurrentUrl().length();
// Printing URL on Console
System.out.println(sTitle);
// Printing URL length on console
System.out.println(iTitleLength);
// Refreshing current page
driver.get(driver.getCurrentUrl());
// Storing Page Source in String variable
String sPageSource = driver.getPageSource();
// Storing Page Source length in Int variable
int iPageSourceLength = driver.getPageSource().length();
// Printing Page Source on console
System.out.println(sPageSource);
// Printing Page SOurce length on console
System.out.println(iPageSourceLength);
//Closing browser
driver.close();
}
}
Selenium Webdriver need to open and close browser. Below are the numbers of commands you can apply on the Selenium opened browser.
Get Command
Method Name: get(String url)
Syntax: driver.get(String url);
Purpose: open new web page in the current browser.
Example:
view plainprint?
driver.get("wwww.google.com");
Get Title Command
Method Name: getTitle()
Syntax: driver.getTitle();
Purpose: get the title of the current page.
Example:
view plainprint?
driver.getTitle();
Get Current URL Command
Method Name: getCurrentUrl()
Syntax: driver.getCurrentUrl();
Purpose: get the URL of the page currently loaded in the browser.
Example:
view plainprint?
driver.getCurrentUrl()
Get Page Source Command
Method Name: getPageSource();
Syntax: driver.getPageSource();
Purpose: get the source of the last loaded page.
Example:
view plainprint?
driver.getPageSource();
Refresh Command
Method Name: refresh()
Syntax: driver.navigate().refresh();
Purpose: refresh the current browser.
Example:
view plainprint?
driver.navigate().refresh();
Close Command
Method Name: close();
Syntax: driver.close();
Purpose: close the current window of the browser, if it’s the last window it will close the browser.
Example:
view plainprint?
Quit Command
Method Name: quit()
Syntax: driver.quit();
Purpose: quit the browser and all the opened windows in the browser.
Example:
view plainprint?
driver.quit();
Example
view plainprint?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BrowserCommands {
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
WebDriver driver driver = new FirefoxDriver();
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch the Google Website
driver.get("http://www.google.com");
// Storing Title name in String variable
String sTitle = driver.getTitle();
// Storing Title length in Int variable
int iTitleLength = driver.getTitle().length();
// Printing Title name on Console
System.out.println(sTitle);
// Printing Title length on console
System.out.println(iTitleLength);
// Storing URL in String variable
sTitle = driver.getCurrentUrl();
// Storing URL length in Int variable
iTitleLength = driver.getCurrentUrl().length();
// Printing URL on Console
System.out.println(sTitle);
// Printing URL length on console
System.out.println(iTitleLength);
// Refreshing current page
driver.get(driver.getCurrentUrl());
// Storing Page Source in String variable
String sPageSource = driver.getPageSource();
// Storing Page Source length in Int variable
int iPageSourceLength = driver.getPageSource().length();
// Printing Page Source on console
System.out.println(sPageSource);
// Printing Page SOurce length on console
System.out.println(iPageSourceLength);
//Closing browser
driver.close();
}
}
No comments:
Post a Comment