Session 44 – Selenium with Java | TestNG | Dependency M… — Transcript

Learn how to manage test dependencies and group tests in TestNG using Selenium with Java to optimize test execution and avoid unnecessary failures.

Key Takeaways

  • TestNG executes all test methods by default, ignoring dependencies unless specified.
  • Failing a critical test like openApp should skip dependent tests like login and search.
  • Using dependency methods in TestNG helps avoid unnecessary test execution and saves time.
  • Proper test organization and dependency management improve test reliability and efficiency.
  • Assertions can simulate pass/fail to demonstrate dependency behavior in tests.

Summary

  • Introduction to dependency methods in TestNG and their importance in test execution.
  • Explanation of a scenario with multiple test methods like openApp, login, search, advanced search, and logout.
  • Default TestNG behavior executes all test methods regardless of failures in dependent methods.
  • Problem highlighted: executing dependent tests after a failure wastes time and causes cascading failures.
  • Requirement to skip dependent tests if a prerequisite test method fails to save time and effort.
  • Demonstration of creating test methods with priorities and assertions to simulate pass/fail scenarios.
  • Observation that by default, TestNG executes all tests even if dependencies fail.
  • Explanation of how to implement dependency management in TestNG to skip dependent tests on failure.
  • Practical example setup in Eclipse with a class named DependencyMethods and multiple test methods.
  • Focus on improving test efficiency by skipping tests dependent on failed methods.

Full Transcript — Download SRT & Markdown

00:03
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...
00:27
Speaker A
say uh I have a login test so let's say I have multiple test in my single class let's say I have to open application then I have to do login and then I have to do search functionality then I
00:42
Speaker A
have to do Advan search functionality and after that I should log in log out from 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 are there so I'll create five different test methods so when
01:01
Speaker A
you execute these test methods by default test NG will execute every test method right suppose when you specify the test annotation for every test method test NG will execute all the methods by default it will not ignore any method because we specify test annotation for every test method
01:20
Speaker A
so by default test NG 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
01:41
Speaker A
properly working let us assume this open app the test method which is failed so then what happens because test will execute every test method by default right so even though the open app is got failed test NG will still try to execute next method and then what will happen definitely login
02:05
Speaker A
test will fail right because if you really write your web driver code inside these test methods if open app itself is got failed definitely the login also will fail and if the login is got failed again test NG will try to execute another method and that will also will fail and again test
02:25
Speaker A
NG will try to execute another method that also should fail and finally test Ng execute the logout method and that also will fail if I look at this particular uh scenario because of this open app test is got failed and rest of them will executed by the test NG and after that they will definitely
02:45
Speaker A
fail right but my requirement is suppose my open app test is or test method is got failed I don't want to execute the rest of the methods why I should execute the rest of the methods if it is got f failed definitely I will know other test method will definitely fail and it is unnecessary
03:07
Speaker A
effort to run those test methods right so but by default what test NG 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 is got failed I just want to skip login and then I will skip it search method
03:29
Speaker A
advance search and log 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 wasting of the time and effort but test NG by default execute all the test methods but I don't
03:47
Speaker A
want to happen that so how we can organize these type of test and what is the dependency method means what suppose login is dependent on open app and search and Advan search is dependent on login and log out is dependent on login because once you successfully login then only you can do log
04:10
Speaker A
out 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 is got failed because of some reason I just want to skip all dependent methods okay how we can achieve it through test
04:32
Speaker A
NG so we'll see that practical example so let's go to eclipse and create new package for today day 44 so inser this I'm creating a new class I'll name it as dependency methods and all methods should be test methods at the rate test methods other annotation methods are
05:04
Speaker A
not possible sorry we should not have main method so this is simple class I created now I'll create a few test methods let's say void open app and this is test method also I will give one priority equal to
05:23
Speaker A
1 and so then I'll create another method login and this is also another test method and priority equal to two 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 Advan sarch and and this is also another test
06:02
Speaker A
method say priority four and finally I'll create one more method void log out from application this is also test annotation and priority is five okay now if you really write your web driver code right so in open app and login so if open app is got failed definitely login will fail
06:30
Speaker A
if the login is got failed definitely rest of them will fail that we already know okay but now I'm not writing any web driver code I'm just writing uh a simple assertions okay so interally I'll make some method pass some methods will fail so let me just make a login as I can say asset dot asset to
06:50
Speaker A
true of when I say asset do asset to true of false then what will happen this is a hard assertion right so what you have done in this asset. asset true or false means what I'm expecting true here
07:05
Speaker A
but I'm passing false so then will it pass or fail the test method will it pass or fail definitely it will pass it will fail so I'll put this in this open up and in the login I'll say true so that
07:20
Speaker A
mean this particular assertion will make my test method pass even search also I will pass true here and even at Advan search also I will pass through even I log out also I'll pass through so according to my assertion except this open app rest of all the test method should pass because my assertion
07:45
Speaker A
is written like this so open app method should fail because I passing false okay now just observe the default behavior of test NG I'm just executing this run as test NG test okay now you can just look at this as per expectation five methods are got executed four of them are passed one is got
08:10
Speaker A
failed so this is our expectation okay fine I go back to the result also uh this is console result what happens it is not populated okay we'll see that so five test methods are got executed four got passed first one is got failed if I look at this let me run once
08:35
Speaker A
again okay so now if I look at this open app is got failed and rest of all test methods are got passed fine so if I look at this as per our expectation if you really write your web driver
08:55
Speaker A
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 open app is definitely will fail obviously the login also should fail okay but what test is doing is even
09:13
Speaker A
this method is got failed the another method which is dependent right login is depending on open up but still test NG will execute this method after that it will fail right you can see all the test method got passed or not even though the dependency method is got failed rest of them are
09:31
Speaker A
passed but as of now I have not written any web driver code so by default everything is got passed but actually what will happen is even though the dependency method is failed test ngng will try to execute rest of the methods and after execution it will make fail okay so after execution so open
09:53
Speaker A
app is got failed test NG 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 is got failed I don't want to run the rest of the test method I just want to skip them because that will
10:12
Speaker A
saves 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 is got passed then I will go and execute this if my dependency method is got failed I don't want to execute this I just want to
10:31
Speaker A
skip the test method so this is our requirement so how we can make it happen so for that what we need to do is we need to identify the dependency methods for example let's say login is dependent on openup so what is a dependent method for login is openup so now what we have to say is we need to
10:55
Speaker A
tell testng like execute this login method only if open app is got passed okay and if the open app is got failed then skip this login so how we can do that in the test method test annotation we have to
11:13
Speaker A
pass one parameter that is depends on methods this is a k sensitive keyword depends on O is a capital letter M is also capital letter so it depends on methods plural equal to do in the curly brace we
11:32
Speaker A
have to specify the name of the method let's say what is the name of the method is open up so that you can specifying the double codes so now what happens is I have specified what is the dependent
11:44
Speaker A
method of this and the dependent method is what open app so that I specified so once you specify then what will happen test NG will try to execute the open app and uh if it is passed then only
12:00
Speaker A
the login will execute okay and if the open app is got failed then login test will not be execute it will just skip so that's the reason we specify this parameter so depends on method equal to open up so if this method is got passed then only login will execute if this method is got failed or
12:23
Speaker A
skipped then login will not be executed it will just skip it okay now similarly post search and advanc search let us assume these two methods are dependent on login functionality or login test let us assume so these two search and advanced search are depending on login so login is a dependent
12:44
Speaker A
method for search and advanc search so then how we can specify that for the search in the test annotation we can add this parameter and what is the dependency method for this is login and for Advan resarch also the dependency method is what same login I can say login and what is
13:06
Speaker A
the dependency test for log out that for that also login is a dependency so now we specify that so now let us assume earlier open up is got failed and rest of them are executed and also passed right now observe carefully so I'm just running the test okay now we can just see the output
13:31
Speaker A
here go to the console window so five is got executed all tests are executed one is got failed let me save and execute just a moment run as test NG test okay so now we can look at this open app
13:54
Speaker A
is got failed and because of this failure rest of them are skipped you can see these are skipped yellow color cross mark you can see earlier every method is got executed and passed so executed by test NG by default every test method is got executed by
14:12
Speaker A
test NG but this time because of open up is got fail rest of the methods are just skipped they have not executed okay now suppose I'll say like this login is depending an open app okay now I'll make open app is true through so open up will pass so login is depending on what open up okay
14:37
Speaker A
login will execute open up will pass and login will execute but internally I'll make this login failed I'll make this login failed okay now what about the search method will it execute or will it skip will it execute or will it skip search method I'm talking about search okay so open
15:01
Speaker A
app will pass and what about login test login is depending on open app so open up is got passed so login will execute but after execution we intentionally make failure so login test will fail and search method advanced search method and log out method so these three are
15:23
Speaker A
dependent on login and but login is got failed so obviously what happened rest of the three methods will automatically skip so ultimately the first method will pass login method will fail and rest of the three methods Should Skip so that is our expected let me run and show
15:44
Speaker A
you yes let me run one again yes now we can see open app is got passed login is got failed and rest of three methods are got skipped so they have not executed because of login is got failed so like this we can control
16:09
Speaker A
the dependency method so what is an advantage of this feature we unnecessarily no need to run every test method if I don't specify this dependency methods so even though the depend method is got passed or failed in all the cases test NG will try to execute this method and ultimately it will
16:28
Speaker A
fail so unnecessarily I don't want to execute this method it is a wasting of time right if you have n number of tests it will be uh a lot of time will be wasted so I I just want to skip them okay but
16:43
Speaker A
sometimes suppose if there are multiple dependent methods let us say let me take this advanc search so if search is successful normal search is successful then only I can verify advanced search right now advanc search is dependent on what search functionality if the search is got
17:04
Speaker A
passed then only I will continue to execute Advan search so let us assume I have multiple dependent methods let's say Advan sear is depending on login and also it is dependent on search then how we can specify that very simple same one parameter depends on method equal to login
17:24
Speaker A
comma one more parameter we need to pass search that also you can pass so now I specified two dependent methods so when I want to execute this advanc search method these both the methods should pass both the methods should pass login should pass search should pass then only the advanced
17:45
Speaker A
search will execute and it will pass even one of the method among them login or search even one of the method is got skipped or failed in both the cases advanced search method will not be continued so these number of parameters you can specify any number of parameters but there is only one
18:04
Speaker A
par dependence method keyword but inside this curly braces you can specify multiple method names you can specify a number of methods okay depends upon the requirement so one method sometimes one method you can have dependent another method you can have dependent okay
18:22
Speaker A
now I specified Advanced searches depending on login and search functionality now just observe I'll make login is also passed but search method I will make failed okay now tell me open app I'll make it as passed and what about login login will pass or fail it will pass why
18:47
Speaker A
because it is depending on openup so openup is got passed and login also will execute and internally I'm making it passed fine and what about search search will also will execute because dependent method is got passed but internally I'm failing it but what about Advan search for advanc search
19:09
Speaker A
login is got passed login is got passed but what about search search is got failed so obviously the advance search will be skipped login is passed but a search is failed right that's the reason Advan search will be skipped and what about login it will pass so let's EX
19:29
Speaker A
execut and see so this is very very useful feature sometimes if you want to specify the dependency method okay now we can see open up is passed login passed search is failed because of search is failed Advan search is got skipped and log out is executed and passed so when you really
19:59
Speaker A
write your web driver code so then it will be very very useful feature but I have not written any web driver code I just intentionally failed or pass or test methods normally we implement the test methods by adding validation points right so according to the validation points it will decide
20:15
Speaker A
so this is the parameter which we have to use to specify dependency methods this is the parameter so here in the double quotations we have to specify method name and and if you want specify multiple you can say comma and you can say method name two like this you can put n number of methods
20:38
Speaker A
and this parameter depends on method attribute is available only for test annotations okay so everybody's understood about this feature depends on methods how we can specify dependent methods in the test and what is an advantage of this feature so what is the main advantage of this feature
20:59
Speaker A
we don't need to unnecessarily execute all the methods so before executing any test method we can cross check the dependent method is pass or not if it is a pass then execute if it is a failed then skip it right so very very useful feature from test a small concept okay so now let
21:20
Speaker A
us move on to the next topic which is very very important grouping given an automation frame works everywhere the grouping concept is very very very important so now we'll see what is grouping how we can achieve this in test NG how we can group our test cases and how we can execute them as per
21:41
Speaker A
the groups we'll see step by step so first of all let us see what is a grouping concept so normally uh in the real environment or realtime projects what will happen is you will have n number of test cases so test case in the sense I we already disc discussed in the test Eng how we can organize
22:00
Speaker A
them first we will create package inside the package there are multiple classes and we will create XML file in that we'll create all the tests and everything so what is a grouping means in the grouping we will categorize all the test methods into different categories suppose let's say I have
22:19
Speaker A
created class one and I have cre class two class three and so on in every class there are methods test method M1 M2 M3 so on in every test method let's say here M4 M5 m6 and in class three let's
22:39
Speaker A
say M7 M8 M9 and so on so in every class we have a different test methods okay now what is a grouping means I just want to categorize those methods not classes I'm talking about methods and I'm talking about especially test methods I can group them into multiple categories for example most of
23:05
Speaker A
the times we group them based on the type let say sanit group or all the regression test I can make them as a regression and suppose I want to execute both sanity and regression so then I can make one another group called let's say functional test functional group so sanity test
23:26
Speaker A
groups regression test group functional test let's say sanity test group in the sense what whatever the basic functionality test like login something like that sign up so those type of test methods comes under sanity so basic functionality test so if those test methods got failed we cannot proceed
23:47
Speaker A
further execution so such type of test methods we will identify and we can group them into sanity and uh regression means whatever test methods we want to execute reexecute in every build in every cycle of testing whenever the developer provided build to us we need to do some kind of regression
24:07
Speaker A
test means what the earlier test cases whatever is got failed after developer is fixed those defects or issues we will again cross check whether they are working properly or not so those type of test methods we will make part of regression functional means all the test sanity regression
24:27
Speaker A
every each and every test will be part of the function So based upon the type of test case or type of test method we will categorize them into multiple groups and this uh this uh task should happen before starting your automation at the time of writing your manual test cases at the time of
24:46
Speaker A
executing a test cases you need to update one flag so you'll have a test cases document right in the same test case you will update one flag whether it's sanity test or regression test or functional UI test or something like this okay so we need to do that work during the manual testing time itself
25:05
Speaker A
so once you written your automation test then you can categorize them each and every method as a sanity or regression or functional group this is a first task okay and after specifying this particular tag for every test method whichever tag you want you can specify and later on we
25:24
Speaker A
can specify which group of test methods I want to execute through XML only it is possible only through XML it is possible grouping concept is Possible only through XML file we have to run all the test only through XML file so in the XML file test change XML file we can specify whether you
25:42
Speaker A
want to execute only sanity methods from all the classes or do you want to execute only regression from all the classes or do you want to execute all the test method from all the classes that you can decide in XML file that is called grouping concept because in every round of execution you
26:02
Speaker A
don't want to execute all the test methods it's not necessary depends upon the requirement depends upon the cycles of testing you will choose a set of test cases then you will execute them okay so that is the concept of grouping so how we can achieve this in test Eng so we'll see practical
26:23
Speaker A
example let's go to Eclipse so for that I'm going to create few number of classes which contains certain number of test methods so let's create new class and my first class name is login test so in this class I will keep all the login related test methods login related test methods
26:47
Speaker A
so I'm writing something so vo login by email okay this is one test method also I'm giving priority equal to one and import this test and here I'm not passing or failing the test method I'm just writing the printest message so this is this is login by email similarly I will create few more
27:21
Speaker A
methods so this is another test method priority to login by email login by Facebook login by Facebook this is another method here I'm changing the text login by Facebook and then I'll create another method this is priority three and
27:49
Speaker A
login by let's say Twitter and this is also login by Twitter so I have created three different test methods login by email login by Facebook login by Twitter all three belongs to one functionality so login test I have created to three different test methods priority 1 2 three so let me
28:12
Speaker A
execute so all three test methods got executed and passed by default all three are got passed perfect similarly I will also create one more class and let's call it as a sign up T test login test and this is a sign up test and let's
28:34
Speaker A
take the Finish sign up test as a finish so inside the sign up test also I'll create few more test methods let's create few more test methods here the first method void sign up by email sign up by email
28:59
Speaker A
and this is a test annotation and priority equal to 1 Import this test annotation and simply I will write a print message this is sign up by email similarly let me write few more test methods so this is sign up by Facebook
29:36
Speaker A
and this is sign up by Twitter so in the login test we have created all the login test methods and sign up test I create all the sign up test methods signing up by Twitter okay now this is a
29:51
Speaker A
sign up test all the sign up test I've created in one test so now I'll create one more test one more class and here payment test payment tests so in this I will create few more test methods so I can say void payment by or payment in
30:21
Speaker A
rupees payment in rupees and this I can make test annotation and priority equal to one import and inside this I'm writing a simple print statement I can print payment in payment on rupees okay now I can create another similar
30:52
Speaker A
type of method one more test method priority to payment in dollar okay so now I have created three different classes this is our login test and this is our sign up test and this is our payment test so let me execute my login test all three methods
31:18
Speaker A
so login test is got successfully executed all three methods got executed now let me execute uh this one sign up test so sign up test all three got executed let me execute payment test individually I'm trying to execute so all payment tests are successfully executed perfectly fine so
31:38
Speaker A
now what is the first step we need to do is we need to identify what are all test methods are comes under which group okay a few test methods comes under sanity few of them are comes under regression few of them are comes under functional so we need to First identify them okay so so what
31:58
Speaker A
I will do is I will have a small requirement here so based on the requirement I will categorize okay so this is my requirement let's say in the login test I have created three test method login by email login by Facebook login by Twitter so let's consider these three methods comes under sanity
32:19
Speaker A
so I can make these three methods of Sanity group login by Facebook login by email login by Twitter comes under purely sanity group and after that in the sign up method sign up test in the sign up test three sign up test methods are there so I'll make all three part of regression so that's
32:40
Speaker A
another requirement so sign up by Facebook sign up by Twitter sign up by I make them as a regression and also some of the methods will comes under both sanity and regression for example payment test dot payment test class in the payment test we have have created two test methods and let us
33:00
Speaker A
assume these two test methods comes under both sanity and regression both sanity and regression and combinely we can give a name called functional okay but I have why I have given a specific name I'll show you at the at the time of execution so let us assume these two test methods will comes
33:21
Speaker A
under both sanity and regression so there is a possibility right there are some test method which comes under both and regression the possibility is always there so let us consider requirement like this so first three methods belongs to sanity all sign up methods belongs to regression and uh
33:40
Speaker A
payment test or belongs to both sanity as well as regress okay let us see this requirement according to this I want to create groups so before creating group we need to have more clarity on this which one we need to put a sanity which one is regression which one is comes under both
33:57
Speaker A
categories so once you have this requirement then we need to specify each and every method with the particular group so how we can specify that group so first let's go to login test so login test all test method should part of the sanity group so how we can specify that in the test annotation
34:21
Speaker A
itself we have one option called groups okay when have an attribute called groups in this in the clyra we have to specify the name of the group so for example let's say I'm giving sanity and this group name can be anything guys so here I'm taking just like a sanity regression something like this
34:43
Speaker A
but the group name can be anything like you can put a b c also or you can put group number 1 2 3 also you can give this name is user different so whichever name you want you can give okay so here
34:56
Speaker A
I have just given some meaningful name called sanity regression and functional so now in the login test I can add groups tag so groups equal to sanity same thing I will add this login by Facebook also and also I add same thing for login by Twitter also because all three test methods
35:15
Speaker A
I'll make part of the sanity group so I have added groups fine now go to another one signup test and all three sign up tests are belongs to regression so here I will add groups equal to regression groups equal to regression and same attribute I will add for rest of the test methods
35:39
Speaker A
here and also here so all three test methods are belongs to segre regression now payment test are belongs to both sanity and regression two groups so then what I can do is go to the payment test now how we can specify the two groups here I can specify like this groups equal to in the curly
36:03
Speaker A
base first parameter is sanity comma second parameter is a regression I can specify two groups like this and even payment in dollars also I will make both part of sanit regression okay so for now just follow like this why I have given one single name to both the groups I will tell
36:24
Speaker A
you later so now I have categories according to my requirement so all login test login test methods part of Sanity sign up test methods are part of regression payment test methods are part of both sanity and regression okay according to my requirement I have categorised all the test
36:45
Speaker A
method from three different classes now save them so after adding I'm saving everything okay done so now we have to execute these three classes using XML file because only through XML file grouping is possible okay now let's create an XML file which will run all three classes so I will create my own
37:11
Speaker A
XML file or you can create your own XML file if you want or you can automatically generate XML file so I will create my own XML file so take right click new file in the package I'm selecting here so where you want to create this XML file you can just create under day 44 XML
37:30
Speaker A
file name I'm giving is grouping XML so grouping is the name of the XML file I'm giving so click on finish now it is created an empty XML file so whenever you create a new XML file the first
37:44
Speaker A
thing is we need to add first few statements these two statements are mandatory so I'll add them now we need to write our own tags so now tell me what's the first tag so by this time I hope you familiar with XML file right so what's the first add suit so suit is having name
38:07
Speaker A
attribute so I can give a name I can say grouping concept or I can say grouping suit you can put any name and close this suit next inside the sort test tag so that is also having some name okay I can
38:27
Speaker A
call it is a grouping test it can give some name and closing the test tag and inside the test tag we have a classes tag opening tag and closing tag and this test should be next one right so this is
38:43
Speaker A
a classes tag now inside the classes tag we can specify all the classes now class name equal to so which one is a first class login test that is a part of day 44 package so we have to specify the
38:57
Speaker A
package P name also d44 Dot Login test and then close the class right so I can just close this class is it correct test classes so inside the classes we specify the class name equal to this one and closed and classes tag is
39:31
Speaker A
closed any synex the content element type classes must match class parameter is it correct or not we'll check it once again so test contains a classes classes contains a class name equal to this okay so what is wrong in this guys can you notice so this is a suit suit contains a test a test
40:09
Speaker A
contains a classes classes contains a class tag day 44. let me try to execute and see run as just iner suit yeah perfectly executing so this is an eclipse error actually let me close and open one again that will resolve the problem problem sometimes Let me
40:31
Speaker A
refresh open the grouping XML yeah see this is Eclipse is not refreshing it actually but perfectly fine all right so let's create another class I think another bracket we put here yeah this is a problem so let me put another line and uh login test and then we have sign up test
40:58
Speaker A
sign up test and then we have payment test so this is payment test okay so all three we mentioned and when I execute this XML file it will execute all the test method from all the classes by default
41:20
Speaker A
because we haven't specify anywhere execute only this particular group like that we haven't specified anywhere so when execute this XML file so by default it will execute all the test methods you can see from the login test three methods are executed and passed for sign up test all three
41:36
Speaker A
got executed for payment test two methods are got executed so this is the default execution so now we want to execute specific group of test methods according to my requirement okay so step one what we have done we created multiple test classes and in every test method we specified which group it
41:57
Speaker A
is belongs to and once it is done we created a new XML file so in the new XML file we specified all the classes now we need to filter it so what type of groups of test we have to execute that
42:11
Speaker A
we need to filter so how we can filter this so to filter this uh what we can do is we have to add uh a special tag so along with the suit and test classes so we need to add special groups
42:27
Speaker A
where we have to add before starting the classes so before starting this classes tag earlier I told you one important thing someone is asked so if you are able to create a automatically the XML file why we need to create a manually so this is the importance because when you create XML file
42:46
Speaker A
automatically only it will generate the basic tax but now I want to add a groups which group of test methods I want to execute from all these classes so how where we can specify here we can specify so before starting this classes tag after completion of the test tag here we need to add one
43:04
Speaker A
tag called groups inside the groups here is the opening tag and closing tag inside the groups we have to specify one more tag called run again this opening and closing tag inside this run we have one more tag called include include and name equal to here we have to specify
43:27
Speaker A
then group name and close this include tag so this is a additional stuff we need to write okay so inside this include name we have to specify which group of test we want to create so automated XML file you can edit okay the basic stuff you can create automatically then you can add additional
43:50
Speaker A
tags in the same XML file that is always possible okay rating is always possible even if you create automatically no problem but when you create automatic XML file by default you will get the basic tags like suit test classes class these tags you will get so Additionally you can add
44:06
Speaker A
these groups okay so now I have additionally added this particular tax groups groups contains a run tag and inside the Run include and this is the most important part so inside this include tag we have to specify which group of test methods we have to execute from all three classes so I want
44:31
Speaker A
to execute only sanity test methods sanity test methods from all the classes now I say sanity just observe I say only sanity so whenever I say sanity here according to our requirement what are the sanity test methods we have in these three classes how many methods will execute you can just tell
44:53
Speaker A
me the number so I have created multiple three classes and some of them are sanity some of them are regression some of them are comes under both sanity and regression right so in this grouping tag I put only sanity so when I say sanity how many test methods will execute from three classes
45:14
Speaker A
how many test methods will execute when it say sanity three or five which one is correct three or five five is the correct answer why because first three methods are belongs to sanity they will anyway execute and in the class three these two methods also belongs to sanity right so
45:35
Speaker A
these also belongs to sanity so it will capture all sanity test methods so finally totally five methods will execute from all three classes let me run and show you so run as test NG Su yes you can see total how many methods are executed five is got executed what are those five these
45:57
Speaker A
so all three login test and payment methods two payment methods so totally five are got executed and five are got passed so we can specify grouping like this so when I say sanity all sanity are got executed suppose I say regression only regression now tell me how many test methods
46:20
Speaker A
we execute as part of regression again fine because these three are belongs to regression and again payment methods also belongs to regression right so totally five will execute see when I run this test changes suit right so now we can see all five are got executed regression
46:44
Speaker A
okay now this is fine so when s say sanity all sanity methods are executing all santies are executing when I say regression all regressions are executing perfectly fine now my requirement is I want to execute only sanity but not regression which are not part of regression see I want to
47:05
Speaker A
execute only sanity methods which are not part of regression then how many methods will execute that let me know what are the methods will comes under that category only sanity but not regression only these three methods comes under right but if you look at these two these are also part
47:24
Speaker A
of regression I don't want to execute them so if you specifies the sanity here five methods are executing right what are those five methods first three and last two because last two also part of the S so they are also executing now my my requirement my filter is I want to execute purely
47:44
Speaker A
sanity methods but they not belongs to regression that means I'm filtering these two methods because these two methods are belongs to sanity but also belongs to regression so I just don't want to to consider them I want to get only sity only three methods then how we can specify that
48:05
Speaker A
include sanity and next dag we need to specify that is exclude exclude name equal to regression exclude tag we can specify okay then what happens it will execute all sanity test methods if suppose they are also belongs to regression they will exclude okay so this time we will execute only
48:33
Speaker A
three methods which are purely belongs to sanity now can see loging by email Facebook log by Twitter so regression is just excluded is just avoided but earlier if I don't specify this exclude then what happens it is captured all sanities from all the classes and this time it is
48:52
Speaker A
captured only sanity but not regression understood everyone so the first requirement is all sanity test we have executed second requirement is all regression we have executed third requirement is all sanity but not regression we have executed by putting including and excluding now my requirement
49:20
Speaker A
is all regression all regression but not San now tell me how we can write this all regression but not sanity all regression means what here we have to specify include but not sanity means we have to exclude sanity here so what are the test comes and how many comes are only regression but not part of
49:46
Speaker A
Sanity so these three will come into picture and these two even though they are belongs to regression they will not be executed because they are also belongs to sanity that we have excluded so these three methods will be executed so let's go and
50:02
Speaker A
execute so we include regression but exclude sanity so now we can see these three exactly belongs to regression okay perfectly fine now fifth requirement this is most important I want to execute the test methods which are belongs to both sanity and regression
50:28
Speaker A
okay execute all the methods all methods which are belongs to both sanity and regression how we can a this both sanity and regression it should only both sanity and regression means what what does it mean end operator I'm using end end means what the test method should be belongs
50:57
Speaker A
to both sity and regression how many methods will execute how many methods are there in this which are belongs to both sity and regression only two methods payment in rupes payment in dollars so these two methods are belongs to both sity and regression rest of them we should avoid okay now
51:15
Speaker A
can you tell me how we can write this requirement include and exclude tag we understood include means it will include everything exclude means it will avoid so now I want to execute methods which are Bel bels to both sanity and regression can anyone guess how we can specify both sanity and
51:36
Speaker A
regression suppose if I just comment this exclude for now suppose if I write like this I said what I said the group should include both regression and Sanity right if I rate both two include groups for example let's say here I'm saying sanity
51:57
Speaker A
will it achieve the requirement when I said two includes in the first include as say regression second include as a s will it achieve the our requirement I want to execute the test method which are belongs to both regression and Sanity I say both includes regression and Sanity just
52:16
Speaker A
observe what will happen it is executed all the methods right but we want only these two right not possible so then how we can achieve this by writing two includes we cannot achieve the requirement so then how we can achieve this so for that reason what we need to do is we need to
52:40
Speaker A
give a separate name for that so whenever a method is belongs to multiple groups for that particular category we will give a different name okay let us call them as a functional so functional means what sanity and regression together we can call it as a functional okay we'll give us different name then
53:01
Speaker A
what we need to do is go to the test methods so you can give this one sanity and everything but also give go to the payment test and here I will specify functional you can have sanity regression tag no problem and also additionally I will add one more tag called functional
53:27
Speaker A
okay I'll give another tag called functional so what is a function means it is a combination of two groups it is a combination of two groups so now if I go back and grouping XML instead of including regression and Sanity I just put only functional include only functional
53:47
Speaker A
I remove this part so now what happens I given this name to the com by combining two groups sanity and regression so now when I execute this it will satisfy our requirement so when I say run as a test CH suit now it is executed only two test methods which are belongs to both sanity
54:09
Speaker A
and regression so all methods which are belongs to both sanity regression means what whatever method we added sanity and regression those methods we also added another tag called functional so in the XML file when you say functional only these two test methods will be executed so like
54:29
Speaker A
that we can achieve this requirement all methods which are belongs to both sanity and regression both sanity and regression understood everyone so this is how we can execute group of test methods specific group of test methods if you want to execute you can configure like this and if you
54:53
Speaker A
do not specify these tag groups stag so let me com this so if I don't specify this group tag okay so this is a comment in XML file if you want to comment any line in XML file we need to
55:07
Speaker A
say less than colon iPhone iPhone and ending is this one okay let me just comment this so then what will happen if I don't specify this group tag but still all my test methods are added to the particular group right but still our XML file will execute all the test method
55:27
Speaker A
from all the classes because I'm not specifying the grouping here I'm not specifying anywhere execute this particular group of test methods so by default this will execute all the classes all the categories of test methods by default execute everything only when you specify
55:45
Speaker A
this grouping then only it will automatically filter so I can comment this this is exclam so when I say include functional or sanity or regression then only it will filter okay only through the tax we can control the grouping of execution just by adding the tax nothing will
56:12
Speaker A
happen just by adding tax nothing will happen so we need to specify and one more thing whatever name you specified in this groups the same name we have to refer in the XML file okay so for example in the test you put ABC instead of sanitary regression functional you say ABC so the
56:31
Speaker A
same ABC names we have to refer in this grouping also that name should exactly match okay so if you want exclude functional you can put that also in exclude tag no problem suppose I want to execute uh uh I I just want to exclude functional just say only exclude function and uh I don't want
56:56
Speaker A
to execute fun functional and include tag I can comment so now tell me what all test will execute I say exclude functional and in include section I'm not specifying anything now tell me this water all will execute water all not execute in include tag I have not specified anything whereas
57:16
Speaker A
in exclude tag I specified only functional in include tag I have not specified anything but in exclude tag as a functional okay let's see what will execute I'm just running as a testes see except that functional two test methods rest of them all executed by default even though we not
57:39
Speaker A
specified include okay if we don't specify include by default it will execute all the test methods but again we put additional filter called exclude execute all the test methods but excluding this functional so that's the reason you're not able to see the functional test methods again execution
57:57
Speaker A
here except this functional rest of all test methods will execute by by why because we have not specified anything in the include section okay so that's how we can control the grouping concept using this include and exclude tag very very important and include and exclude tag also will
58:20
Speaker A
be part of the Run tag and even run tag is also part of the groups tag tag and this entire part should comes before starting the classes tag okay so you just remember this and you'll understand so everybody's clear how we can achieve the grouping the first task is what we need to identify which
58:44
Speaker A
method is belongs to which category that's the first ground work we have to do once we have categorized then go to each and every test and each and every test method and then add particular group by using groups you can add single group or you can add multiple groups and once you added
59:03
Speaker A
the third step is create an XML file and specify what are all groups you want to execute okay and excluding and including tag we can control so this is a separate suit we will maintain grouping XML like that we can create a separate Source separate XML file and whenever you want to execute specific
59:25
Speaker A
group of test we will try to execute this XML file just by changing this options okay so this is all about grouping concept so which is very very important feature in test NG so we need to remember this all right so uh I'll stop today here because another topics are very huge data
59:53
Speaker A
uh like parallel testing parameterization passing parameters through XML file so there are few more concepts are there I will discuss them in the next class but before that just need to practice this and get familiar with this concept because I'll go a little bit slowly because test NG features
60:11
Speaker A
are very very important even in the framework also we are going to use all these Concept in one place first we will learn all testing features bits and pieces so once you understand all the features so then we will integrate all these features in one place that is called framework so all the
60:28
Speaker A
features will integrate in framework so then you will see exactly how we can use all these things in the framework okay so I'll stop here for today's session just practice this dependency methods and grouping test and let me ask you one thing so far in the atate test annotation
60:47
Speaker A
we have seen some attributes right so can let me know what are the attributes so far we covered in the test annotation so attribut is necess we can as a parameter so what are the things we have covered so far how many types of attribute we can specify in the test annotation so far
61:04
Speaker A
what we learn first we learn about priority yes priority you can specify then what is another attribute next one depends on methods next one groups groups right so these are the priority depends on method and groups so these are the three attributes we have learned so far in the
61:37
Speaker A
test annotation in the coming sessions I will also show you some more parameters so these are only three not only these three parameters we can also have some more attributes in the test annotation okay and these attributes are used only with the T annotation not for another type
61:53
Speaker A
of annotations okay remember this and there are many more there we will discuss them in the next sessions all right so I'll stop here for today's session and we will continue in the next session
Topics:SeleniumJavaTestNGDependency MethodsGrouping TestsTest AutomationSDETQA TestingTest ExecutionTestNG Annotations

Frequently Asked Questions

What happens if a dependent test method fails in TestNG by default?

By default, TestNG executes all test methods regardless of failures in dependent methods, which can lead to cascading failures and wasted test execution time.

How can TestNG be configured to skip dependent tests when a prerequisite test fails?

TestNG allows specifying dependencies between test methods using the 'dependsOnMethods' attribute in the @Test annotation, enabling it to skip dependent tests if the prerequisite test fails.

Why is managing test dependencies important in Selenium test automation?

Managing test dependencies prevents unnecessary execution of tests that are bound to fail due to earlier failures, saving time and improving the efficiency and reliability of the test suite.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →