Test Automation

Setup Selenium in IntelliJ with TestNG Framework

Setup Selenium in IntelliJ is quite simple process if you have understanding of Selenium, which is an open-source automation testing tool used for automated web application testing. It can be used to automate tasks on web browsers such as opening a browser, entering data, clicking buttons and so on. Integrating Selenium with the IntelliJ IDE makes it easier to debug your code and get feedback about errors. In addition, TestNG is a powerful testing framework that can be used to extend the capabilities of Selenium.

Selenium Installation

Following are the steps you need to install Selenium on you machine.

Install Java Development Kit (JDK)

Selenium is primarily used with programming languages like Java, so you need to have Java installed on your system.

  1. Check if Java is Installed: Open your terminal (on macOS and Linux) or Command Prompt (on Windows) and type: java -version If you see a version number, Java is already installed. If not, proceed to the next step.
  2. Install Java: Visit the Oracle JDK or OpenJDK website to download and install the latest version of Java for your operating system.

Set Up Java Environment Variables

After installing Java, you’ll need to set up environment variables to ensure Selenium can locate the Java executable.

  1. Find Java Installation Path: Note down the path where Java is installed on your system.
  2. Set JAVA_HOME Variable (Windows):
    • Right-click on “This PC” or “My Computer” and select “Properties.”
    • Click on “Advanced system settings” on the left sidebar.
    • Click the “Environment Variables” button.
    • Under “System Variables,” click “New.”
    • Enter “JAVA_HOME” as the variable name and the path to your Java installation as the variable value.
    • Click “OK.”
  3. Add Java to PATH (Windows):
    • Locate the “Path” variable under “System Variables” and click “Edit.”
    • Add the path to the “bin” directory within your Java installation (e.g., C:\Program Files\Java\jdk1.8.0_291\bin) to the list of values.
    • Click “OK” to save your changes.

IntelliJ Installation

Here are very precise steps to download and install IntelliJ IDEA:

Windows:

  1. Visit the official JetBrains IntelliJ IDEA download page: https://www.jetbrains.com/idea/download/.
  2. Under the “Community” edition (free), click the “Download” button.
  3. Once the download is complete, run the downloaded installer.
  4. Follow the on-screen instructions. You can choose the installation location and whether to create desktop shortcuts.
  5. During installation, you might be asked to choose a UI theme. The default “Light” theme is a good starting point, but you can always change it later.
  6. After installation is complete, launch IntelliJ IDEA.

These steps should help you download and install IntelliJ IDEA on your respective operating system. If you are using selenium with pythong using IDE Pycharm,do visit this link.

Install Selenium with a Programming Language

Developers use selenium with various programming languages. Here, we’ll briefly cover the installation process for Java, Python, and JavaScript.

Maven and pom.xml

Maven is a build automation tool commonly used with Java-based projects. It uses project object model (POM) files written in XML format to store information about the project and its dependencies. To use Maven with IntelliJ and Selenium, you will need to create a POM file. This file should include information about the project’s name, version number, source directories, etc. as well as details of the dependencies required for running Selenium tests.

Java (Maven)

If you’re using Java, it’s common to manage Selenium dependencies with Maven. Create a Maven project in your preferred IDE (e.g., IntelliJ IDEA, Eclipse) and add the following dependencies to your pom.xml file:

<dependencies>
    <!-- Selenium WebDriver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
</dependencies>

Download Selenium WebDriver

Selenium WebDriver is the core component of Selenium that allows you to interact with web browsers. You can download WebDriver for your preferred browser(s) from the Selenium Downloads page.

  1. Go to the Selenium Downloads page.
  2. Scroll down to the “WebDriver” section.
  3. Choose the WebDriver for the browser(s) you intend to automate (e.g., Chrome, Firefox, Edge).
  4. Download the WebDriver executable for your specific browser and operating system.

TestNG Plugin installation in IntelliJ

TestNG is a testing framework that makes it easier to write tests for Selenium automation. To install TestNG in IntelliJ, first, go to the main menu and select ‘File -> Settings’. Then select ‘Plugins’ on the left-hand side of the window. In the search bar, type ‘TestNG’ and click on the Install button for the ‘TestNG-J’ plugin. This will install TestNG in IntelliJ and enable you to begin writing tests with Selenium.

Verify Selenium Installation

To ensure that Selenium is properly installed and configured, you can create a simple test script to open a web browser using Selenium WebDriver and interact with a web page.

Here’s an example using Java:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {
    public static void main(String[] args) {
        // Set the path to the Chrome WebDriver executable
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // Create a new instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Open a web page
        driver.get("https://www.example.com");

        // Perform interactions and assertions here

        // Close the browser
        driver.quit();
    }
}

In above code we need to download required driver on some location and set it using System.setProperty command, but if you place the driver exe in the project then there is no need to use this command. Another way to set driver configuration in the pom.xml like

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.5.3</version>
</dependency>

Practical Demo with Code

Once you have all the necessary components installed and configured, it is time to start writing your tests in IntelliJ. Here is a simple test in Java using Selenium and TestNG:

public class SampleTest {
private WebDriver driver;
@BeforeClass
public void setUp() {
// Initiate WebDriver with Chrome Driver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void sampleTest() {
// Open URL in Browser
driver.get("https://www.example.com");
// Assert page title is correct
Assert.assertEquals(driver.getTitle(), "Example Website!");
}
}

By running the above code, you will open a browser window and navigate to the specified website. The test will then check that the page title is ‘Example Website!’ and fail if it is not.

Related Post: Difference Between XPath and CSS Selector

Conclusion

Selenium can be a powerful tool for automated web testing, especially when used with IntelliJ and TestNG. Installing all the necessary components and setting up a project may seem like a daunting task at first, but once you understand the basics, it is quite simple. With this tutorial, you should now have a good understanding of how to set up Selenium with IntelliJ and TestNG as well as some practical examples.

Rashid Ali

I am an IT professional with 10 years of experience in the field of Software development. My track record of satisfied clients speaks to my proficiency in delivering top-notch services in QA manual and Automation, IT support services, Blogging , and On-page SEO.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button