Selenium Commands | Navigation, Browser and Get Commands
This page is created to help you with some of the basic Selenium Commands that you might be required to use while performing your tests. We are going to see everything that might prove useful while creating simple test cases to navigate across browsers, to locate elements on a page or to switch between windows.
We will cover some of the most commonly used commands too that deals with the aftermath of finding elements.
The good news is that we will also run step by step tests to see all of those commands in action. Isn’t that like an icing on the cake?
Will Ferrell looks hungry. Let’s get started at once.
Selenium Commands for Navigation
This is probably one of those most basic Selenium commands that you might come across while preparing or executing your test cases.
Commands worth remembering here are:
- navigate().to()
- navigate().back()
- navigate().forward()
- navigate().refresh()
Let’s take a look at them one by one in our program and see them in action.
Navigate To Method
The first one is navigate().to() where we can enter a URL in the parenthesis. It will take us to the said URL, as in, literally navigate us there.
Let us type that in our Eclipse IDE and see this in action.
I am gonna use ChromeDriver to create an instance of it first:
ChromeDriver driver = new ChromeDriver();
Then follow it up with the following to use our Navigate To command.
driver.navigate().to("http://dumbitdude.com");
This is how it looks like in Eclipse:
Let’s run this by clicking on the green arrow button:
Voila! It works!
Navigate using Get() Method
So the above is basically the same thing that you could achieve by using the get() method. We will see more get Selenium Commands later on this page too.
Let’s try that now. We will add another line with the get() method this time to depict that it will also work, and then take you to the second website.
Just append the following code to the existing one:
driver.get("https://www.google.co.in");
This should take you to the Google’s webpage.
So the code we have now is:
Let’s run this and see whether everything happens as planned:
Coo!
So we saw that it opened the first website first in the order they were mentioned in the code and then later navigated us to the second one.
Navigate Back Selenium Command
So we want to go back to the previous website. How do we do that?
navigate().back() command helps us to achieve exactly that.
We will just use this command in our existing code and see whether we get the “Dumb IT Dude” page back online or not.
Adding the following code:
driver.navigate().back();
This is how it will appear in our Eclipse together with our previous code:
Let us run this and see for ourselves:
So it came back to the “Dumb It Dude” site. Victory!
Navigate Forward Selenium Command
Wish to go back again? That’s where navigate().forward() command comes to the rescue.
It will move you forward, basically press the forward button to take you once again back to where you came from. So in this case we will simply use the command and expect Google’s page to show.
Let us append the following line of code to our existing code:
driver.navigate().forward();
Our whole code might look something like this:
Let us run this and keep our fingers crossed for Google to reappear:
So I have been waiting for a while but the transition happened as expected, and it was beautiful! I even cried a little.
Navigate Refresh Selenium Command
At times you might have a requirement that requires you to refresh the opened window. Basically what the refresh button or pressing the F5 key does for you. It is one of thos selenium commands that caters exactly that.
Selenium provides navigate().refresh() code for that, for you to be able to refresh a window.
Let’s get rid of everything except the first line of code now. Then follow it up using the Navigate Refresh command:
navigate().refresh();
So the piece of code you might have with you might look like:
You run this and you will see the website open and then once again get refreshed once it finishes loading. You can see it in action in the following video:
But hey! Amidst all of this did you notice that our windows weren’t maximized at all? So the question is how do you achieve that?
How to Maximize a Window in Selenium | Fullscreen Selenium Commands
Sometimes when you are trying to open a website using the get or navigate method windows don’t always come maximized. Now you might need your windows maximized to be able to see certain options or features on a webpage. Don’t fret! You can easily do that using a simple manage().windows().maximize() method.
Here’s how to use that.
Just type the following before you use the navigate or get method to open a URL:
driver.manage().window().maximize();
NOTE: Remember it is important to use this prior to using get or naviate method, or it wouldn’t work.
Here’s how it will look in your Eclipse:
Just gonna run this to see if it works:
Cool!
In case the maximize command doesn’t work for you, you can give another method a shot as well. It is called the fullscreen() method.
Luckily it is located in the same window() method. So there’s no writing a different code:
Try that and if it still doesn’t work for you, maybe you are using the latest browser. Try rolling it back to an older version.
Other Get Commands
We use get methods in our Driver classes to grab a variety of stuff from the server. The first one we have already seen is used to grab the url, and is one of the most commonly used methods. To check out other Get commands you can simply type get and then see the results for yourself.
We are going to take a look at some of the widely used Get Methods. Rest we might cover at a later stage in time.
Get Class Command
So you are in a situation wherein you are required to find out the name of the class you are in. In order to find it, you can run a simple getClass() method. Here’s how to do that:
Just type the following in your code:
driver.getClass();
Since this is going to return a class you can choose to print it out and display using System.out or even use it elsewhere in your code. To show you I will use System.out. Our whole code will now look something like this:
As you can see, it tells you the class name of the object you are using to call it. So the class name here is org.openqa.selenium.chrome.ChromeDriver. Remember that’s how a class is identified, with all the package info even though you have written as simply the last part.
Get Title Selenium Command
If you wish to grab the title of the webpage you are opening the getTitle() command comes really handy.
Just type the following in your code:
driver.getTitle();
Doing so will grab the title of the page and remove any unnecessary spaces from either ends and present it to you. To depict this in Eclipse I will tie this into the System.out. baggage:
There! Our site’s name was google. Well done getTitle() command!
Get Current URL Selenium Commands
At times when you are not sure as to what your current URL is, you can take the aid of getCurrentURL() Selenium Command. The reason you might need this is owing to the fact that sometimes what you are trying to open automatically redirects to something else. During such times you need to assure whether what was opened is exactly what you were looking for.
A perfect example for this would be Google. Here I will show you how:
As you can see we opened “google.com”, but it redirected us to the “google.co.in” version with random letters next to it. So if it’s not something that you are looking for, based on your requirement you can pass or fail your test case.
getCurrentUrl method is something that you will be required to use a lot since it’s useful in assertion to check whether clicking on a link is navigating to the right page or not. So keep this method handy.
Get Page Source Command
Page Source is all the info that constitutes a particular web page. If you grab that info, you could obtain some really useful information about a website you are testing.
You might be required to grab it on several occasions. So if you are wondering how to do that, there’s a simple way by using the getPageSource(), a readily available method in your WebDriver instance.
The getPageSource method is a WebDriver method that returns String.
Just type the instance and follow it by a dot operator and type get. You will see something like this:
Type the rest like this:
driver.getPageSource();
You have just grabbed the Page Source of the website you have opened using this method.
Since its return type is String you can choose to store it in a String variable or simply choose to display it using Sys.out like this:
System.out.println(driver.getPageSource());
The result you might get be some random stuff that might look unreadable:
Well what cha gonna do? That’s how Page Sources look like!
Get Window Handles Selenium Commands
Some websites open with multiple windows. Each window is identified by Selenium as a separate handle. Selenium WebDriver identifies these handles using a certain alphanumeric value. You can check it out by running it using the following code:
System.out.println(driver.getWindowHandle());
If you type the above, the result you get might look something like this:
CDwindow-(82CF1B0E970237F4614F8363EA5889D)
That’s the window handle by which Selenium WebDriver has identified Google’s web application. It would however change every time you run it. So the next time you run it, the window handle might change to a different Unique ID:
CDwindow-(2319A89E962B2034A34FA725DBCFA370)
As you can see running it again has changed it to the above.
Selenium does it so that it could set opened windows apart, so that when you are trying to switch windows it is easier for it to remember which is which.
If you want to get the window ‘handles’ of all the opened windows, you can alternatively use the following:
driver.getWindowHandles();
So if there are many windows that are being opened (or in case of pop ups) you can grab the window handles and print their individual results.
The getWindowHandles() method returns Set of handles, and hence printing it out as String would only take the handle of the current window.
To print the handle values of all the open windows you have to use:
Set<String> handle = driver.getWindowHandles(); for(String h: handle) { System.out.println(h); }
We have used for each loop here to cycle through the set we created. Using the above code you can get the values of both the handles.
Let’s run it:
CDwindow-(ADAECAF09ED88BBBE14C4C67B213A1DE) CDwindow-(266C0CC18A184D7192C40D873DED6E46)
Cool right?
Then you can use these results to switch to the window you desire to work on. It is a whole different story that will be addressed in a different tutorial.
Thanks for bearing with it all. Hope you know some basic Selenium commands now. Get started already!
2 Responses
[…] from each other. A handle has nothing but an alphanumeric value for itself as was seen earlier in Selenium Commands tutorial that helps in […]
[…] 1 […]