Selenium is an open-source tool that allows developers to write automated tests for web applications.
What is TestNG?
TestNG (Test Next Generation) is a testing framework inspired from JUnit, but it introduces some new functionalities to make it more powerful and easier to use.
What Is Visual Studio Code?
Visual Studio Code is an IDE and it is modern, lightweight, and powerful source code editor developed by Microsoft
Why Use Visual Studio Code for Selenium and TestNG:
While we work with other IDEs like Eclipse, they often feel heavy; they take a considerable amount of time to open, and during execution, they consume a lot of RAM. In contrast, VS Code is not just an IDE; it’s very lightweight. Using it ensures that your system runs efficiently.
- Lightweight It starts up and works faster than most other Java IDEs.
- Extensibility You can customize your development environment with lots of extensions in VS Code.
- Cross-platform: You can use VS Code on Windows, Linux, and macOS.
- Integrated Git Control: It makes version control and collaboration easier with integrated Git control.
- User-Friendly Interface The user interface looks modern and is easy to use.
Step 1 : Download and Install Java and Set up JAVA_HOME in an environment variable.
Step 2: Download and Install Maven and Set up MAVEN_HOME in an environment variable.
Step 3: Download and install VSCode.
Setting up Visual Studio Code for Java
Open VSCode, go to Extensions (Ctrl+Shift+X) and search for Java Extension Pack. Install this extension pack. The extension pack includes all the necessary extensions for Java development, such as Language Support for Java, Java Test Runner, and Maven for Java.
How to Create a Maven Project:
In VSCode, press Ctrl+Shift+P to open the command palette.b. select Maven and type:Create Maven Project.c. Choose maven-archetype-quickstart.
Adding Selenium and TestNG Dependencies
Edit the pom.xml of your Maven project and add the following dependencies:
TestNG Annotations Overview:
The @BeforeTest, @AfterTest, and @Test annotations are part of TestNG, a testing framework. Let’s understand their significance:@BeforeTest:The method annotated with @BeforeTest will run before any test method belonging to the classes inside the <test> tags in the TestNG suite is executed.It is usually used to set up the initial settings, configurations, or prerequisites before executing the test methods.
@AfterTest:The method annotated with @AfterTest will run after all the test methods belonging to the classes inside the <test> tags in the TestNG suite have executed.It’s commonly used to tear down or clean up activities after the execution of tests, like closing database connections, closing browsers, etc.
@Test:The @Test annotation tells TestNG that the method below is a test method. This method will be executed as part of the test.You can also add attributes to the @Test annotation to control test execution, like setting priorities, including groups, etc.
@BeforeSuite & @AfterSuite: Runs before/after all tests in this suite have run.@BeforeClass & @AfterClass: Runs before/after the first/last test method in the current class is invoked.@BeforeMethod & @AfterMethod: Runs before/after each test method.
Writing a Sample Selenium Test with TestNGCreate a new Java class in src/test/java named SampleTest.java:
Running Your TestIn the integrated terminal of VSCode, navigate to your project’s root directory and run:
Maven will compile and run your tests using TestNG.
Setting up Java, Selenium, and TestNG in VSCode is straightforward, especially with the help of Maven. This setup allows for a streamlined development and testing environment, making web automation efficient and robust. Happy testing!