Selenium consists of a range of tools & libraries for automation of web browsers.
Selenium is mainly used as a testing automation tool, mainly for web-based applications. If you go to the selenium website the definition they give is, “Selenium automates browsers. That’s it.” Yes! Selenium can be used for automating various web-based tasks! Being open source (yay! It’s free), you get to customize it for your needs!
We use selenium Web drivers for browser automation. Web driver, an object-oriented interface, is used for writing instructions that can be used across browsers. That is yet another feature of selenium, it supports cross-OS platforms (Mac, Linux, Windows etc.) as well as cross browsers (IE, Edge, Chrome, Firefox, Opera, Safari etc.) With Selenium web Driver, there is no need for a server, the scripts written can be directly executed in the browser.
Another main advantage of using selenium is you can use different languages like Ruby, Java, Python, dot net, C# etc.
One of the popular combinations is Selenium with Java. Lots and lots of job openings are there for Selenium with Java. Even with other programming languages/ Technologies, syntax will be more or less the same.
One thing to note is, with selenium we cannot generate the reports directly, but we need a third-party tool like testNG for generating reports.
We use selenium Web drivers for browser automation. Web driver, an object-oriented interface, is used for writing instructions that can be used across browsers. That is yet another feature of selenium, it supports cross-OS platforms (Mac, Linux, Windows etc.) as well as cross browsers (IE, Edge, Chrome, Firefox, Opera, Safari etc.) With Selenium web Driver, there is no need for a server, the scripts written can be directly executed in the browser.
Another main advantage of using selenium is you can use different languages like Ruby, Java, Python, dot net, C# etc.
One of the popular combinations is Selenium with Java. Lots and lots of job openings are there for Selenium with Java. Even with other programming languages/ Technologies, syntax will be more or less the same.
One thing to note is, with selenium we cannot generate the reports directly, but we need a third-party tool like testNG for generating reports.
Step 1: Download Selenium Jar files.
Go to link www.selenium.dev and click on download web driver.
Go to Selenium Client & Web driver language bindings. You can see that JAR files are available for all the different programming languages. For this tutorial, lets download Selenium with Java.
You can see stable versions as well as alpha versions. Alpha versions may have some defects and may not have proper functionalities. Also, you can find API docs and change logs.
Download the latest stable version from the link. And extract the files in a specific path. We might need these files again and again, so keeping them in one place will make things handy.
STEP 2: Configure the Selenium JARs to the working project.
To add the downloaded JAR files to the project in Eclipse, select the project, right-click and select properties. In the properties, select Java build path and navigate to libraries tab.
Select the class path (you may not find this option in Java 8 and hence has to be done directly under libraries) and click on Add external JARs.
Select all the downloaded Selenium Jars, add it, apply and close. Once you have done this step, you can see your JAR files in the Referenced Libraries on the package explorer.
These JAR files can be accessed by all the packages in that particular project. Every time you create a new project, the selenium JAR file configuration has to be repeated. This means it is applicable only at project level.
STEP 3: Downloading the Browser Drivers
Each browser has a different browser server. Go to link www.selenium.dev/downloads and click on download browser driver. You have different drivers for different browsers.
Extract the browser driver in your preferred location.
Alternatively, you can do a search and get the browser web drivers. One thing you have to note is the version of browser driver and the version installed in your machine should be the same or a lower version. Higher versions won’t be compatible.
STEP 4: Launching the browser
We are using selenium with java. So, let’s create a java program in any package under this same project.
To launch a browser, we need to instantiate the preferred browser driver.
For that, we need to set the system property.
For chrome driver,
system.setproperty(“webdriver.chrome.driver”,”path of chrome driver in your machine”);
Now to access the browser, create the object of the class. There are predefined classes inside the selenium interface.
Let’s create the object for the class Web driver.
WebDriver driver= new ChromeDriver();
Here driver is the object. It gives the reference to chrome driver.
On executing the code, your chrome browser will be launched. You will see a message in the browser stating, it is controlled by automated test software.
STEP 5: Opening An URL
Using the driver object, we can navigate to the specified URL. We will use get method from class webdriver to open the URL.
driver.get(“complete path of the URL as string input”);
For example, to open google, the code will be
driver.get(https://google.com/);
On execution, you will see the browser will be launched and the google home page will be opened.
This is the basic steps of browser automation and these scripts remain the same.
For Firefox, the codes will be
system.setproperty(“webdriver.gecko.driver”,”path of firefox driver in your machine”);
WebDriver driver= new FirefoxDriver();
driver.get(https://google.com/);
For Internet Explorer, the codes will be
system.setproperty(“webdriver.IE.driver”,”path of IE driver in your machine”);
WebDriver driver= new InternetExplorerDriver();
driver.get(https://google.com/);
You can see the demo tutorial of the same in this video.
For navigation in browser, you can use the following commands:
To navigate to a different URL:
driver.navigate().to(“complete url”);
For going forward in browser
driver.navigate().forward();
For going back in browser
driver.navigate().back();
For refreshing the browser page
driver.navigate().refresh();
Want to know more about browser automation? New batches starting soon for our selenium automation testing course. Contact us for enquiries.