So far we wrote our first Selenium Script by creating a WebDriver object. Now by using this object we will navigate to a website and then perform various validations.
Our First Test Case:
Navigate to “Google” website using Chrome Browser. Find the Search Web Element, enter a text “Selenium” and then do a search. Click on the first link displayed and verify the title of the page after loading the web page. This is a manual test case that we will try to automate.
- Let resume from our FirstScript.java that we created in last post. Add a new code “driver.get(http://www.google.com)” and save the script.
- To execute the script, right click FirstScript.java and click Run As -> Java Application. The moment we execute this script, we get an exception in the IDE Console. The error says “The path to the driver executable must be set by the webdriver.chrome.driver system property”. This means that we have to inform the System or JVM the path of Chrome driver executable file by setting the system property.
Setting up System Property:
Windows OS/Mac OS:
- To keep the script organized, you can create a new folder called “Chrome”inside our Java Project. This can be done easily by right clicking “FirstSeleniumProject” and then selecting New -> Folder.
- Place the downloaded Chrome Driver Executable file (the one we downloaded in the last post) in this folder. We can do this by simply drag and drop from the Download folder. The above two steps can be avoided, however I did this just to make our script organized.
- The next step is to set the System Properties for “webdriver.chrome.driver” with the location of the Chrome Driver executable file that we downloaded in the last post. Always remember, that this setting need to be done before assigning the WebDriver.
- For Window OS – Add the following code for setting System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\Chrome\\chromedriver.exe");
- For Mac OS – Add the following code for setting System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Chrome/chromedriver");
- To make the driver object to navigate to a Google website, use the following code ‘driver.get(http://www.google.com)’. Always make sure to use the complete path of the URL including the http protocol.
- Save the code and execute the script by clicking Run As -> Java Application or else you can also use the green play icon displayed in the header of Eclipse.
- Once the execution starts, you will be able to see Google website opens in a new Chrome Browser window.
And this completes the first step of our test case.