Getting started with Selenium

Nov 19
15:47

2020

jayesh jamindar

jayesh jamindar

  • Share this article on Facebook
  • Share this article on Twitter
  • Share this article on Linkedin

Selenium is an open source set of libraries initially developed by ThoughtWorks. These libraries can be used with java, ruby, C#, perl, and other programming languages to interact with the web browser. Over time, selenium has evolved and became a widely used automation tool attributed to the wide community base which contributed to its enhancement, maintenance, and scalability.

mediaimage

GETTING STARTED WITH SELENIUM

 

To start with selenium webdriver,Getting started with Selenium Articles you need to have the following items installed on your machine.
Prerequisite:

JDK (java 1.8 preferable)

Selenium libraries (Nowadays there is single jar 'selenium-server-standalone.jar' is enough unlike earlier period where we need to install multiple jars),

Browser drivers (.exe of chrome driver, ie driver, gecko driver)

 Eclipse IDE


Now that you have downloaded the minimum required elements to start writing selenium code, so let's start by setting up the environment:


Now select a java project:


Give the project name and finish.
Now configure the build path where you need to set your downloaded libraries i.e. selenium jar files, also need to set JDK and JRE path in the java compiler section. Right-click on your project and build path and configure build path.


Now select the libraries tab above and click add external jars. Add your downloaded selenium jars here:



Also now don't forget to set your compliance level as per your JDK and JRE version.
For all your latest version of selenium, make sure the compliance level under the java compiler section must be set to java 1.8 or above. Ignoring this might lead to a "class not found" run time exception during running your selenium test script.


Now that you have set libraries and compliance levels, it's time to write your first selenium code using java.
Right-click on your project and select create a new class.



Give it a name, check "public static void main.." , click finish. You are creating a sample test, where we are not using any testNG, cucumber, or maven therefore we need to include public static void main in our first class to run the test as the java execution will begin through the class where the main method is defined. In subsequent lectures, when we use testNG, cucumber, Maven, we will not use or include the main method as our execution will be taken care of by testNG or cucumber.


Now it's time to write our first test script which simply opens the browser and navigate to the site specified in the script. We'll write the code and understand the meaning of each one of them.
In our class, first include the required packages and classes which come with selenium libraries.



Now at class level, that is inside the class but before the main method, declare a webdriver instance variable:

Webdriver driver;

Now inside our main method, write code to set system property:



Here setProperty method is accepting two parameters in the form of key and value. The first one is the string parameter in which we are telling that we are going to use a chrome driver, and the second is also a string parameter where we are placing an entire path till the driver's exe.
Now in the next line, we've initialized the driver with chromeDriver. This piece of the line will open the chrome browser upon execution. Now our browser is open, and we want to navigate to any site. This task will be done by driver.get(""); method. So driver.get("") will land us to the specified URL.

INTERVIEW TIP: Why we cannot write Webdriver driver=new Webdriver();


Since webdriver is an interface therefore we can create its instance variable but cannot create its object. i.e. we cannot write Webdriver driver=new Webdriver(); this will throw error. We can rather write Webdriver driver =new FirefoxDriver(); or Webdriver driver=new ChromeDriver(); or Webdriver driver =new InternetExplorerDriver(); These FirefoxDriver, ChromeDriver, and InternetExplorerDriver are the actual classes which implement the webdriver interface and provide implementation to its methods. So we are assigning the instance variable of webdriver to its implementation classes.

 

 

SUMMARY


In this lecture, we've installed basic minimum items required for running and executing our first selenium script. We've installed JDK, eclipse, selenium libraries, drivers exe's for browsers. We've created a java project, set all the libraries in our project' build path, created a class, and imported the packages and classes needed to execute our test script. We've written the code to set system property, initialized our driver object, open the browser, and finally lands at the site specified in the driver.get("").


Also From This Author

Software Testing as a Career- A beginner's guide

Software Testing as a Career- A beginner's guide

This article is a beginner's guide for those who want to start their career in software testing. Software testing is one of the crucial phases of the software development life cycle. Software testing has evolved significantly over time. Earlier it was considered as an inferior job type as there is hardly any creativity, learning, and value involved in it. But over time, the testing is not just limited to manual validation of the application under test.
Mastering Performance Testing with JMeter

Mastering Performance Testing with JMeter

Discover the essentials of performance testing with JMeter, an open-source Java tool from Apache Software Foundation. JMeter is designed to analyze and measure the performance of various applications by simulating loads on servers. It supports testing for web applications using HTTP/HTTPS, web services via SOAP and REST, databases through JDBC, and more. With its user-friendly GUI, JMeter allows testers to easily add and configure components such as Samplers, Config elements, Listeners, Controllers, and Assertions.
Test Automation Architecture

Test Automation Architecture

A test automation architecture is the organization and abstraction of test scripts, test data, and business logic. An efficient test automation architecture is one that has all its layers loosely coupled and scalable. In this article, we'll understand the POM design pattern and its implementation. The page functions and logic are abstracted from test scripts in POM, moreover, it is easy to maintain and scale. It is the application of different tools, methods, and techniques in an efficient way to accomplish varied testing goals.