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 the selenium official site(www.selenium.dev) you will find it says โselenium automates browsers. Thatโs it.โ
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.
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.
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
๐ป๐ฏ๐ฐ๐ต๐ฒ ๐ธ๐ผ๐จ๐ณ๐ฐ๐ป๐!