06 - 01 - TestNG Tutorial Annotations
06 - 01 - TestNG Tutorial Annotations
Annotations,
Framework, Examples
in Selenium
May-2019
RBVH/ENG1
Content
What is TestNG?
Why Use TestNG with Selenium?
Advantages of TestNG over JUnit
First test case using annotations
Creating a New TestNG Test File
Coding Our First Test Case
Running the Test
Checking reports created by TestNG
Annotations used in TestNG
Multiple Test Cases
Parameters
Multiple Parameters
Summary of TestNG Annotations
2
What is TestNG?
TestNG is an automation testing framework in which NG stands for
"Next Generation". TestNG is inspired from JUnit which uses the
annotations (@).
Using TestNG you can generate a proper report, and you can easily
come to know how many test cases are passed, failed and skipped.
You can execute failed test case separately.
The TestNG provides an option, i.e., testng-failed.xml file in test-
output folder. If you want to run only failed test cases means you
run this XML file. It will execute only failed test cases.
3
RBVH/ENG1
Why Use TestNG with Selenium?
Default Selenium tests do not generate a proper format for the test
results. Using TestNG we can generate test results.
Most Selenium users use this more than Junit because of its
advantages. There are so many features of TestNG, but we will only
focus on the most important ones that we can use in Selenium
4
RBVH/ENG1
Why Use TestNG with Selenium?
5
RBVH/ENG1
Key features of TestNG
6
RBVH/ENG1
Key features of TestNG
7
RBVH/ENG1
Key features of TestNG
TestNG simplifies the way the tests are coded. There is no more
need for a static main method in our tests. The sequence of actions
is regulated by easy-to-understand annotations that do not require
methods to be static.
8
RBVH/ENG1
Advantages of TestNG over JUnit
9
RBVH/ENG1
Advantages of TestNG over JUnit
10
RBVH/ENG1
11
Running the Test
To run the test, simply run the file in Eclipse as you normally
do. Eclipse will provide two outputs – one in the Console
window and the other on the TestNG Results window.
12
Checking reports created by TestNG
13
Generating HTML Reports
TestNG has the ability to generate reports in HTML format.
Step 1: After running our FirstTestNGFile that we created in the
previous section, right-click the project name (FirstTestNGProject) in
the Project Explorer window then click on the "Refresh" option.
14
Generating HTML Reports
Step 2: Notice that a "test-output" folder was created. Expand it and
look for an index.html file. This HTML file is a report of the results of
the most recent test run.
15
Generating HTML Reports
Step 3: Double-click on that index.html file to open it within
Eclipse's built-in web browser. You can refresh this page any time
after you rerun your test by simply pressing F5 just like in ordinary
web browsers.
16
RBVH/ENG1
17
RBVH/ENG1
18
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Parameters
If you want the methods to be executed in a different order, use
the parameter "priority". Parameters are keywords that
modify the annotation's function.
Parameters require you to assign a value to them. You do.this by placing
a "=" next to them, and then followed by the value.
Parameters are enclosed in a pair of parentheses which are placed right
after the annotation like the code snippet shown below.
19
RBVH/ENG1
Parameters
TestNG will execute the @Test annotation with the lowest priority
value up to the largest. There is no need for your priority values to
be consecutive
20
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Parameters
The TestNG HTML report will confirm that the methods were
executed based on the ascending value of priority.
21
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
Aside from "priority," @Test has another parameter called
"alwaysRun" which can only be set to either "true" or "false." To
use two or more parameters in a single annotation,
separate them with a comma such as the one shown below.
@Test(priority = 0, alwaysRun =
true)
22
RBVH/ENG1
Multiple Parameters
23
RBVH/ENG1
Multiple Parameters
Consider the code below.
24
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
Applying the logic presented by the table and the code above,
we can predict that the sequence by which methods will be
executed is:
1st - launchBrowser()
2nd - verifyHomepageTitle()
3rd - terminateBrowser()
The placement of the annotation blocks can be
interchanged without affecting the chronological order by
which they will be executed. For example, try to rearrange
the annotation blocks such that your code would look similar to
the one below.
25
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
Applying the logic presented by the table and the code above,
we can predict that the sequence by which methods will be
executed is:
1st - launchBrowser()
2nd - verifyHomepageTitle()
3rd - terminateBrowser()
The placement of the annotation blocks can be
interchanged without affecting the chronological order by
which they will be executed. For example, try to rearrange
the annotation blocks such that your code would look similar to
the one below.
26
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
27
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
Run the code above and notice that
28
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Multiple Parameters
@BeforeMethod and @AfterMethod
29
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Excercise
1.Go to the homepage and verify its title.
2.Click REGISTER and verify the title of its target page.
3.Go back to the homepage and verify if it still has the
correct title.
4.Click SUPPORT and verify the title of its target page.
5.Go back to the homepage and verify if it still has the
correct title.
30
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Code:
31
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Code:
32
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Result
After executing this test, your TestNG should
report the following sequence.
33
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
34
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
35
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
36
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Example
37
RBVH/ENG1
Output
Exercise
TEstNG_Anotation.docx
39
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Conclusion
TestNG is a testing framework that is capable of making Selenium
tests easier to understand and of generating reports that are easy
to understand.
The main advantages of TestNG over JUnit are the following.
Annotations are easier to use and understand.
Test cases can be grouped more easily.
TestNG allows us to create parallel tests.
The Console window in Eclipse generates a text-based result while
the TestNG window is more useful because it gives us a graphical
output of the test result plus other meaningful details such as:
Runtimes of each method.
The chronological order by which methods were executed
TestNG is capable of generating HTML-based reports.
Annotations can use parameters just like the usual Java methods.
40
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1
Reference
https://www.guru99.com/all-about-testng-and-selenium.html
41
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.