TestNG Annotations Flashcards

1
Q

What is TestNG?

A

TestNG is the framework created for executing unit tests in java program by the developers.

TestNG is also used by the software testers to efficiently run the automated test scripts created in Selenium Webdriver. Its full form is the “Testing New Generation” framework.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the annotations used in TestNG?

A

There are three sections of annotation in TestNG:

  1. Precondition annotations: These are the TestNG annotations that are executed before the test.

@BeforeSuite, @BeforeClass, @BeforeTest, @BeforeMethod are the precondition annotations.

  1. Test annotation: This is the annotation which is only mentioned before the test case (Before the method written to execute the test case)

@Test is the test annotation

  1. Postcondition annotation: These are the annotations that are executed after the test case. (After the method is written to execute the test case)@AfterSuite, @AfterClass, @AfterTest, @AfterMethod are the postcondition annotations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the sequence of execution of the annotations in TestNG?

A

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@Aftertest
@AfterSuite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the advantages of TestNG?

A
  1. It is an open-source framework, hence it is easy to configure.
  2. Using TestNG we can systematically create the test cases.
  3. It gives lots of annotations which in turn makes the test case creation easy.
  4. Using TestNG, priorities of the tests and the sequence of execution can be defined.
  5. Grouping is possible using TestNG.
  6. It generates HTML reports (Selenium Webdriver cannot generate the test reports alone, it helps SW to achieve this).
  7. Data parameterization is possible using TestNG.
  8. In addition to all the functionalities of JUnit, TestNG has its functionalities, which in turn makes it more powerful.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to set priorities in TestNG?

A

===========

There are always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution.

If we want to run the tests in the sequence we want, then we need to set the priority along with the @Test annotation.

This can be done as follows:
@Test (priority=1), @Test (priority=2)

Consider the following Example:
~~~
@Test (priority=2)
public void getText()
{
driver.findElement(By.id(“id”)).getText();
}
@Test(priority=1)
public void clickelement()
{
driver.findElement(By.id(“id”)).click();
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How will you define grouping in TestNG?

A

Groups are the collection of multiple test case methods combined into one single unit. By grouping, we can operate directly onto the group, which will reflect on all the test case methods under it. Moreover, in TestNG, we can also create a group of groups as a bigger unit of test methods.

define the Groups in TestNG by passing the “groups” parameter to the Test annotation with the value being the group name
@Test(groups=”title”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a dependency on TestNG?

A
```There are some methods on which many methods are dependent on.

***For Example***, If we want to test any application, and if the login page of the application is not working then we won’t be able to test the rest of the scenarios.
 
So, LoginTest is the method on which many tests are dependent on.
 
 ==============
**Hence, we will write as follows:**

@Test(dependsOnMethods=”LoginTest”)
Public void homePageLaunched()
{
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is InvocationCount in TestNG?

A

If we want to execute a test case “n” number of times, then we can use the invocationCount attribute as shown in the below example.

====================
Example:
~~~
@Test(invocationCount=8)
Public void print()
{
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is timeOut in TestNG?

A

If any method in the script takes a long time to execute, then we can terminate that method using “timeout” in TestNG.

@Test(timeout = 5000)

In this case, the method will get terminated in 5000 ms (5 seconds) and the test case is marked as “Failed”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to handle exceptions in TestNG?

A

If there are some methods from which we expect some exceptions, then we can mention the exception in @Test annotation so that the test case does not fail.

==========
Example: If a method is expected to have “numberFormatException” exception, then the test case will fail because of this exception if no try-catch block is specified.

But we can do it in TestNG by using “expectedException” attribute as follows.

@Test(expectedException=numberFormatException.class)

Then the test case will run without failing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the common TestNG assertions?

A

An asset is a piece of code that helps us verify if the expected result and the actual result are equal or not. In TestNG, we leverage the inbuilt “Assert” class and a lot of its method to determine whether the test case passed or failed. Additionally, in TestNG, a test case acts as a “pass” if none of the assert methods throws an exception during the execution. The syntax for TestNG assert is:

  1. Assert.assetEquals(String actual, String expected)
    • It accepts two strings.
    • If both the strings are equal, the test case executes successfully otherwise the test case fails.
  2. Assert.assertEquals(String actual, String expected, String message)
    • It accepts two strings.
    • If both the strings are equal, the test case executes successfully otherwise the test case fails.
    • The message is printed if the test case fails.
  3. Assert.assertEquals(boolean actual, boolean expected)
    • It accepts two boolean values.
    • If both the boolean values are equal, the test case executes successfully otherwise the test case fails.
  4. Assert.assertTrue(<condition(t/f)>)
    • It accepts a boolean value.
    • The assertion passes if the condition is True, else an assertion error is displayed.
  5. Assert.assertFalse(<condition(t/f)>
    • It accepts a boolean value.
    • The assertion passes if the condition is False, else an assertion error is displayed.
  6. Assert.assertTrue(<condition(t/f)>,message)
    • It accepts a boolean value.
      *The assertion passes if the condition is True, else an assertion error is displayed with the mentioned message.
  7. Assert.assertFalse(<condition(t/f)>,message)
    • It accepts a boolean value.
    • The assertion passes if the condition is False, else an assertion error is displayed with the mentioned message.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to disable a test in TestNG?

A

To disable a test in TestNG, we have to use the “enabled” attribute as follows:

@Test(enabled=”false”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to pass parameter in the test case through the testng.xml file?

A

If we have a class in which a login method is defined, then we can pass the login parameters to this login method from the testing.xml file

We will have to use the “@parameters” annotation as follows:

@Parameters({"username","password"})
@Test
public void loginapp()
{
driverget(“appname”);
driver.findElement(By.id(“login”)).sendkeys(username);
driver.findElement(By.id(“password”)).sendkeys(password);
}

==========
Now, go to the testng.xml file and enter the parameters there as follows:
~~~
<Suite name = “suitename”>
<test name =”testname”>
<parameter name =”user_name” value=”user1”/>
<parameter password =”password” value =”pass1”/>

<Classes>
<class name =”passingparameters”/>
<classes></classes>
<test></test>
<Suite></Suite>
~~~
</Classes>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the need to create a testng.xml file?

A

When we test a project using Selenium Webdriver, it has a lot of classes on it. We cannot choose these classes one by one and put them for automation. Hence we need to create a suite so that all the classes run in a single test suite.

We can achieve this by creating a testing.xml file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the advantages of TestNG?

A
  1. Firstly, TestNG is capable of producing reports automatically with all the necessary information such as failed tests, passed tests, test execution times, etc.
  2. Secondly, TestNG makes use of annotations such as @BeforeMethod, @Test, etc., which are easily understandable as their naming is after their working.
  3. Thirdly, TestNG provides a grouping of methods by which we can group multiple methods as one unit. In other words, Grouping performs operations on all the tests in a group at once rather than individually.
  4. Fourthly, TestNG provides a test method parameterization, which means we can provide parameters in the TestNG and call the function repeatedly with different values. Moreover, parameterization helps in data-driven testing in TestNG.
  5. Fifthly, TestNG provides the prioritization of methods. In other words, by defining the priorities of the methods in TestNG, we can alter the default execution sequence of the test methods according to our wish.
  6. In addition to the above, TestNG allows parallel testing, which increases efficiency and improves the overall running time of test methods.
  7. With the TestNG framework, you can easily integrate with other tools such as Maven, Jenkins, etc.
  8. Moreover, TestNG provides a feature to run multiple test methods on various browsers to test for cross-browser compatibility issues on your website. It is cross-browser testing.
  9. Additionally, TestNG allows us to run the tests separately. So, if you run the tests and only one test failed, you can run this test independently in the next execution.
  10. Moreover, TestNG allows the test methods to depend on each other. Its also called Test Dependency in TestNG.
  11. Lastly, TestNG provides a bunch of assertion methods for testing more efficiently.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the types of Asserts in TestNG

A

To validate the results (pass/fail), we have to use the assertion.

There are two types of assert in TestNG:

  1. Hard Assert:

Hard Assert is the normal assert which is used to do validations in the TestNG class.

We have to use Assert class for hard assert as follows:

Assert.assertEquals(actual value, expected value);

If the hard assert fails, then none of the code gets executed after the assert statement.

  1. Soft Assert:

If we want to continue the test execution even after the assert statement fails, then we have to use soft assert.

To create a soft assert, we have to create an object of a “softAssert” class as follows:

softAssert sassert = new softAssert();
sassert.assertAll();

So now if the test case fails, the execution is not terminated when we use soft assert.

17
Q

How to throw a SKIP Exception in TestNG?

A

If we want to SKIP any Test using testing, then we have to use the SKIP exception in TestNG.

It is written as follows:

public void skipExc()
{
System.out.println("SKIP me");
throw new skipException(“Skipping skipExc”);
}
}
18
Q

What is the difference between a TestNG test and a TestNG test suite?

A

TestNG test suite refers to a collection of tests that we can run simultaneously with the help of the TestNG XML file. On the other side, a TestNG test is a single test case file, and when we say “we are running a TestNG test case”, we simply mean we are running a single test case file.

19
Q

What are the types of reports generated in TestNG by default?

A

TestNG generates two types of reports by default after the execution of all the test methods finishes. They are:

  1. Emailable Reports
    Emailable reports generate under the project folder and test-output subfolder. This report is available as “emailable-report.html” by default.
  2. Index Reports
    The index report generates under the project folder and test-output subfolder. Moreover, this report is available as “index.html” by default.
20
Q

How do you exclude a group from the test execution cycle?

A

Excluding a group in TestNG denotes that this particular group refrains from running during the execution, and TestNG will ignore it. Additionally, the name of the group that we want to exclude is defined in the XML file by the following syntax:

<groups>
  <run>
    <exclude name = "demo">
    </exclude>
   </run>
</groups>
21
Q

What is meant by dependency in TestNG?

A

Dependency in TestNG is a process of making one test dependent on the other test. By providing dependencies in the test methods, we assure that a test method B would only run if test method A runs (given B depends on A). Moreover, in TestNG, we can also have one test method dependent on multiple tests.

22
Q

How do you create dependencies in TestNG?

A

We can create the dependent tests in TestNG by providing the dependsonMethods parameter on the @Test annotation. The value of the attribute is the name of the method on which we want this method to depend. The usage of this method is as follows:

import org.testng.annotations.Test;
public class DependsOnTest {
  @Test (dependsOnMethods = { "OpenBrowser" })
  public void SignIn() {
	  System.out.println("User has signed in successfully");
  }

  @Test
  public void OpenBrowser() {
	  System.out.println("The browser is opened");
  }
}

===========
TestNG also allows us to create dependencies between groups through the TestNG XML file. Such dependencies denote the dependence of one group onto another. The following code demonstrates how to achieve the same goal:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="TestNG XML Dependency Suite" >
   <test name="ToolsQA" >
   	<groups>
   		<dependencies>
   			<group depends-on= "openbrowser" name= "login"></group>
   		</dependencies>
   		</groups>
       <classes> 
          <class name="GroupDependency" />
       </classes>
   </test>
 </suite>
23
Q

What is meant by parallel test execution in TestNG?

A

The parallel test execution means executing different test methods simultaneously, i.e., parallelly in TestNG. It is achieved by creating threads and assigning these threads to different test methods (which is done automatically and is an operating system’s job). Moreover, running the tests parallelly rather than sequentially is very efficient.

24
Q

On what levels can we apply parallel testing in TestNG?

A
  1. Methods: This will run the parallel tests on all @Test methods in TestNG.
  2. Tests: All the test cases present inside the <test> tag will run with this value.</test>
  3. Classes: All the test cases present inside the classes that exist in the XML will run in parallel.
  4. Instances: This value will run all the test cases parallelly inside the same instance.
25
Q

How is exception handling done in TestNG?

A

We carry out Exception handling in TestNG by defining the exception at the @Test annotation level. If we proceed in such a manner, the test case will not fail even after raising an exception.

Example:
@Test (expectedException = numberFormatException.class)

26
Q

Why is the reporter class used in TestNG?

A

The reporter class in TestNG logs the tester defined messages into the reports generated by TestNG. These logged messages then print into the reports, which we can share with the team.

27
Q

What is @Factory annotation in TestNG?

A

The need to run multiple test cases in a single test suffices by using the @Factory annotation. The name factory resembles the generation of test class object that is provided by the method under it. Moreover, it is similar to a factory producing a product. The following example shows a factory annotation in TestNG:

@Factory()
	public Object[] getTestClasses() {
		Object[] tests = new Object[2];
		tests[0] = new Test1();
		tests[1] = new Test2();
		return tests;
}
28
Q

What is the difference between @Factory and @Dataprovider annotations?

A

@Factory and **@Dataprovider **are two types of annotations available in TestNG, which look similar in their working but are different.

@Factory: The use of the factory annotation is when the tester needs to execute the test methods multiple times, which are present in the same class. Additionally, we achieve this by creating different instances of the same class.

@Dataprovider: The dataprovider annotation enables the tester to run a test method multiple times using a different set of data provided by the dataprovider.

29
Q

What are listeners in TestNG?

A

Listeners in TestNG are the piece of code that listens to certain events and execute the code associated with that event. As a result, with TestNG listeners, we can change the default behavior of TestNG. Moreover, in TestNG, the tester gets the benefit of a lot of listeners who have different functionalities.

30
Q

How are listeners declared in TestNG?

A

The listener code in TestNG exists in a separate file than the TestNG test case file. Subsequently, this file contains the listener code and the type of listener to implement is done by “implementing” the listener class in the following way:

public class ListenersTestNG implements ITestListener {
	public void onStart(ITestContext context) {	
		System.out.println("onStart method started");
	}
}

To apprise the TestNG test case file about the listener, we declare the @Listener annotation and mentioning the listener class name in the following manner:

@Listeners(ListenersTestNG.class)
public class TestNG {
	WebDriver driver = new FirefoxDriver();
	@Test  //Success Test
	public void CloseBrowser() {
		driver.close();
} } ~~~

~~~

31
Q

What do we need to generate a customized report in TestNG?

A

This is amongst many other TestNG interview questions that are asked. A customized report in TestNG generates with the help of TestNG listeners. Using the interface ITestListener in TestNG, we can control the events such as method start, method pass, fail, etc., and according to these events, a tester can log appropriate messages.