DEV Community

Cover image for Lets write our first Selenium test with the help of Code Modular
Manul wickrmanayaka
Manul wickrmanayaka

Posted on • Updated on

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

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

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

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

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

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

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.

System.setProperty("webdriver.gecko.driver", "location of gecko driver exe");
System.setProperty("webdriver.gecko.driver", "location of chrome driver exe");
Enter fullscreen mode Exit fullscreen mode

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.

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();

    }

}   

Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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

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

Top comments (0)