0% found this document useful (0 votes)
12 views41 pages

06 - 01 - TestNG Tutorial Annotations

Uploaded by

minhtandragon29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views41 pages

06 - 01 - TestNG Tutorial Annotations

Uploaded by

minhtandragon29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

TestNG Tutorial:

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?

 Generate the report in a proper format including a number of test


cases runs, the number of test cases passed, the number of test
cases failed, and the number of test cases skipped.
 Multiple test cases can be grouped more easily by converting them
into testng.xml file. In which you can make priorities which test
case should be executed first.
 The same test case can be executed multiple times without loops
just by using keyword called 'invocation count.‘
 Using testng, you can execute multiple test cases on multiple
browsers, i.e., cross browser testing.
 The testing framework can be easily integrated with tools like
Maven, Jenkins, etc.
 Annotations used in the testing are very easy to understand ex:
@BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest

5
RBVH/ENG1
Key features of TestNG

 Generate the report in a proper format including a number of test


cases runs, the number of test cases passed, the number of test
cases failed, and the number of test cases skipped.
 Multiple test cases can be grouped more easily by converting them
into testng.xml file. In which you can make priorities which test
case should be executed first.
 The same test case can be executed multiple times without loops
just by using keyword called 'invocation count.'
 Using testng, you can execute multiple test cases on multiple
browsers, i.e., cross browser testing.
 The testing framework can be easily integrated with tools like
Maven, Jenkins, etc.
 Annotations used in the testing are very easy to understand ex:
@BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest

6
RBVH/ENG1
Key features of TestNG

 WebDriver has no native mechanism for generating reports. TestNG


can generate the report in a readable format like the one shown
below.

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

 There are three major advantages of TestNG over JUnit:


 Annotations are easier to understand
 Test cases can be grouped more easily
 Parallel testing is possible
 Annotations in TestNG are lines of code that can control
how the method below them will be executed. They are
always preceded by the @ symbol

9
RBVH/ENG1
Advantages of TestNG over JUnit

10
RBVH/ENG1

First test case using annotations


Please follow the link below to do exercise
Setting up a new TestNG Project_TestNG.docx

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

The Console window in Eclipse gives a text-based report of our test


case results while the TestNG Results window gives us a graphical
one.

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

Annotations used in TestNG


 In the previous section, you have been introduced to the @Test
annotation. Now, we shall be studying more advanced annotations
and their usages.
 Multiple Test Cases
 We can use multiple @Test annotations in a single TestNG file. By
default, methods annotated by @Test are executed alphabetically

17
RBVH/ENG1

Annotations used in TestNG


 Run this code and on the generated index.html page, click
"Chronological view."

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

@BeforeTest and @AfterTest

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

Summary of TestNG Annotations


 @BeforeSuite: The annotated method will be run before all tests
in this suite have run.
 @AfterSuite: The annotated method will be run after all tests in
this suite have run.
 @BeforeTest: The annotated method will be run before any test
method belonging to the classes inside the tag is run.
 @AfterTest: The annotated method will be run after all the test
methods belonging to the classes inside the tag have run.
 @BeforeGroups: The list of groups that this configuration method
will run before. This method is guaranteed to run shortly before the
first test method that belongs to any of these groups is invoked.

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

Summary of TestNG Annotations


 @AfterGroups: The list of groups that this configuration method
will run after. This method is guaranteed to run shortly after the
last test method that belongs to any of these groups is invoked.
 @BeforeClass: The annotated method will be run before the first
test method in the current class is invoked.
 @AfterClass: The annotated method will be run after all the test
methods in the current class have been run.
 @BeforeMethod: The annotated method will be run before each
test method.
 @AfterMethod: The annotated method will be run after each test
method.
 @Test: The annotated method is a part of a test case

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

Summary of TestNG Annotations


 @AfterGroups: The list of groups that this configuration method
will run after. This method is guaranteed to run shortly after the
last test method that belongs to any of these groups is invoked.
 @BeforeClass: The annotated method will be run before the first
test method in the current class is invoked.
 @AfterClass: The annotated method will be run after all the test
methods in the current class have been run.
 @BeforeMethod: The annotated method will be run before each
test method.
 @AfterMethod: The annotated method will be run after each test
method.
 @Test: The annotated method is a part of a test case

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

Based on the above output, the execution procedure is as follows −


•First of all, beforeSuite() method is executed only once.
•Lastly, the afterSuite() method executes only once.
•Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest()
methods are executed only once.
•beforeMethod() method executes for each test case but before executing the
test case.
•afterMethod() method executes for each test case but after executing the test
case.
•In between beforeMethod() and afterMethod(), each test case executes.
38
RBVH/ENG1

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.

You might also like