Grab Deal : Flat 20% off on live classes + 2 free self-paced 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.
get(string arg0):void - WebDriver
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.
getTitle() : String - WebDriver
getTitle() : String - WebDriver
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
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.
getCurrentUrl : String - WebDriver
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.
close() : void - WebDriver
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.
quit() : void - WebDriver
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 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”
3. Get the title of the page and verify it with Given Title
String ExpectedTittle="Facebook – log in or sign up";
4. Get the URL and verify with the Given URL
5. Get the pageSouce and find the length of it
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.
Selenium Webdriver Browser commands using quit command: Practice Example 2
Test Case 2:
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.
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.
refresh() : void - Navigation
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.
back() : void - Navigation
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.
forward() : void - Navigation
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.
|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 .
Back button is not accessible here.
|Browser commands can be use after Navigate().to() method
Example : driver.navigate().to(URL);
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
1. Launch the Google Chrome
WebDriver driver=new ChromeDriver();
2. Navigate to “www.facebook.com”
3. Click on the link “Create a Page”
driver.findElement(By.linkText("Create a Page")).click();
4. Click on Back button for access Home page.
5. Clcik on Forward button for access last open page
6.Refresh the webpage
7. Close the browser.
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.
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.
MS SQL Server
Receive Latest Materials and Offers on Selenium Course