Lets write our first Selenium test with the help of Code Modular

Manul Wickramanayaka
Test Automation Hub
4 min readMay 18, 2021

๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ฆ๐—ฒ๐—น๐—ฒ๐—ป๐—ถ๐˜‚๐—บ?

Selenium is a set of tools and libraries that automates web browser actions. If you go to the selenium official site(www.selenium.dev) you will find it says โ€œselenium automates browsers. Thatโ€™s it.โ€

Selenium logo

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 the 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, Let's 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โ€

๐Ÿฎ.๐——๐—ผ๐˜„๐—ป๐—น๐—ผ๐—ฎ๐—ฑ ๐—ฎ๐—ป๐—ฑ ๐˜€๐˜๐—ฎ๐—ฟ๐˜ ๐—˜๐—ฐ๐—น๐—ถ๐—ฝ๐˜€๐—ฒ

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

๐Ÿฏ.๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—ฎ ๐—๐—ฎ๐˜ƒ๐—ฎ ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜ ๐—ถ๐—ป ๐—˜๐—ฐ๐—น๐—ถ๐—ฝ๐˜€๐—ฒ

Right-click on the package Explorer,
New โ†’Java Project
And let's name it as โ€œSeleniumTestโ€

๐Ÿฐ.๐—”๐—ฑ๐—ฑ ๐˜€๐—ฒ๐—น๐—ฒ๐—ป๐—ถ๐˜‚๐—บ ๐—๐—ฎ๐—ฟ๐˜€

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

www.selenium.dev/downloads

www.selenium.dev/downloads

After the 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, let's 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 them to the 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 is to launch each browser. Without that, it is not possible to execute Selenium test scripts in browsers as well as automate any web application.

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

Once it's 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 into this folder.

To set the system properties to these drivers here are the code formats.

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.

๐Ÿฒ.๐—ช๐—ฟ๐—ถ๐˜๐—ฒ ๐—ฎ ๐—ฝ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ ๐˜๐—ผ ๐—ผ๐—ฝ๐—ฒ๐—ป ๐—•๐—ฟ๐—ผ๐˜„๐˜€๐—ฒ๐—ฟ

Let's 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 into the class.

SeleniumTest1.java

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โ€.

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.

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

We have divided the code into three simple functions and call each one when the test runs. As a best practice, it's 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 cleaner.

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

๐‘ป๐‘ฏ๐‘ฐ๐‘ต๐‘ฒ ๐‘ธ๐‘ผ๐‘จ๐‘ณ๐‘ฐ๐‘ป๐’€!

--

--

Manul Wickramanayaka
Test Automation Hub

Full Stack Quality Engineer ๐Ÿ‘จโ€๐Ÿ’ป