Test Automation with Selenium | Step by step Selenium Installation

Selenium has become an indispensable tool in software test automation today. Let's take a look at the foundation of this tool, as it is widely preferred by many companies and individuals.

What is Selenium?

Selenium is an open-source automation testing tool. While it is commonly used for interactive testing through web browsers, Selenium's WebDriver library, in particular, allows interaction with various types of applications. Therefore, Selenium's scope is not limited to web applications alone. It has the capability to interact between browsers, simulate user interactions, and support various application types. Selenium can also be used for mobile applications (especially with Appium), API testing (it doesn't perform direct API testing but can be combined with other tools like RestAssured for API testing), GUI applications (especially for testing GUI applications supporting the Java programming language, such as Swing and JavaFX), and game applications (especially for testing web-based games).

The broad scope and open-source nature of Selenium enable effective use across various application types by supporting different test scenarios. Selenium is compatible with various programming languages, including Java, Python, C#, Ruby, and can be integrated with popular testing tools and frameworks.

Selenium supports different browsers and can be used on various operating systems. It is compatible with browsers like Chrome, Firefox, Safari, and starting from Selenium version 4, it allows you to configure the necessary driver for your project without downloading an external browser driver separately.

Automation and the Importance of Selenium

Selenium is essential for automating and expediting test processes. Automated tests enhance software quality, reduce errors, and support continuous integration processes. They are particularly useful for repetitive tasks and in areas where human oversight might lead to missed points. Selenium is often preferred for maintenance and regression tests. Its usage can be adapted to various test levels based on your project or organizational structure.

Selenium also comes with several advantages:

  • Cross-browser compatibility
  • High flexibility and a wide user base
  • Open-source and free availability

Relationship Between Selenium and Other Tools

Creating an automation project requires more than just Selenium. Robust test automation projects can be built by using tools like TestNG, JUnit, Maven, among others. Let's briefly explore these concepts.

TestNG: It is a Java-based testing framework used to manage and organize software tests more effectively. Inspired by JUnit, TestNG supports test classes, test groups, dependencies, and parameters, allowing flexible configuration of test suites. It is known for its parallel test execution and reporting capabilities.

For example:

  • @BeforeSuite, @AfterSuite: Code blocks that will run before and after the test suite starts.
  • @BeforeTest, @AfterTest: Code blocks that will run before and after a specific test starts.
  • @BeforeClass, @AfterClass: Code blocks that will run before and after a specific test class starts.
  • @BeforeGroups, @AfterGroups: Code blocks that will run before and after a specific group of tests starts.
  • @BeforeMethod, @AfterMethod: Code blocks that will run before and after each test method.
  • @Test: Defines actual test methods. It also supports features like grouping with the "groups" attribute and parameterization with the "parameters" attribute.

JUnit: JUnit is a testing framework written in the Java programming language. Its primary purpose is to write and run software tests. JUnit is widely used for unit testing in the software development process. It shares similar annotations with TestNG.

For example:

  • @BeforeClass, @AfterClass: Code blocks that will run before and after a specific test class starts.
  • @Before, @After: Code blocks that will run before and after each test method.
  • @Test: Defines actual test methods. Test methods are automatically detected and executed by JUnit.

Maven: Maven is a project management tool used to manage, configure, and handle dependencies for Java projects. It automates tasks such as project compilation, dependency resolution, test execution, packaging, and overall project lifecycle management. Maven uses an XML-based structure and has become a standard for many Java projects.

Selenium Installation

To create automation projects using Selenium and Java, you need to add some dependencies to your project. These can be added to your project from the Maven Repository site. To use Selenium on your computer, you need to install the Java SDK. If you are using Selenium 4 or later, having Java SDK 8 or higher will minimize compatibility issues.

The basic dependencies include:

  • Selenium WebDriver: The core library used to interact with browsers.
  • TestNG (Optional): A Java-based testing framework for effective test management.
  • JUnit (Optional): A testing framework for Java used to write and run software tests.

Installation of Required Applications

You can develop your projects using either IntelliJ IDEA Community Edition or Eclipse IDE for Java Developers. The choice is yours; both are commonly used. After making your choice, there are some necessary steps to follow, and I'll guide you through them.

Firstly, you need to download JAVA JDK to your computer. Search on Google as follows and access the Oracle site.

After entering the website, any JDK can be selected as JAVA 8 and above support all JDKs. I recommend version 11.

After clicking on the relevant area, you will see many options for MAC and WINDOWS. Choose according to your operating system.

Run the downloaded file and complete the installation by clicking NEXT without selecting anything else.

After installation, let's check if it has been successfully installed in the relevant area. Go to System C.

Go into Program Files.

There should be a folder named Java, go into it.

The downloaded Java versions will be listed here. I have a few, it doesn't matter, but you must have the version you downloaded. If there are extras, you might have downloaded them earlier, it doesn't matter. We will use version 11 in our projects, that's what matters.

We need to add Java's path to our computer. For example, the path for the JDK-11.0.12 I downloaded is: C:\Program Files\Java\jdk-11.0.12 Note this by determining your own. Type "Edit The System Environment" or "Sistem Ortam Değişkenleri" in the search area of your computer and open the relevant computer settings. Click on the marked area below.

Click on New.

Fill in the fields as shown below; the name must be JAVA_HOME. The Value should be the location of the Java you downloaded. Enter the information and press OK.

You will see that it has been added as shown below. Close the windows by clicking OK.

Now, let's download our IDE. You can download any of the ones mentioned above. For now, I will continue with projects using IntelliJ. Below, I will also share visuals for Eclipse.

IntelliJ IDEA Installation

After downloading your program, create a new project. Choose SDK 11. Your project will be a Maven project. Specify the project name and location.

Create your project, and a pom file will be created as shown below.

Eclipse

First, we will create a Maven project in our project. Right-click on your project -> Configure -> Convert to Maven Project.

You can write Group id, Artifact id, and Version as you like. It's up to you. After determining, click OK.

A pom file will be created as shown below.

We haven't added anything related to Selenium to our project yet. Now we will handle these parts. MAVEN is primarily a structure automation tool used for Java projects. MAVEN is a central repository for Java-based projects. We will include the relevant libraries we will use in our project from here.

MAVEN Repository Installation

Search for MAVEN Repository on Google. Click on the relevant link.

Here, type selenium in the search area and click on the marked area. Selenium Java

Choose version 4.6.0. It is recommended for Selenium 4 compatibility with W3C, ensuring smoother passage of browser tests. Also, in the latest version, the WebDriverManager Bonigarcia extension comes with Selenium. This means that we won't need to make an additional definition.

Copy the marked code below and import it into our project. All necessary components will be automatically loaded into our project.

Now, we will create a space in our pom file to add our MAVEN projects. We will write MAVEN project scripts in this space. <dependencies> We will write the scripts of our MAVEN projects in this range. </dependencies>

Take the script of our Maven project named Selenium Java and paste it into this range. For those using Eclipse, you can press CTRL+S for the pom file to download and save the components. The project components will be automatically installed. For IntelliJ, you only need to right-click -> Maven -> Reload project.

Your components will be added as shown below.

Similarly, download TestNG from Maven Repository and add it to your pom file.

After making all the additions, save your pom.xml file again or reload it.

We have completed all installations. Now you can write your first automation.

Types of Automation and Examples

When we say Selenium, there are other concepts that come to mind: Cucumber, Gherkin, BDD, TDD, Page Object Model (POM), etc. Let's take a look at these concepts.

BDD and TDD

Behavior Driven Development (BDD) and Test Driven Development (TDD) are methodologies that guide the software development process. BDD emphasizes understanding, defining, and testing the behaviors of software. TDD advocates writing tests as part of the software development process, and these tests are written in advance to ensure that the code is written to pass them.

Cucumber and Gherkin

Cucumber and Gherkin are fundamental to the Behavior Driven Development (BDD) approach. Gherkin is a scenario language written in a language that humans can understand. Cucumber is used to execute scenarios written in this language. For example: Feature: Calculator Scenario: Add two numbers Given the first number is 5 And the second number is 7 When I add the numbers Then the result should be 12 This method is written in a language that people can understand.

Page Object Model (POM)

It is a design pattern used to better organize test scenarios and pages. This design assumes a structure where each page is represented by a page object and all interactions with the page are managed through this object. When all these concepts are combined, let's summarize the table that emerges. Cucumber and Page Object Model (POM) are different concepts. Both are structures and design patterns used in software test automation, serving different purposes.

Cucumber: Used to express scenarios in natural language and to associate these scenarios with automated tests. Cucumber helps to clearly define the expected behaviors of the software and convert these behaviors into test scenarios.

Page Object Model (POM): This is a design pattern, especially used with web automation tools like Selenium. POM suggests using classes that represent pages of a web application and methods that interact with these pages. This improves code readability, makes maintenance easier, and minimizes the impact of page changes. In other words, while Cucumber is used to define and test scenarios in a natural language, POM is used to organize pages and methods that interact with these pages in a more structured and modular way. Combining Cucumber scenarios with Page Object Model (POM) is quite common and is often used in large, complex test automation projects. This way, Cucumber scenarios can be organized in a more structured and readable way. Creating a separate Page Object Model class for each page or component makes maintenance and extension of the code easier.

Wishing you successful results in your tests.

Author: Melih Can Demirtel