๐ช๐ต๐ฎ๐ ๐ถ๐ ๐ฆ๐ฒ๐น๐ฒ๐ป๐ถ๐๐บ?
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."
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"
๐ฎ.๐๐ผ๐๐ป๐น๐ผ๐ฎ๐ฑ ๐ฎ๐ป๐ฑ ๐๐๐ฎ๐ฟ๐ ๐๐ฐ๐น๐ถ๐ฝ๐๐ฒ
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.
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");
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();
}
}
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 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)