Complete Guide For Your First TestNG Automation Script
The love of Automation testers, TestNG, is a Java testing framework that can be used to drive Selenium Automation script.
When it comes to automation, no one can compete with Selenium training.
For a beginner, it becomes hard to understand the point from where he needs to start.
So, in this blog, we have come up with a step by step method to assist you in getting started with your first automation test script.
For this, you need to set up an Test Automation environment for selenium with python online training Eclipse first and then go ahead with the script. Let’s do it.
Open your browser in the next tab and get started for your first automation test script in TestNG. All you need is just 20 minutes.
Setting Up The Environment
Step 1: First of all, download java8 or jdk1.8 and install it in your system.
You can use the link to download java8.
After installing java8, set up the path.
To do so, Go to Start→ Computer→ Right click → properties→ Advanced system settings → Environment variable→ New→ After clicking on that you need to enter variable name and variable path.
You’ll see the following screen in the ‘Environment variable’ window.
Let’s validate if this is properly installed or not.
To do so, open cmd window and type ‘javac’.
Select Eclipse IDE for Java EE Developers and install it.
For this, Go to Help→ Eclipse Marketplace
Step 4: Download Selenium 3.11.0
You can use this link to download: https://www.seleniumhq.org/download/
Step 5: Download Selenium online training India dependency files
Download selenium dependency files for jdk 3.11.0 using the link provided here.
Step 6: Create the project
Create a Java project
To create it, go to file→ New→ Other
Select create a Java Project
Enter your project name, I am entering ‘FirstAutomationProject’.
Note: Do not enter spaces in the name of the project
Create a new TestNG class under the package's new pack. Right-click new pack–> New –> Other
Step 8: Import Selenium online training Hyderabad Jar Files in dependency files
Right-click
Select the .jar file that you’ve downloaded above.
Writing The Code
On the eclipse window that you’re seeing now, write the following code.
We are writing code in order to verify the title of the webpage.
CODE(self-explanatory):
package newpack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class NewTest {
WebDriver driver;
@Test
public void FirstTest() {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
//comment the above 2 lines and uncomment below 2 lines to use Chrome
//System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
//WebDriver driver = new ChromeDriver();
String baseUrl = "https://www.lambdatest.com/";
String expectedTitle = "LambdaTest - Perform all your tests on cloud";
String actualTitle = "";
// launch Chrome and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page with the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Fire fox
driver.close();
}
}After writing the code, save it and right-click on ‘New Test.java’ → Run as→ TestNG Test.
Once you click on it, you’ll see a chrome browser window opening the website that you’ve entered in your code to test. And it will automatically close as soon as the test is completed and in the output section below, you’ll see ‘Test Passed’ as the status of your test on the console below.
With this, you have run your first automation test in TestNG.
To verify the title of a given web-page matches the one already specified.
Nothing matches the feeling you get after doing something new. So, feeling good after executing your first automation script? Let us know your experience in the comment section below for more selenium testing course online.