Browser Commands in Selenium :
- Selenium gives an order to open a URL, click on the button, type in textbox, turn off the browser, and perform actions such as closing the browser etc.
- We can do a lot of command operations in the webdriver to automate the software application.
- Selenium will have to make an instance of a webdriver to handle browser commands.
Create WebDriver instance to Handle Browser Commands in Selenium:
1 2 3 | // Creating a FireFox Driver Object WebDriver driver = new FirefoxDriver(); |
Get Command:
- “get()”: The command () command is used to open a new browser window and it will get / receive the page you provided.
- Passing string URLs as parameters to navigate to a web page.
- It gives the string as a parameter and gives zero i.e. nothing.
1 2 3 | // Using get command redirect to url driver.get("https://stqatools.com/"); |
Get Title:
- “getTitle()”: This command is used to get the title of the current page.
- It does not allow any parameter and its return type string.
1 2 3 | // Using getTitle command get current url Title driver.getTitle(); |
Get Text:
- “getText()”: This command is used to obtain the text of the element.
- Able to store element text in string.
1 2 3 | // get Text of particular element and save into string String Button_Text = driver.findElement(By.id("Button")).getText(); |
Get Current URL Command:
- “getCurrentUrl()”: This command is used to get the current URL of the browser.
- This method currently returns a string containing URLs opened in the browser window, it does not allow any parameter and its return type string.
1 2 3 | // get Existing url and save into string String Current_url = driver.getCurrentUrl(); |
Get Page Source Command:
- “getPageSource()”: This command is used to get the source code of the page.
- It does not allow any parameter and its return value is string.
1 2 3 | // get Existing page source and save into string String Page_Source = driver.getPageSource(); |
Close Command:
- “close()”: This method destroys the existing window which is controlled by WebDriver.
- It does not allow any parameter and its return type is zero.
- This will end the browser, if it’s just the currently open window.
1 2 3 | // close the browser or page currently which is having the focus driver.close(); |
Quit Command:
- “quit()”: This method closes all windows opened by WebDriver.
- It does not allow any parameter and its return type is zero.
- It will automatically close all open windows and eliminate the browser.
1 2 3 | // Shut down or destroy the web driver instance(Close all the windows) driver.quit(); |
Maximize a browser:
- “maximize()”: The maximum () command is used to maximize the new open window.
1 2 3 | // Maximize the Browser driver.manage().window().maximize(); |
Minimize a browser:
- “setPosition()”: The setPosition command is able to minimize the browser command.
1 2 3 | // Minimize the Browser driver.manage().window().setPosition(new Point(0, -1000)); |
Example of Browser Commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Browser_Commands { public static void main(String[] args)throws InterruptedException{ // TODO Auto-generated method stub WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Using get command redirect to url driver.get("https://stqatools.com/"); // Get current url Title & store into string String Title = driver.getTitle(); // Get Text of particular element and save into string String Button_Text = driver.findElement(By.id("Button")).getText(); // Get Existing url and save into string String Current_url = driver.getCurrentUrl(); // Get Existing page source and save into string String Page_Source = driver.getPageSource(); // Maximize the Browser driver.manage().window().maximize(); // Minimize the Browser driver.manage().window().setPosition(new Point(0, -1000)); // C the browser or page currently which is having the focus. driver.close(); // Shut down or destroy the web driver instance (Close all the windows) driver.quit(); } } |