Creation of Sample TestNG project
Let us begin with the creation of TestNG project in eclipse IDE.
Step 1: Click on the File option within the menu -> Click on New -> Select Java Project.
Step 2: Enter the project name as “DemoTestNG” and click on “Next” button. As a concluding step, click on the “Finish” button and your Java project is ready.
Step 3: The next step is to configure the TestNG library into the newly created Java project. For the same, Click on the “Libraries” tab under Configure Build Path. Click on “Add library” as shown below.
Step 4: The user would be subjected with a dialog box promoting him/her to select the library to be configured. Select TestNG and click on the “Next” button as shown below in the image. In the end, click on the “Finish” button.
The TestNG is now added to the Java project and the required libraries can be seen in the package explorer upon expanding the project.
Add all the downloaded Selenium libraries and jars in the project’s build path as illustrated in the previous tutorial.
Creating TestNG class
Now that we have done all the basic setup to get started with the test script creation using TestNG. Let’s create a sample script using TestNG.Step 1: Expand the “DemoTestNG” project and traverse to “src” folder. Right click on the “src”package and navigate to New -> Other..
Step 2: Expand TestNG option and select “TestNG” class option and click on the “Next” button.
Step 3: Furnish the required details as following. Specify the Source folder, package name and the TestNG class name and click on the Finish button. As it is evident from the below picture, user can also check various TestNG notations that would be reflected in the test class schema. TestNG annotations would be discussed later in this session.
The above mentioned TestNG class would be created with the default schema.
Now that we have created the basic foundation for the TestNG test script, let us now inject the actual test code. We are using the same code we used in the previous session.
Scenario:
- Launch the browser and open “gmail.com”.
- Verify the title of the page and print the verification result.
- Enter the username and Password.
- Click on the Sign in button.
- Close the web browser.
1 | package TestNG; |
2 | import org.openqa.selenium.By; |
3 | import org.openqa.selenium.WebDriver; |
4 | import org.openqa.selenium.WebElement; |
5 | import org.openqa.selenium.firefox.FirefoxDriver; |
6 | import org.testng.Assert; |
7 | import org.testng.annotations.Test; |
8 |
9 | public class DemoTestNG { |
10 | public WebDriver driver = new FirefoxDriver(); |
11 | String appUrl = "https: //accounts.google.com"; |
12 |
13 | @Test |
14 | public void gmailLogin() { |
15 | // launch the firefox browser and open the application url |
16 | driver.get("https: //gmail.com"); |
17 | |
18 | // maximize the browser window |
19 | driver.manage().window().maximize(); |
20 | |
21 | // declare and initialize the variable to store the expected title of the webpage. |
22 | String expectedTitle = " Sign in - Google Accounts "; |
23 | |
24 | // fetch the title of the web page and save it into a string variable |
25 | String actualTitle = driver.getTitle(); |
26 | Assert.assertEquals(expectedTitle,actualTitle); |
27 | |
28 | // enter a valid username in the email textbox |
29 | WebElement username = driver.findElement(By.id("Email")); |
30 | username.clear(); |
31 | username.sendKeys("TestSelenium"); |
32 |
33 | // enter a valid password in the password textbox |
34 | WebElement password = driver.findElement(By.id("Passwd")); |
35 | password.clear(); |
36 | password.sendKeys("password123"); |
37 | |
38 | // click on the Sign in button |
39 | WebElement SignInButton = driver.findElement(By.id("signIn")); |
40 | SignInButton.click(); |
41 | |
42 | // close the web browser |
43 | driver.close(); |
44 | } |
45 | } |
1) @Test – @Test is one of the TestNG annotations. This annotation lets the program execution to know that method annotated as @Test is a test method. To be able to use different TestNG annotations, we need to import the package “import org.testng.annotations.*”.
2) There is no need of main() method while creating test scripts using TestNG. The program execution is done on the basis of annotations.
3) In a statement, we used Assert class while comparing expected and the actual value. Assert class is used to perform various verifications. To be able to use different assertions, we are required to import “import org.testng.Assert”.
Executing the TestNG script
The TestNG test script can be executed in the following way:=> Right click anywhere inside the class within the editor or the java class within the package explorer, select “Run As” option and click on the “TestNG Test”.
TestNG result is displayed into two windows:
- Console Window
- TestNG Result Window
(Click on image to view enlarged)
HTML Reports
TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browser and it can also be viewed using Eclipse’s build –in browser support.To generate the HTML report, follow the below steps:
Step 1: Execute the newly created TestNG class. Refresh the project containing the TestNG class by right clicking on it and selecting “Refresh” option.
Step 2: A folder named as “test-output” shall be generated in the project at the “src” folder level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.
Step 3: The HTML report shall be opened with in the eclipse environment. Refer the below image for the same.
Refresh the page to see the results for fresh executions if any.
Setting Priority in TestNG
Code Snippet1 | package TestNG; |
2 | import org.testng.annotations.*; |
3 | public class SettingPriority { |
4 |
5 | @Test (priority= 0 ) |
6 | public void method1() { |
7 | } |
8 |
9 | @Test (priority= 1 ) |
10 | public void method2() { |
11 | } |
12 |
13 | @Test (priority= 2 ) |
14 | public void method3() { |
15 | } |
16 | } |
this sample testng project is very helpful to create my testng project
ReplyDeleteSelenium Training
Selenium Training in Chennai
Selenium Training in Chennai
It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
ReplyDeleteDevops Course Training in Chennai |Best Devops Training Institute in Chennai
Selenium Course Training in Chennai |Best Selenium Training Institute in Chennai
Java Course Training in Chennai | Best Java Training Institute in Chennai