• Selenium Video Tutorials

Selenium - IntelliJ



We would need to configure Selenium with an editor to execute the automated tests. There are several editors available in the market for example: Eclipse, IntelliJ, Atom, and so on. Using these editors, we can start working on a Java project to start our test automation. Today, we will discuss how to set up our Selenium code in IntelliJ.

Lets begin −

Step 1 − Navigate to the official website of IntelliJ (which is a product of Jetbrains) using the below link and then click on Download −

https://www.jetbrains.com/idea/.

Selenium IntelliJ 1

Step 2 − Once we navigate to the next page, we will get the option to download IntelliJ in various operating systems, like Windows, macOS, and Linux. Click on the tab based on the operating system you are currently using in your system.

IntelliJ comes in two versions, Paid and Community (which is free).

Selenium IntelliJ 2

We would download the Community version. For that we would navigate to the IntelliJ IDEA Community Edition section and then click on Download.

Selenium IntelliJ 3

After downloading was completed in the macOS, we dragged the IntelliJ IDEA CE file from the Downloads folder to Applications folder.

Step 3 − IntelliJ logo should display for a few seconds, and next the JETBRAINS COMMUNITY EDITION TERMS should appear. Click the checkbox to accept the terms and conditions, then click on Continue.

Step 4 − Welcome to IntelliJ IDEA should appear. Clicked on the New Project button.

Step 5 − Enter a name under Name: field. Selected Language as Java, Build System as Maven, and JDK version, then click on Create.

Step 6 − Entered an ArtifactId and then click on Create.

Step 7 − IntelliJ editor setup should be completed successfully.

Step 8 − Next, we woud add the Selenium Maven dependencies from the below link −

https://mvnrepository.com

Selenium IntelliJ 4

Step 9 − Selected and clicked on a version link under the Central tab. We navigated to the Selenium Java >> <version> page. Copied the dependency under Maven tab.

Selenium IntelliJ 5

Dependency example −

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>4.11.0</version>
</dependency>

Step 10 − Pasted the dependency copied in the Step 9 in the pom.xml file (available under the Maven Project created in the IntelliJ workspace).

Step 11 − Added the below code in the Main.java file.

Example

package org.example;

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

public class Main {
   public static void main(String[] args) throws InterruptedException {
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.google.com");
      System.out.println("Browser title: " + driver.getTitle());
   }
}

Dependencies added in pom.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
   </dependencies>
</project>

Step 12 − Right click and select Run ‘Main.main()’ option. Wait till the run has completed.

Step 13 − Chrome browser got launched, and we had got the output in the console- Browser Title: Google with the message Process finished with exit code 0, signifying successful execution of the code.

Along with that Chrome browser got launched with the message Chrome is being controlled by automated test software.

Thus we had successfully configured Selenium with IntelliJ in this tutorial.

Advertisements