Framework#1 - BaseTest to handle browsers & share driver object across project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back so in our selenium series today we are going to discuss about how to design a framework so today i'm going to show how we can design a framework from scratch so what does it what are the demerits of the basic sample selenium script which we were learning from past couple of sessions right so i will be showing you the classic approach and then we will go over from the basic to advanced level of framework designing so in this particular session we will be going to see how we can create a base test uh so that the driver can be initialized and after that we will see how we can share the same driver object into different other classes in your framework okay so that is what we are going to see so these two are the basic things that uh when you go into an interview as well so they will be asking you what is your framework structure folder structure so you might be saying that we have a base class where we are determining what browser it is to run and then we are distributing the driver object so let me just go to intellij i will be creating a new project and so for this when you install the intellij and click on and open that intellij you will be seeing something like this welcome screen in this you go and create on the new project so as you create a new project it will be asking you the java version at this moment i have java 11 so that's why it is showing without selecting anything let's click on next and after this it will be asking you in which folder you want to save your project so for this let me click on this browse and after this let me go back to this one and here let's just create one folder structure so for this let me just create here new folder at this moment let's just give probably i will give the date or let's say that september 21 so that it won't override with anything and i will click on open as you can see in that project folder it has been created now you can give you can change the name whatever it is there at this moment i'm keeping as it is for this or dot example you can just give anything at this moment i'm giving this one qab box and then let's keep it as orgy and finish now you can see the empty project is created now because it is a maven project what i'm going to do i will be adding couple of form.xml dependencies as plugins right now how do you what are the different uh different dependencies you need and the plugins as well so for a starter level i would say that you need selenium okay selenium java library and after that you need your test ng because we are going to see a test ng project and then you need the maven surefire plugin so these three particular dependencies we have seen in detail in our previous sessions if you're not sure then go back to my playlist and you can look for that so now where should we get the form.xml there are couple of ways you can go to the maven repository mvnrepository.com and you can search any of the dependencies java dependencies you will be getting the dependencies from there at this moment i will be showing you a quicker approach so i have created a block and in that i have specified all the form.xml that is required for a qa analyst the basic things for an instance let's say that you are working with selenium so then you can just copy paste this particular form.xml dependencies i have captured the selenium java test and then after that extent report the latest version i'm keep trying myself to update to the latest libraries if you are not sure you can just search this particular artifact id into the maven repository and you will get the latest one okay and then there are a couple of uh log back and then power for xml sorry for excel sheet reading and then mavel compiler project plugin if so this is for compiling only compiling with respect to a specific jdk okay and after that uh maven surefire plugin we have seen a lot of different ways that we can use the surefire plugin to get the data to execute the test cases on the command line if you are using api then you can just specify this dependency there is a thing that you need to note it down when you are using ipm with java client do not use the selenium because apium itself is having the selenium library inside so you can just replace this particular dependency with ipm okay so that's one of the note now at this moment what i'm going to do so always what i'm doing i'm not taking from here i'm not copy pasting everything when i create a new project what i do i always take it from here okay from this dependency folder so i will be just copy pasting dependencies element and ctrl c and here you can see i'll just select this one and i copy paste here now there are a couple of things you need the only thing is that for this properties you just copy paste these two things uh in our while we will be talking a lot uh in our what you call a lot more detail into the maven and palm.xml i will be letting you know what exactly i'm doing here but yeah at this moment you can just copy paste for a starter level and you can get started with them now okay form.xml is saved right sometimes as as you save it will download all the repositories or the maven dependencies or the jar files sometimes it won't automatically download the jar files so what you need to do you can go to the right hand side here you can see maven and then go back to this life cycle and here you click on this install okay so that any new libraries or dependencies or the plugins that you are adding into your form.xml it will be installed there so let's wait for some time so that it will download all the dependencies couple of the dependencies i have added as in newly or the latest versions which i didn't download before so it will take some time so let's wait for that okay so my dependencies are downloaded so it's throwing one error the reason is that i have kept here testnj xml into this your file plugin so as you can see here but the testnj jxml is not present so at this moment let me comment this one so that the error will be gone and after that we will be seeing once we will create the testchain.xml we will uncomment this particular section okay so everything is good so what i'm going to do after you have installed everything you have to go to the right click on this project then click on this maven option and here what you can do reload this project now what this does sometimes even though the jar files are loaded it is not binding to your current project so you can just do a reload so you can see it is indexing that after that you will be getting all the available what you call methods and features of these dependencies which we are using okay so hopefully everything is downloaded if you are not sure you can go to this external library and you can identify the packages that you are going to or you downloaded from by using your form.xml so everything is good now let me write a simple script which we know already so i will be right clicking creating a java class and here what i'm going to do i'll give a name as sample selenium okay and i have already created one simple script actually here so without wasting time let me copy paste that one so i'll go back here and i will be pasting and there are couple of imports are required so let me just do quickly okay so as you can see this is all set now let me tell you what is the problem with this approach this is absolutely working fine i mean it is doable actually with this or it is workable right now what is the problem with this the problem is that we have created one test uh test method right in this particular project now this is not a real time scenario because you might need more than one test case right in your project let's say that you have 50 different test cases that you're writing now as you can see the approach here what i've done that the driver initialization and sharing everything is happening very local to this particular test method now if i want to add another test method into a different class file then what i'm going to do i have to copy paste everything right which is not a good approach because when you are building a framework it has to be like right once and utilize as many as time so you need to have that approach always okay as much as possible you need to implement in that way so what i'm going to do is i was telling that i will be doing two different two things as a starting level for a framework designing so the first thing what i'm going to do this particular browser whatever i'm doing right i will be taking to a separate class so that it can be shared across different test methods okay so for this i will be using the source main so far we haven't used this main right so let me utilize that so here i will be creating a java probably i will create a package here and this package i will be taking it as a base and under this i will be creating a java class name it as base test okay so under this i will be writing the logic actually here so here what i'm going to do i will be taking all the browser logic inside this now so where exactly i will be taking this one i mean if i have multiple test case see when exactly this is executing this is executing just before my test case right so this is my actual test case simple login i'm taking here so before executing my test case i need to set up this one right like which browser it has to run and then what is the driver object and so that i'm initializing this driver object so that i can take it it means that before executing your test case right so in this base test i need to do something actually now what are the different options i have if i want to run a particular chunk of code before executing my method so can i use the test ng feature or test ng annotation that is before class so before class why i'm taking before class there might be instances where in one class you might have multiple test cases right let's say that you want to launch the browser once but you want to execute multiple test cases okay there are two benefits of doing before class if you want to have multiple test cases running on the same browser or same driver instance you can achieve that here another way let's say that no my organization is demanding that every test case before to that you want to execute or you want to launch the driver no need to worry what you can do which is my way of doing that each test class or the each java file should be having one test case only like one test annotation only so that the before test before class will be also applicable before method if you are doing then it is a problem if you are using multiple test cases so that's why one class one java file one test case that's what we will be taking it granted for throughout our framework designing so in the before class i will be mentioning like this public void and i will say setup now inside this i will be writing the logic right what is that logic so i will be copy pasting everything this thing and then after doing this one what i'm going to do so see at this moment i'm taking chrome as a default one at this moment i'm not taking it from the command line if you want to take it from command line you can just try the same way only we have seen even another approach where i can have a specific config.java file i can specify this one as well just to make it simplified this is i'm taking a browser variable i'm taking this as chrome at this moment so this is very similar to string browser equal to chrome okay why i'm making this system.getproperty in case you want to change this chrome to something firefox or safari or something what you can do you can send it from the command line that is why i'm taking this statement okay so after this what i'm going to do the next target is that i need to share this driver see again this driver object is very much local to this one right so same problem so if i want to execute this cetera okay think about i have a java and the new java and here i'm making it as test test login let's say that i want to do a test login here so what i'm going to do i will be copy pasting everything to this one but instead all these things i don't want here right because i don't want any driver initialization in this and know this one as well because we will take it up there no driver quit nothing here now the question comes what from where i should be getting this driver object in this test case i don't want to create it from here okay so the thing what i have to do i have to create another what you call java class which will be doing the page uh which will be sharing my what you call this particular driver object so for this what i'm going to do i will create a getter and setter so what i will do here page driver i will take it as name you can name it anything driver factory or any name you can give so here what i'm going to do i will create a getter sensor so this is very much java specific so let me just write it so public static and the return type has to be web driver right now here i will say getter okay get driver now when i call this method this has to return me the driver object so i will say return now what is that i'm going to return it so i will be returning so i will create a private static and then final so that i won't be changing this one and i will say that web driver okay equal to null or something i'm making but instead of that what i'm going to do i will be creating a thread local because the java is not a thread safe okay if you are just creating a drive an object it is not thread safe so the problem you it might arise when you do parallel testing okay so that time always make sure that you do a thread local here thread local and it need a type okay what is the type that type has to be web driver okay web driver type and web driver equal to just create a new object thread local that's it you just need to do this one so instead of creating directly a web driver object you need to do this one now what i can do i can return this one but it won't work in this way you can see it is throwing one error what it is telling you have to create a see required is web driver but we are providing this one so for this what i'm going to do i'll make it as a get so this is the way we can get a driver an object actually now if you see this is just a null write if i use get driver then it is becoming null because i have to assign something to this variable and what is that i am going to assign this driver object i want to assign to this driver okay how do i do that from one class how can i take a variable or the value and assign it to here simple thing i will be creating one what you call setter actually that one so setter and let's make it see for setting it up i don't need to return anything right so i'm making it as null uh void actually so here what is that i'm going to assign this has to be a driver actually so here i will say how i used the get method right the same thing there is a set method as well what is this you have to do this one now in this case this set method you can see i'm not using anywhere right so somewhere i need to set it so that by using this get driver i can share it across different files in my project so what i'm going to do after everything is done in this before class what i'm doing going to do here i will say page driver okay i will be calling that particular setter method actually so page drive dot set driver and here i will be sending this one as simple as that okay now in this case what it is everything is ready so my base test will now launch different browsers based on my entry whether it is a hard coded or you are getting it from your properties file or you are getting it from your command line whatever way you can just play with this line to your job is to get the browser name what browser it has to run actually now depending on this variable you can write the logic now see this is only written once now i can use this driver object as many as times i want now tomorrow let's say that somebody wants to run it in edge driver edge browser no need to worry you can just have a if condition or switch case you can write it depending on this browser variable you keep adding the functionality into this but this logic will not be changing the getters and setters okay so my set driver is done now the thing is that into this test login i i cannot get directly this driver object right so instead of this what i'm going to do here so i will say webdriver driver equal to i will say oops let's get this one import class now here what i'm going to do i will be doing page driver page driver see there in the base test i have used set driver this is this has to be only once throughout your before class you are doing just once only now get driver i will be using here as simple as that get driver that's it and this one also i will be trying to avoid it so don't worry about that so at this moment let me just uh web driver weight web driver weight and space wait that's it okay and i don't want again the dependency every time i will have to create an object and do that i will be creating a utility method so that i can use it across my test cases at this moment we are focusing into the driver object now you see the driver everything is set now let's say that you are creating another test class actually so now let's say that you are creating a new test login or another test case actually so what i'm going to do copy go to this java and then paste it now in this case let's say that you want to do invalid login okay now in this case what you're going to do everything is set for you now you see i'm not going to change the driver object i'm not going to change or write a again the same logic of which browser which browser i want to run it see this is the thing how many test cases are there the browser is fixed all the test cases has to run in chrome browser only so that's why your browser is fixed your driver is fixed everything is done now in case of let's say that you want to have a invalid login so what you can do you can just use some invalid password and you are done with that okay at least these two logics whatever we have written i don't want to repeat in all of these test cases okay so that is one good thing about that now as i told still there are some limits like this is repeating in all of these test cases the another thing is that i am use utilizing the same thing only see the username password login button everything remains same right but still i'm writing multiple times as many as the test cases i'm writing i'm every time i'm writing the same logic driver.find element this is also not a good approach so i have to write it once so username i will be declaring once and i can use the same instance every time i want so that is again another way of another good approach of framework designing but like i said we are only worrying about the browser instance and the driver initialization so let's focus into this only now at this moment what i'm going to do i will be creating a testng xml so that i can show you multiple test cases how it is working fine so at this moment let's do one thing uh let's let me get one test in the xml sample so that i don't need to write from scratch so i'll say testng.xml so let me go back to this search and then type testng xml so that i can get a sample test in gxml file so you can just go to this one so this is a good candidate i can copy this one and then paste it here and then i just want to have one test file and no class no methods let me just do a simple one here so i will be saying uh test login so you can see there is no package declared this is again not a good approach so let me create a package and i will say that test cases okay under this i will be copy pasting these two test cases and then this one as well okay so now it will become easy for me let me go to this one and i will say test cases dot and then this is my first one and the same thing let me copy page for the second test case as well so here i will say underscore invalid that's it now let me run this one you know so the problem here is that you can see we have created this particular test actually all these things but we didn't integrate this one so that's why the driver was not at all initializing i mean driver is initializing but this particular method is not executing and when this will execute we have to link this one with the with our test cases right so what i'm going to do here i will extend this one with the base test so that whatever the logic we have written that will be coming here so the same thing let me do extends and then i will say base test okay so that every time this particular test method will run or any test method base test will be executing that is the before class so now let's go back to this and one more thing that i'm going to do here let's do this one so i will say after class after class means after all of your test cases with respect to that particular class is executed all the test methods it will do a tier down okay so what is this tear down this is simple logic so page driver dot get driver so you get that driver and then you quit it so that we are nullifying our driver object okay this is one more thing now let's right click and run this and you can see this is my second test case and as expected this is the invalid login that's why it didn't log in properly and i'm not verifying actually anything i'm just trying to click on this login method that's it i'm doing here okay fine so as you can see there are two test cases i have executed i did the driver initialization and i have used the getters and setters here so that this particular driver object can be shared across different test classes there are a couple of things as i have discussed this is not the right way to do it we need to create a utility methods because all these lines are repeating actually you can see driver.get i'm repeating here drive this login functionality i'm repeating see in these two test cases everything is similar the only difference is this one only i'm sending different password now for just this uh invalid login password do i need to write entirely a separate test case and repeating all these things not really the problem is that if i want to change something let's say that tomorrow the application id got changed to username 1 or maybe it is changed to user email now at that moment whether it is one or two or twenty test cases every test case i need to go and change this one which is not a good approach right so all this drive i mean all these locator strategies we need me not to write into this test case so my way of doing is that always try to hide the implementation whether it is login whether it is clicking on a button or it is verifying certain things don't write the logic inside the test case the test case has to only contain your steps only what are the steps that you are performing so each step you maintain the methods and that method we will be keeping it inside the page classes so that's why we will be using your page object model and so in our upcoming sessions i will be showing you how to write utility methods how to write the page object model and everything so stay tuned and do practice this one if you have any queries uh do send me a message or you or you can comment into this particular session okay so thank you guys and do subscribe and stay tuned to this particular channel
Info
Channel: qavbox
Views: 211
Rating: undefined out of 5
Keywords:
Id: Y8gjXLKWMGA
Channel Id: undefined
Length: 26min 38sec (1598 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.