30
DecChristmas Special : Upto 40% OFF! + 2 free courses - SCHEDULE CALL
Hopefully, you have completed the setup for Selenium. You have configured Selenium Jar and Browser in your system. Now you are good to learn with Selenium Webdriver Commands. I will cover all important Selenium Commands which you can use in your Test Scripts.
So many questions might be floating in your mind.
Let me give you answers to your questions. Webdriver commands can be accessed by creating a driver object. You just need to create an instance of Webdriver and then all the commands of Driver you can access. Without using these commands you can't even access your web URL.
You can see in the image below the total number of Selenium Webdriver commands which are divided into 3 parts.
I will explain each Selenium command in detail one by one.
These are those commands which directly interact with your browser. For accessing the commands just create an object of WebDriver. With the help of this object, you can use all Browser commands. You can see in the below image all get commands.
Get Command: How to open a Webpage
This is the very first command used in every test script. Before performing any action on a webpage it is required to open that webpage. So, Get command is used to access and open the WebPage in the current browser window by taking the URL name as a parameter. It returns nothing as void is the return type.
Command: get(string arg0):void - WebDriver
Syntex: driver.get(WebSiteURL);
Example:
driver.get(“https://www.facebook.com”);
OR
String URL=”https://www.facebook.com”
driver.get(URL);
GetTitle Command : How to get the title of Webpage
This command is used to fetch the title of the current webpage opened. It does not take any argument as a parameter but returns a String value.
Command: getTitle() : String - WebDriver
Syntex:
String title=driver.getTitle();
OR
driver.getTitle();
getTitle() : String - WebDriver
Example:
GetPageSource Command : How to get the page source of WebPage
This command is used to fetch the page source of the webpage. It does not take any argument as a parameter but returns a String value. The same source code you get as when you right click on a page and choose View Page source to check the HTML code of your webpage.
Command: getpageSource() : WebDriver
Syntex:
driver.getPageSource();
OR
String driver.getPageSource();
getCurrentURL: How to get the current page URL
This command is used to get the URL of the current page. When you work on multiple web pages then this command is used to navigate on multiple pages. It does not take any argument as a parameter but returns a String value.
Command: getCurrentUrl : String - WebDriver
Read: What is Actions Class in Selenium and How to use it?
Syntex:
String URL=driver.getCurrentUrl();
OR
driver.getCurrentUrl();
Example:
Close : How to close the current Webpage or you can say browser
The close command is used to close the current browser window which is webdriver controlling. It neither takes any argument as a parameter nor returns any value.
Command: close() : void - WebDriver
Syntex : driver.close();
Example:
Quit : How to close all open session
This command is used to close all open windows of the browser. If you are working on Multiple windows then this command is used to close all open windows in one go. It neither takes any argument as a parameter nor returns any value.
Command: quit() : void - WebDriver
Syntex : driver.quit();
Example:
There is one question that might come in your mind. What is the difference between quit and close command? This is a famous interview question which is mostly asked in interviews.
Let me give you an answer to this. Quit command closes all the open windows but close command only closes a single-window on which the driver has control.
Actually when your script works on multiple windows, so, in that case, quit command is useful as you want to close all opened windows. But in case your script works on a single browser window then you can use close command.
Don’t worry I will explain these with an example so that you could understand clearly.
Selenium Webdriver Browser commands using close: Practice Example 1
Test case1:
Test Step:
Test Script 1: Here you can see that the verification part is done without applying assertion. For simplicity, I have verified using the Simple String function. I will cover Assertion in upcoming blogs.
Test Steps in Script :
1. Test Steps in Script :
Launch the chrome browser.
WebDriver driver=new ChromeDriver();
2. Access “ www.facebook.com”
driver.get("https://www.facebook.com");
3. Get the title of the page and verify it with Given Title
String ExpectedTittle="Facebook – log in or sign up";
String Actualtitle=driver.getTitle();
Boolean result1=Actualtitle.equalsIgnoreCase(ExpectedTittle);
Read: Selenium Tutorial: A Beginner's Guide to Web Automation
4. Get the URL and verify with the Given URL
String ExpectedURL="https://www.facebook.com/";
String ActualURL=driver.getCurrentUrl();
Boolean result2=ActualURL.equalsIgnoreCase(ExpectedURL);
5. Get the pageSouce and find the length of it
length=driver.getPageSource().length();
6. Print Title,URL and Page Source length on Eclipse console
System.out.println("Title of Webpage is "+Actualtitle);
System.out.println("URL of webpage is "+ActualURL);
System.out.println("Length of Page Source is "+length);
7. Close the session.
driver.close();
Selenium Webdriver Browser commands using quit command: Practice Example 2
Test Case 2:
Test Steps:
Test Script 2:
Here you can see that two new pages are opening. So close all webpage quit commands you can use. To act on a newly opened page you have to switch on that page. You will learn this in my next blogs.
Now you have run successfully your first scripts with all-important Browser commands. Now we are moving to Navigation Browser commands.
In a similar way to Browser commands, Navigation commands used to access a webpage. In case you want to refresh and move forward and back the webpage then these commands are used. You just need to create an object of Webdriver. With the help of that object, you can use all Navigation commands. You can see in the image below all Navigation Selenium web driver Command.
Navigate.to command :
In web driver, This method is used to load a new webpage in an existing window. It does take an argument as a parameter in the form of URL which you want to access and return nothing.
Command :
Syntex: driver.navigate().to(“String URL”);
Example :
driver.navigate().to("https://www.facebook.com");
OR
String URL=”https://www.facebook.com”;
driver.navigate().to(URL);
Refresh the Webpage command
Sometimes it happens that you need to refresh the page to load the data properly or Data is getting updated when you refresh the page so in that case, this command is useful. It neither takes an argument as a parameter nor returns anything.
Command: refresh() : void - Navigation
Syntex : driver.navigate().refresh();
Back Command
The navigate command maintains the history of the page so in case you want to go back to the webpage then you can use this command. This command enables the web browser to click on the back button. It neither takes an argument as a parameter nor returns anything.
Command : back() : void - Navigation
Read: Selenium WebDriver Architecture you Need to Know
Syntex : driver.navigate().back();
Forward Command
The navigate command maintains the history of the page so in case you want to go to the next webpage then you can use this command. This command enables the web browser to click on the forward button. It neither takes an argument as a parameter nor returns anything.
Command : forward() : void - Navigation
Syntex : driver.navigate().forward().
All Navigation commands are shown in the below image.
Here one question could arise in you mind that Get and Navigate both commands are used to access the webpage URL then what is the difference between them
Let me clear your doubt and this is another most important interview question.
driver.get() | driver.navigate().to |
It is used to access a particular website but this method doesn’t maintain the browser history so you can not use the Back and Forward method | It is used to access a particular website but this method maintains the browser history so you can use back and forward method |
This method waits until a webpage fully loaded before returning the control to your test script. | This method doesn't wait until a web page fully loaded |
This method return type is void so it returns nothing. | This method returns the instance of the Navigation interface. This interface provides a lot of additional functionalities other than just loading Cookies. |
To refresh the page you have to your pass URL again | To refresh the page this method provides a separate method navigate().refresh(); |
Navigation method can’t be used after the get method . driver.get(URL); driver.navigate().back(); Back button is not accessible here. |
Browser commands can be use after Navigate().to() method Example : driver.navigate().to(URL); driver.getTitle(); |
Let us consider a sample test script that will cover most of the Navigation Commands provided by WebDriver.
Selenium Webdriver Navigation Browser commands: Practice Example 3
Test Case 3: To perform Navigation commands
Steps:
Test Scripts:
1. Launch the Google Chrome
WebDriver driver=new ChromeDriver();
2. Navigate to “www.facebook.com”
String url="https://www.facebook.com";
driver.navigate().to(url);
3. Click on the link “Create a Page”
driver.findElement(By.linkText("Create a Page")).click();
driver.findElement(By.xpath("//button[@data-testid='BUSINESS_SUPERCATEGORYSelectButton']/div/div[contains(text(),'Get Started')]"));
4. Click on Back button for access Home page.
driver.navigate().back();
5. Clcik on Forward button for access last open page
driver.navigate().forward();
6.Refresh the webpage
driver.navigate().refresh();
7. Close the browser.
driver.close();
How to run the script:
Now you have understood all Browser and Navigation commands. WebElement commands I will cover in my next blog so keep reading my blog to get all complete knowledge.
Happy Learning…...
Read: Simple and Important Guidance For You In Selenium Test Case
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Receive Latest Materials and Offers on Selenium Course
Interviews