Selenium: First webdriver script with Java


Till now we have gone through all the rudimentary fundamentals of the Java OOPs and Interface. Its time to start writing automation scripts in Selenium with Java.

In this blogs, we will focus only on selenium webdriver as it is widely used automation component of selenium and  all other selenium components are obsolete now. We will start from writing some basic scripts and eventually we will cover all other advance stuffs of selenium. Lets begin !!

We will automate our google search task here as it is the most common thing we do in our daily routine.(isn't it ?)


package blog;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


@SuppressWarnings("unused")
public class googleSearch {

public static void main(String[] args) {

WebDriver driver;

System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.google.com");
Thread.sleep(1000);
driver.manage().window().maximize();

WebElement search=driver.findElement(By.id("lst-ib"));
search.sendKeys("India");

Thread.sleep(3000);

WebElement btn=driver.findElement(By.className("lsb"));
btn.click();
Thread.sleep(3000);

driver.close();

}
}



[Source: www.google.com ]



Explanation:

We are creating one package called 'blog' in eclipse and we will include all our automation scripts in that package.

In this code, first we are importing all the necessary selenium packages which are added as external jars in our build path in eclipse. As I am using chrome browser here I have imported jars for chrome. Similarly, you can import jars according to which browser you are using. You can download jars from here.

Then we are creating and instantiating webdriver object with google chrome driver. we need to store .exe files of browsers in our location and provide path of the same while setting the system properties of browser.

driver.get() command is used to open the particular URL and with driver.manage functions we can customize browser properties.

Next and important part is to search all the web elements on which we will work. As for google search we write our content in search bar. So, we need to find that web element using one of its developer properties. It can be id, class name, css selector, xpath or any other. Here, we are finding web element using its id and sending our search query to search bar using sendkeys command.

Then we need to click on button 'Google Search' and after defining web element btn for that in this code, we can click on it using btn.click() command.

In our code, I have mentioned Thread.sleep(3000) which is actually a wait time, it is in ms and it can be according to requirements. It is necessary because may be sometimes web page may render slowly and our driver may not be able to find particular web element on page and eventually it will lead to 'element not found' exception. 

With driver.close() command, we are closing the browser. We will see more functions, locators and commands in coming posts.

Till then just like this, try to automate your daily web surfing !! 

Cheers !










Comments

More posts on selenium...

Selenium: Automate Web Application Login

Selenium: BDD Cucumber- Cucumber Options

Selenium: All about XPath Locator in WebDriver

Selenium/Java-OOPs-Encapsulation

Selenium: Waits

Selenium: BDD- The Disruptive Framework

Selenium: Capturing the screenshot

Selenium: Frameworks

Selenium/Java-Interface