Speaker A
So in today's session, we'll see how we can do a dependency test, which is one of the most important features from TestNG. Then we'll see the grouping concept. First, we'll see dependency methods. So what does this exactly mean? Dependency methods. So let me give you some example. Let's say I have a login test. So let's say I have multiple tests in my single class. Let's say I have to open the application, then I have to do login, and then I have to do search functionality. Then I have to do advanced search functionality, and after that, I should log out from the application. Let's say I have these steps in my test case, and I'll create multiple test methods for each step. Let's say I have three, four, five steps. So I'll create five different test methods. So when you execute these test methods, by default, TestNG will execute every test method, right? Suppose when you specify the test annotation for every test method, TestNG will execute all the methods. By default, it will not ignore any method because we specify test annotation for every test method. So by default, TestNG will execute all the test methods. So now the problem is, for example, let's say open app is one test, login is another test. For example, let's say open app. So this particular test method is failed by some reason. Maybe the URL is not properly opening or your application is not properly working. Let us assume this open app test method which is failed. So then what happens? Because TestNG will execute every test method by default, right? So even though the open app test got failed, TestNG will still try to execute the next method. And then what will happen? Definitely, login test will fail, right? Because if you really write your WebDriver code inside these test methods, if open app itself got failed, definitely the login also will fail. And if the login got failed, again TestNG will try to execute another method, and that will also fail. And again TestNG will try to execute another method. That also should fail. And finally, TestNG executes the logout method, and that also will fail. If I look at this particular scenario, because of this open app test got failed and the rest of them were executed by TestNG, and after that, they will definitely fail, right? But my requirement is, suppose my open app test or test method got failed, I don't want to execute the rest of the methods. Why should I execute the rest of the methods if it got failed? Definitely, I will know other test methods will definitely fail, and it is unnecessary effort to run those test methods, right? So by default, what TestNG will do, TestNG will execute every test method, and definitely they will fail. But I don't want to execute them. First of all, if my open app got failed, I just want to skip login, and then I will skip search method, advanced search, and logout, and all the rest of the methods. Whatever methods are dependent on this particular test method, I will just skip them. Unnecessarily, I don't want to execute. It is a waste of time and effort. But TestNG by default executes all the test methods, but I don't want that to happen. So how can we organize these types of tests, and what is the dependency method means? Suppose login is dependent on open app, and search and advanced search are dependent on login, and logout is dependent on login because once you successfully login, then only you can do logout. And once you successfully open the application, then only you can do login. So sometimes you can find dependency methods. So here my requirement is whenever a method got failed because of some reason, I just want to skip all dependent methods. Okay, how can we achieve it through TestNG? So we'll see that practical example. So let's go to Eclipse and create a new package for today, day 44. So inside this, I'm creating a new class. I'll name it as DependencyMethods, and all methods should be test methods with the @Test annotation. Other annotation methods are not possible. Sorry, we should not have main method. So this is a simple class I created. Now I'll create a few test methods. Let's say void openApp, and this is a test method. Also, I will give one priority equal to 1. And then I'll create another method login, and this is also another test method, and priority equal to 2. And then void search. This I will create as another test method, priority equal to 3. And then I'll create another test method void advancedSearch, and this is also another test method, say priority 4. And finally, I'll create one more method void logout from application. This is also test annotation, and priority is 5. Okay, now if you really write your WebDriver code, right? So in openApp and login, if openApp got failed, definitely login will fail. If the login got failed, definitely rest of them will fail. That we already know. Okay, but now I'm not writing any WebDriver code. I'm just writing a simple assertion. Okay, so internally I'll make some methods pass, some methods will fail. So let me just make login as, I can say assert.assertTrue of false. Then what will happen? This is a hard assertion, right? So what you have done in this assert.assertTrue of false means what? I'm expecting true here, but I'm passing false. So then will it pass or fail the test method? Will it pass or fail? Definitely it will fail. So I'll put this in this openApp, and in the login I'll say true. So that means this particular assertion will make my test method pass. Even search also I will pass true here, and even advanced search also I will pass true, even logout also I'll pass true. So according to my assertion, except this openApp, rest of all the test methods should pass because my assertion is written like this. So openApp method should fail because I am passing false. Okay, now just observe the default behavior of TestNG. I'm just executing this run as TestNG test. Okay, now you can just look at this. As per expectation, five methods got executed, four of them passed, one got failed. So this is our expectation. Okay, fine. I go back to the result also. This is console result. What happens? It is not populated. Okay, we'll see that. So five test methods got executed, four got passed, first one got failed. If I look at this, let me run once again. Okay, so now if I look at this, openApp got failed, and rest of all test methods got passed. Fine. So if I look at this as per our expectation, if you really write your WebDriver code, okay, suppose here I have written some piece of code for launching the application, and then here I have written some piece of code for login application. So then what will happen? openApp definitely will fail. Obviously, the login also should fail. Okay, but what test is doing is even this method got failed, the another method which is dependent, right? Login is depending on openApp, but still TestNG will execute this method. After that, it will fail. Right? You can see all the test methods got passed or not. Even though the dependency method got failed, rest of them are passed. But as of now, I have not written any WebDriver code, so by default everything got passed. But actually what will happen is even though the dependency method is failed, TestNG will try to execute rest of the methods, and after execution it will make fail. Okay, so after execution, openApp got failed, TestNG will execute the login also, but after execution it will fail. So definitely every method should fail. But what I'm saying is if any dependent method got failed, I don't want to run the rest of the test methods. I just want to skip them because that will save a lot of time. So instead of running and failing the test before itself, I will identify whether the dependency method is passed or failed. If the dependency method got passed, then I will go and execute this. If my dependency method got failed, I don't want to execute this. I just want to skip the test method. So this is our requirement. So how can we...