Introduction
In this part of my tip, we are going to discuss about various browser commands that we would be using in our day to day life in testing. Opening and closing a browser is the very first thing in Selenium, a tester would like to do. The commands refer to what Selenium has to do. Just like a master commands a slave in early days!! Sorry even today. OK!! Let's not get social now.
So here, we discuss regarding the commands we give to the Selenium in order to perform some specific task.
Below are different basic commands, which we will encounter while writing the scripts.
Get Commands
Get commands are used to collect various information about the page that we deal with. Here are some important "get
" commands you must be familiar with:
- Get Command - "
get()
": get()
command is used to open a new browser window and it will find/fetch the page that you have given. - Get Title Command - "
getTitle()
" : This command is used to get the title of the current page. - Get Current URL Command - "
getCurrentUrl()
" : This command is used to get the current URL of the browser. - Get Page Source Command - "
getPageSource()
" : This command is used to get the source code of the page. - Get Text - "
getText()
": This command is used to fetch the text of the element.
Navigate Commands
- Navigate To Command - "
navigate().to()
" : This command is like Get()
command. It opens a new browser window and it will find/fetch the page that you have given. - Navigate Refresh Command - "
navigate().refresh()
" : This command is used to refresh the current page. - Navigate to back page - "
navigate().back()
" : This command is used to go back by one page on the browser's history. - Navigate to forward page - "
navigate().forward()
" : Takes you forward by one page on the browser's history. - Refresh page - "
navigate().refresh()
" : It refreshes the current page.
Other Useful Commands
SwitchTo
command - " driver.switchTo().window("windowName")
" : This command is used to switch from one window to another.- Switching from one iframe to another - "
driver.switchTo().frame("frameName")
" : This command is used to switch from a one iframe to another iframe. - Handling Alerts - "
driver.switchTo().alert()
" : This command is used to handle alerts. - Close Command - "
close()
": This command closes the current window of the browser. - Quit Command - " quit() " : This command is used to quit the browser and all the opened windows in the browser. These are the basic commands that are needed for writing simple script.
Now by using these commands, I will automate a browser by writing scripts so that we can have a clear idea of how to use these commands.
Test Scenario
- Open a Firefox Browser.
- Put an Implicit wait.
- Navigate to the URL.
- Maximize the window.
- Store the Title Name.
- Print the Title Name.
- Store Title Length.
- Print Title Length.
- Store the URL Name.
- Print the URL Name.
- Store URL Length.
- Print URL Length.
- Print the name of Tab which is going to be clicked suppose [NEWS TAB].
- Click on NEWS Tab.
- Store the Text of an element of the new opened tab.
- Print the Text Name of an element of the new opened tab.
- Now Navigate to the previous page.
- Print the Title of the previous page.
- Now Navigate to the next page.
- Print the Text of the next page.
- Refresh the current page.
- Store the Page Source in String variable.
- Print the Page Source Name.
- Store Page Source Length in Integer Variable.
- Print Page Source Length.
- Close the browser or page currently which is opened.
The code/snippet goes as below:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Commands {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://www.bbc.com/");
String btitle = driver.getTitle();
System.out.println(btitle);
System.out.println("Title is : " + btitle);
int bTitleLength = driver.getTitle().length();
System.out.println("Length of the Title is : " + bTitleLength);
String b1 = driver.getCurrentUrl();
System.out.println("URL is : " + b1);
int b2 = driver.getCurrentUrl().length();
System.out.println("URL length is : " + b2);
String b3 = driver.findElement(By.xpath("//*[@id='blq-nav-news']/a")).getText();
System.out.println("Name of the Tab which is being clicked is : " + b3);
driver.findElement(By.xpath("//*[@id='blq-nav-news']/a")).click();
String b4 = driver.findElement(By.xpath("//*[@id='header']")).getText();
System.out.println("Text of current page is : " + b4);
driver.navigate().back();
Thread.sleep(1000);
String b5 = driver.getTitle();
System.out.println("Text of the previous page is : " + b5);
driver.navigate().forward();
Thread.sleep(1000);
String b6 = driver.getTitle();
System.out.println("Text of the NEXT page is : " + b6);
driver.navigate().refresh();
String b7 = driver.getPageSource();
Thread.sleep(1000);
System.out.println("Page source : " + b7);
int b8 = driver.getPageSource().length();
System.out.println("Length of the page source is : " + b8);
driver.close();
}
}
Below is the output of the commands executed as seen in the console window.
SwitchTo
command- Switching from one iframe to another
- Handling Alerts
The above three useful commands are not covered in this part of the tip, I will be penning these details in my upcoming article.
Thanks for reading. Hope this helps beginners in testing.