Lets write our first Selenium test with the help of Code Modular 
Tags: automation, selenium, seleniumwebdriver
Published: 2021-05-18 19:34:45

###𝗪𝗵𝗮𝘁 𝗶𝘀 𝗦𝗲𝗹𝗲𝗻𝗶𝘂𝗺?###


Selenium is a set of tools and libraries that automates web browser actions. If you go to selenium official site(www.selenium.dev) you will find it says "selenium automate browsers. That's it."
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5we84ph34jmk882b440w.png)

Selenium is free and currently is the most widely used and most popular open source solution for test automation of web applications.

Selenium supports all the major browsers like Firefox, Chrome, Internet Explorer, Safari and many more. And all the major Operating Systems are also supported.
If you go to official site you will find that selenium supported by many languages including Java, C#, JavaScript, Perl, PHP, Python, Ruby and so on.

So why not learning it!!!

ok, Lets try to write our first selenium script.

###𝗜𝗻𝘀𝘁𝗮𝗹𝗹𝗮𝘁𝗶𝗼𝗻 𝘀𝘁𝗲𝗽𝘀###

####𝟭.𝗙𝗶𝗿𝘀𝘁 𝗶𝗻𝘀𝘁𝗮𝗹𝗹 𝗝𝗗𝗞 𝘁𝗼 𝘆𝗼𝘂𝗿 𝗰𝗼𝗺𝗽𝘂𝘁𝗲𝗿####

https://www.oracle.com/java/technologies/javase-downloads.html

After installation is finished you can use the following command to check whether Java JDK is installed successfully in your system and installed version.

In Windows/Linux, go to Terminal
Enter command - "java -version"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02d1337fnuhqsopg91i1.PNG)

####𝟮.𝗗𝗼𝘄𝗻𝗹𝗼𝗮𝗱 𝗮𝗻𝗱 𝘀𝘁𝗮𝗿𝘁 𝗘𝗰𝗹𝗶𝗽𝘀𝗲 ####

Download the latest version from this link
http://www.eclipse.org/downloads

####𝟯.𝗖𝗿𝗲𝗮𝘁𝗲 𝗮 𝗝𝗮𝘃𝗮 𝗽𝗿𝗼𝗷𝗲𝗰𝘁 𝗶𝗻 𝗘𝗰𝗹𝗶𝗽𝘀𝗲

Right click on the package Explorer,
New-->Java Project
And lets name it as "SeleniumTest"

####𝟰.𝗔𝗱𝗱 𝘀𝗲𝗹𝗲𝗻𝗶𝘂𝗺 𝗝𝗮𝗿𝘀

Go to Selenium official site and download the Selenium Client & WebDriver Language Bindings.
For this test lets choose Java language.

www.selenium.dev/downloads

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck7ntp490j6lhfmk7we2.PNG)

After download has finished unzip the zip file and copy to a location you prefer.

Again go to Eclipse and create a folder.

Do a right click-->New-->Folder
And name it as "lib"
In this folder lets keep all our Jars.

Now go to the folder that you unzipped and copy all the jar files and paste it inside the lib folder.

Once it has copied select all those jars and add to build path.

Build-->Build Path

As soon as you do this you will see the jars you have selected is added to the Referenced Libraries now.

####𝟱.𝗗𝗼𝘄𝗻𝗹𝗼𝗮𝗱 𝗴𝗲𝗰𝗸𝗼𝗱𝗿𝗶𝘃𝗲𝗿 𝗮𝗻𝗱 𝗰𝗵𝗿𝗼𝗺𝗲𝗱𝗿𝗶𝘃𝗲𝗿

To interact with Firefox and chrome we need geckodriver and chromedriver respectively. The purpose of these drivers are to launch each browsers. Without that, it is not possible to execute Selenium test scripts in browsers as well as automate any web application.

So lets download each of them.
https://chromedriver.chromium.org/downloads
https://github.com/mozilla/geckodriver/releases

Once its finished go to Eclipse and create another folder by right clicking on the project and name it as "WebDriver"

Go to Downloaded web drivers and copy paste them in to this folder.

To set the system properties to these drivers here are the code formats.
```javascript
System.setProperty("webdriver.gecko.driver", "location of gecko driver exe");
System.setProperty("webdriver.gecko.driver", "location of chrome driver exe");
```
To copy the location of each driver right click on the driver and go to properties. you will see the location. You can give either relative path or the absolute path.

####𝟲.𝗪𝗿𝗶𝘁𝗲 𝗮 𝗽𝗿𝗼𝗴𝗿𝗮𝗺 𝘁𝗼 𝗼𝗽𝗲𝗻 𝗕𝗿𝗼𝘄𝘀𝗲𝗿

Lets write a simple program to open the browser in both chrome and Firefox.

First go to your package, do a right click and create another package inside this package and name it as "Test".

In the Test package again do a right click and create a class, name it as "SeleniumTest1".

Copy the following code  in to the class.

```javascript
package SeleniumTest1;

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

public class SeleniumTest1 {

	static String browser = "";
	static WebDriver driver;

	public static void main(String[] args) {

		setBrowser();
		setBrowserConfig();
		runTest();

	}

	public static void setBrowser() {

		browser = "chrome";

	}

	public static void setBrowserConfig() {

		if (browser.contains("firefox")) {
			//Arrange the system properties for the firefox browser
			System.setProperty("webdriver.gecko.driver", "D:\\seleniumTests\\inprogress\\SeleniumTest\\WebDriver\\geckodriver.exe");
			//Initialize the firefoxdriver
			driver = new FirefoxDriver();
		}

		if (browser.contains("chrome")) {
			//Arrange the system properties for the chrome browser
			System.setProperty("webdriver.chrome.driver", "D:\\seleniumTests\\inprogress\\SeleniumTest\\WebDriver\\chromedriver.exe");
			//Initialize the chromedriver
			driver = new ChromeDriver();
		}

	}

	public static void runTest() {

		//Maximize the chrome browser
		driver.manage().window().maximize();

		driver.get("https://ikman.lk/");
		//Close the browser
		driver.quit();

	}

}	

```
To import all missing packages press: CTRL + SHIFT + O 

To switch between two browsers replace the browser string variable with one of them. “chrome” or “firefox”.
```javascript
public static void setBrowser() {

        browser = "firefox";
```
Now here you see we have used a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
```javascript
        setBrowser();
        setBrowserConfig();
        runTest();
```
We have divided the code into three simple functions and call each one when the test runs. As a best practice its recommended to use this method because it allows for easier management and we need to maintain the efficiency of the code. Now you can see our code is so much clean. 

So here we created our simple selenium test to open a web browser and applied Modular programming software design technique to build the code. 

For your reference find my GitHub profile [here](https://github.com/mwickramanayaka/CodeModular.git)
 

####𝑻𝑯𝑰𝑵𝑲 𝑸𝑼𝑨𝑳𝑰𝑻𝒀!####