Task 1: Open the Firefox and go to Google.com

If you have been given the above task what will you do? You will open Firefox browser and type www.google.com. TATADA! Task finished!!!

Great! Now we are going to do this with the help of a program.

Remember! Coding is like teaching a small kid to do things. If we instruct it clearly, it will do miracles for you!

Your first automation code

package seleniumBasics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
 * 
 * @author Arulprasath
 *
 */
public class OpenGoogle {

public static void main(String[] args) throws InterruptedException {

    // code to open the browser and go to google.com

        System.setProperty("webdriver.gecko.driver", "path where u stored\\geckodriver.exe");  
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        driver.quit();
}

}

If you have knowledge in using Eclipse, you can straight away use the code. If you are unsure on what to do, Please follow the below steps.

How to write Your first automation code

  1. Give a suitable project name. (seleniumBasics) and click on Finish.
  2. There will be an src folder inside the project. Right-click on that -> New -> Package ->Give a suitable package name (com.selenium.learning)
  3. Right click on that package -> new Class -> Give a name to the class (OpenGoogle)
  4. Copy the code from the above code segment and paste in the main method of your OpenGoogle class.

Are you getting the errors? Great you are on right track!

  • You should get errors on lines likeimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; WebDriver driver = new FirefoxDriver();anddriver.findElement(By.name("q")).sendKeys("Bishop heber trichy");

  • The errors are because, the code above is a combination of Java and Selenium (A web automation framework). So far we have created a Java project and we didn’t instruct the compiler that our code is also related to Selenium. So we have to tell our compiler to refer, Selenium also.

  • Right click on your project->Build Path -> Configure Buildpath -> Libraries Tab -> Add External Jar

*And then add the Jar you have downloaded in “ Installation and Environment set up “ step.

  • Also mention the correct path of the Gecko driver or chrome driver as explained in the code.

  • If you are using chrome driver then use,System.setProperty("webdriver.chrome.driver", "path of your chrome driver\\chromedriver.exe");

results matching ""

    No results matching ""