Day 12: Start automating - Selenium Java Maven with TestNG Project

Day 12: Start automating - Selenium Java Maven with TestNG Project

To set up a Maven Selenium TestNG project in IntelliJ, you'll need to follow these steps:

Step 1: Install Maven

  1. Download the latest version of Maven from the Apache Maven website (https://maven.apache.org/download.cgi).
  2. Extract the downloaded archive to a directory of your choice.
  3. Add the Maven bin directory to your system's PATH variable.

To add the Maven bin directory to your system's PATH variable, follow these steps:

  1. Determine the Maven bin directory: The bin directory is the one that contains the Maven executable (mvn).

  • If you extracted Maven to a specific directory, the bin directory is located at the path where you extracted Maven. For example, if you extracted Maven to C:\apache-maven-3.8.4, the bin directory is C:\apache-maven-3.8.4\bin.
  • If you installed Maven using a package manager or an installer, the bin directory is typically located at C:\Program Files\Apache Maven\apache-maven-3.8.4\bin on Windows or /usr/share/maven/bin on Linux/macOS.

  1. Set the PATH variable on Windows:

  • Open the Start menu and search for "Environment Variables".
  • Select "Edit the system environment variables" to open the System Properties dialog.
  • Click on the "Environment Variables" button.
  • In the "System Variables" section, scroll down and find the "Path" variable. Select it and click on the "Edit" button.
  • In the "Edit Environment Variable" dialog, click on the "New" button.
  • Enter the path to the Maven bin directory (e.g., C:\apache-maven-3.8.4\bin) and click "OK" to add it.
  • Click "OK" again to close the Environment Variables dialog and then click "OK" on the System Properties dialog.

  1. Set the PATH variable on Linux/macOS:

  • Open a terminal.
  • Open your shell configuration file (~/.bashrc or ~/.bash_profile or ~/.zshrc).
  • Add the following line at the end of the file:

export PATH="/usr/share/maven/bin:$PATH"

  • Save the file and close the editor.
  • Run source ~/.bashrc (or the corresponding command for your shell config file) to apply the changes to your current terminal session.

  1. Verify the PATH configuration:

  • Open a new terminal (or restart the current one) to ensure the changes take effect.
  • Run mvn -v (or mvn --version) to check if Maven is recognized and the version is displayed.
  • If the command is recognized and the version is displayed, it means the PATH configuration was successful.

By adding the Maven bin directory to your system's PATH variable, you can run Maven commands from any directory in the command prompt or terminal without specifying the full path to the Maven executable.

Step 2: Create a Maven Project

  1. Open IntelliJ IDEA and select "Create New Project" from the welcome screen.
  2. Choose "Maven" on the left panel and click "Next".
  3. Ensure that the "Create from archetype" option is selected.
  4. Select the "maven-archetype-quickstart" archetype and click "Next".
  5. Enter the GroupId and ArtifactId for your project (e.g., com.example) and click "Next".
  6. Specify the project name and location, then click "Finish" to create the Maven project.

Step 3: Configure Selenium and TestNG Dependencies:

<dependencies>
??<dependency>
????<groupId>org.seleniumhq.selenium</groupId>
????<artifactId>selenium-java</artifactId>
????<version>VERSION_NUMBER</version>
??</dependency>
??<dependency>
????<groupId>org.testng</groupId>
????<artifactId>testng</artifactId>
????<version>VERSION_NUMBER</version>
??</dependency>
</dependencies>

https://mvnrepository.com/ - search for required dependencies and add it to your pom.xml

Open the pom.xml file located in the root of your Maven project.Replace VERSION_NUMBER with the latest version of Selenium WebDriver and TestNG (e.g., 4.1.0 for Selenium and 7.4.0 for TestNG).

  1. IntelliJ IDEA should automatically import the dependencies. If not, click on the "Reimport All Maven Projects" button that appears at the top-right corner of the editor window.

Step 4: Write Selenium TestNG Test

  1. Create a new Java class in the src/test/java directory of your Maven project.
  2. Write your Selenium WebDriver and TestNG test code within the class.

Step 5: Run TestNG Test

  1. Right-click on your test class and select "Run 'ClassName'" to execute the TestNG test.
  2. The TestNG results will be displayed in the IntelliJ IDEA console.

That's it! You've successfully set up a Maven Selenium TestNG project in IntelliJ. You can continue writing your Selenium tests using TestNG annotations and execute them using IntelliJ's TestNG integration.

要查看或添加评论,请登录

Shree Krishna Priya J的更多文章

社区洞察

其他会员也浏览了