Run the first automation using Selenium and Java
Tags: java, testing
Published: 2021-07-31 17:41:06

Selenium is the first thing that comes to mind when one is planning to automate the testing of web applications. Selenium is a beneficial tool because it is not only open source but also a portable software testing framework for web applications that support multiple languages like Java, PHP, C#, Ruby, Python. Choosing the right language depends on the application under test, the supporting community, available test automation frameworks, usability, elegance, and of course, seamless build integration.

##IDE/Editor
We can use many IDEs and normal editors, but if we want to use free software, then the `eclipse` is the best IDE. However, we can write the code in VSCode and other paid software like Intellij IDEA and many more. 

Here is the link to download eclipse vscode and Intellij IDEA.

Eclipse: [eclipse IDE for Java](https://www.eclipse.org/downloads/packages/release/kepler/sr1/eclipse-ide-java-developers)
Vscode: [Vscode](https://code.visualstudio.com/download)
Intellij: [Intellij IDEA](https://www.jetbrains.com/idea/download)

##Writing the code
In the programming the folder structure is the best thing that all script writers or programmers should follow, that helps to easily understand the code structure and code flow. 

###Driver
Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Selenium WebDriver allows you to choose a programming language to create test scripts.

Here is the link to download firefox(gecko) and chrome driver. 
Firefox: [gecko driver](https://www.selenium.dev/downloads/)
Chrome: [chrome driver](https://www.selenium.dev/downloads/)

### Create a project 
* Create a maven project in any IDE (eclipse/intellij)
* Inside the `pom.xml` file  copy the following dependencies. After adding these dependencies, the maven helps to download all the dependent packages into the project directory, so we do not have to download and install individually. 

> pom.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test_project</groupId>
    <artifactId>test_project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.13.0</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.13.0</version>
        </dependency>
        <dependency>

        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.13.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>

</project>
```
* Create a `Main.java` file in the `src\main\java` directory. 

> Main.java

```java
public class Main {

    public  static  void main (String args[]) {
        System.out.println("Hello Selenium Project !! ");
}
```

* Import the selenium and driver class in the `Main` class. 

```java 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

 ```
* Add the driver functions to run the driver in `Main.java`
```java
    WebDriver driver;
    //set the firefox driver
    public void setFirefoxDriver() {
        System.setProperty("webdriver.gecko.driver","driverlocation\\ geckodriver.exe");
        this.driver = new FirefoxDriver();
        this.driver.manage().window().maximize();
    }

    //set the chrome driver
    public void setChromeDriver() {
        System.setProperty("webdriver.chrome.driver","driverlocation\\chromedriver.exe");
        this.driver = new ChromeDriver();
        this.driver.manage().window().maximize();
   }
```

* Write a simple script to automate any site in `Main.java`. 

```java
public void runTheSimpleTest() {
   this.driver.setFirefoxDrier();
   this.driver.get("https://www.w3schools.com");
   System.out.println("Automation run successfully!!"); 
   //close the tab
   this.driver.close();
   //quite the browser 
   this.driver.quite(); 
}
```
* Run the `Main.java` file. 
 ```java
public class Main {
    public  static  void main (String args[]) {
       System.out.println("Hello Selenium Project !! ");
       runTheSimpleTest();
}
...other code blocks

```
<b>Hurray, the code worked !!</b>