TestNG Annotations Flashcards
What is TestNG?
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.
What are the annotations used in TestNG?
There are three sections of annotation in TestNG:
- Precondition annotations: These are the TestNG annotations that are executed before the test.
@BeforeSuite, @BeforeClass, @BeforeTest, @BeforeMethod are the precondition annotations.
- 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
- 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
What is the sequence of execution of the annotations in TestNG?
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@Aftertest
@AfterSuite
What are the advantages of TestNG?
- It is an open-source framework, hence it is easy to configure.
- Using TestNG we can systematically create the test cases.
- It gives lots of annotations which in turn makes the test case creation easy.
- Using TestNG, priorities of the tests and the sequence of execution can be defined.
- Grouping is possible using TestNG.
- It generates HTML reports (Selenium Webdriver cannot generate the test reports alone, it helps SW to achieve this).
- Data parameterization is possible using TestNG.
- In addition to all the functionalities of JUnit, TestNG has its functionalities, which in turn makes it more powerful.
How to set priorities in TestNG?
===========
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 will you define grouping in TestNG?
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”)
What is a dependency on TestNG?
```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()
{
}
~~~
What is InvocationCount in TestNG?
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()
{
}
~~~
What is timeOut in TestNG?
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 to handle exceptions in TestNG?
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.
What are the common TestNG assertions?
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:
- 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.
- 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.
- 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.
- Assert.assertTrue(<condition(t/f)>)
- It accepts a boolean value.
- The assertion passes if the condition is True, else an assertion error is displayed.
- Assert.assertFalse(<condition(t/f)>
- It accepts a boolean value.
- The assertion passes if the condition is False, else an assertion error is displayed.
- 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.
- It accepts a boolean value.
- 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 to disable a test in TestNG?
To disable a test in TestNG, we have to use the “enabled” attribute as follows:
@Test(enabled=”false”)
How to pass parameter in the test case through the testng.xml file?
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>
What is the need to create a testng.xml file?
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.
What are the advantages of TestNG?
- Firstly, TestNG is capable of producing reports automatically with all the necessary information such as failed tests, passed tests, test execution times, etc.
- Secondly, TestNG makes use of annotations such as @BeforeMethod, @Test, etc., which are easily understandable as their naming is after their working.
- 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.
- 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.
- 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.
- In addition to the above, TestNG allows parallel testing, which increases efficiency and improves the overall running time of test methods.
- With the TestNG framework, you can easily integrate with other tools such as Maven, Jenkins, etc.
- 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.
- 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.
- Moreover, TestNG allows the test methods to depend on each other. Its also called Test Dependency in TestNG.
- Lastly, TestNG provides a bunch of assertion methods for testing more efficiently.