Selenium 4 Grid Tutorial with examples - Parallel execution in Remote Machines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome guys in this section let us understand selenium grid okay so first of all what is this grid about so selenium grid is one of the smart proxy server that makes it easy to run your test in parallel on multiple missions okay so in the last few sections we already saw how to run tests parallely using test ng right so from your test nj xml if you set parallel attribute then test will start running in parallely that's not new to us but this is something new running these tests in multiple missions so there is one disadvantage earlier when we run three tests in parallely all the three tests triggered in the same machine right that is one way i agree but what if you have 15 test cases and if you trigger all them parallelly and what if all start on the same machine okay your machine will run out of memory right so when 15 chrome browsers open at a time to execute so then obviously your system will eat up resources and tests will start failing because of slowness in your system so instead those 15 test cases how about distributing them into multiple machines like running three per mission how does that sound okay so that way three permission your system can still handle it your server can happily run all the three without any issues parallely but you see all the five machines are executed at a time so that 15 test cases the execution time is same when you run everything in parallel on one single mission but when you run in single machine you may run into lot of issues but here we are distributing them into different missions so that way you will not see any flaky errors and your system performance also will be on the speed mode because you are running two three browsers so that's where selenium grid comes into picture it will help you to run your tests in multiple missions so that means you will control your execution from one single machine only okay so you will just say run this test but uber machine will distribute the execution so now when you run your testing gxml file let's say there are 10 tests inside that so from your eclipse you will just run that test xml file but once the execution started the selenium grid will take over those 10 test case requests and it will distribute to different machines to execute right that's where grid comes into picture so first let's understand how that is possible so how does grid works internally to distribute your execution from your local system to different machines and it should also report as the output right whether test or pass it should consolidate it again output from different machines and it should report in your eclipse console how does all that happen that's the magic of selenium grid looks exciting right let's try to understand the internal architecture of this great and then we'll set up the whole infrastructure and i'll show you live here on how to trigger that in multiple missions okay so this is what they have redesigned architecture in latest versions but overall your grid looks something like this on high level okay there is one centralized execution system you can call it as a hub but this hub consists of all these internal architecture components okay so when you go deep dive into this main execution system there are different components we have here which i will talk about that soon but overall from high level the hub could be in your local system like where you are running and writing your selenium code so that you can treat it as a hub you can start this execution system in your local mission and when you run your test your test will first reach to this hub and then this execution system hub will decide on how to distribute tests into different missions so here in great terminology uh different physical machines we call them as a nodes so this is one physical machine and this is one physical machine and this is one physical machine so in this architecture to your grid infrastructure there are three different physical machines are connected nothing but three nodes are connected okay so let's say you have firefox installed in one system chrome installed in one system opera in another system so when you run your test and if your test demands chrome then it will divert traffic to this physical mission okay at runtime this grid execution system can read your script properties and decide which can do the right job okay so if it demands firefox it will send here if it let's say uh in all the three physical machines there is chrome installed and your test also asking to run in chrome at that time it will load balance that means if you have 10 tests and if already 5 and 5 are assigned to this and if you ask another test to run then it will see that already 55 assigned it will divert other traffic to another physical mission so like this it will balance like if you have five test cases two two two two two one like that so everything this execution system can take care at runtime it will read your script properties to know what browser you want to run and what operating system you want to run now let's say one of your machine is mac here and one is windows and if your script demands to run on mac then it will divert your test to run in this mac mission it will not send request to these two missions so that's the knowledge your grit have okay so now let's deep dive in to see what is this green infrastructure consists of overall this whole constitutes a hub we call it as a hub and there are lot of components inside the hub so in earlier versions they didn't expose all these components so recently they started selenium grid have officially shown up in their documentation that what's going on internally in this hub infrastructure so that you know people can troubleshoot the things now first you can see that client that means our selenium raw code which you write in eclipse when you run that when you run your testing jxml file then you are making a request to execute your test cases so client is nothing but your local system uh selenium java which sends a request so here we are executing and when we are when you run your test first request will go to this router so there is one processor internally in this grid you can call it as a processor or component so the request first router will receive your request and then it will immediately ask distributor to create a new session and assign that it to the one physical machine so all your requests first router only will receive if it is first time request if router is receiving brand new request for first time it does not know which physical machine it has to root okay so basically router job here is to route to the physical machines but first time we don't know to which physical machine it want to root so what it does is it send it request to distributor so this distributor is a guy who will read your script properties and understand what is the right physical mission for you to execute okay so it will check what operating system you are expecting and it will see what physical node machines it have to re send a request or how what browsers you are executing or if it is a common uh windows and chrome then it will see like load balancing like what physical machines are already occupied with test and what else i have to assign so like that distributor do will some work and finally what distributor does is it will uh send a request to node to execute that particular your client code that means a physical machine so once it sends a session to one physical machine to execute at the same time what distributor does is it will send information to session map also it will tell you that hey i received one session this is the session id so whenever you create a new session every time your new session will have an id okay so when i say session here that means a session what you send from your client that means from your local eclipse when you run your testing g code or selenium code one session is created so that session information what does distributor do is it will give it to session map so this session map guy will store that session information and distributor will also give the information about uh on which physical mission i assigned this session okay now we are running some google homepage automation that is one session right that's one test case for that test case you ascend one physical machine for that that physical machine ip address also that information it will give to session map so duty of session map here is to simply create one map which consists of what test it is executing and in what physical machine it is executing that information it will store okay you the test which is executing is nothing but session id and in the physical machine which it is executing nothing but node uri that information it will store here now so client when it is running the test anytime it won't send entire uh code let's say in your java file there is 10 lines of selenium code right first line of selenium code will get a request and then all this distributor will see and assign physical mission next subsequently if you are getting the code from the same session from same test now what does rooter do is again it will not ask distributor to figure out which physical mission because distributor is already figured out earlier for that test where to execute which machine it execute okay that that information is already there in session map right so for subsequent request what does rooter do is it will simply talk to session map hey i got another session just check if it is already existing session oh yeah it's already existing test and what is the physical mission okay this is the ip address okay let me root it so it will directly route it to that particular physical machine okay only for first time it will take distributor help and distributor does the work of assigning and giving updating to session map about properties and values so thereafter a router will take care for every subsequent request from same test to assign back to the physical mission so that's how internally your grid is designed okay so now when you look at on high level you know that hub it constitutes basically all these components and then at the end you see the node these are the physical machines it will assign it to the nodes so now from the next lecture let's actually create one hub and let's create some nodes which are physical machines and we will register them and we will see how the real time execution happens by running in the different machines okay theoretically this is the concept people try to ask you in the interviews about explain grid architecture then you can nicely create this diagram and explain what each and every component does here but in practical story will be different it's very easy that you can simply make this hub and server nodes up and you can connect them and you can just distribute your test which step by step we will see from the next lecture with the infrastructure setup instructions okay welcome thank you so in this lecture let's see how to set up selenium grid in our local system so there are different ways you can set up the selenium grid so if you go to their official documentation so let's see what they are saying about setting up the grid so several components compose a selenium grid right we know that okay so if you check out our architectural so there we have clearly discussed that these are the different components so all these components together constitutes a grid right so depending upon your needs you can start each one of them on its own or a few at same time so now when you want to set up this grid architecture in your local system to schedule your tests in different nodes there are multiple ways to do that one is you need to start your router service session map component distributor all this separately you can start them and then work on it okay that's what they are saying you can start each one of them on its own or there is another way also which we can do by saving time so all these you can start together also as a hub okay so now if you scroll down so they are saying that you can start together or same time by using a grid role so there are different grid roles here one is standalone role and one is hub node and one is a distributed role okay so let's try to understand what is difference between all these three now if you want to start your grid in a distributed mode that means each component needs to be started on its own now if you want to set up in your local system in this distributed then you have to start the session session queue and router all the components what you see here you have to start step by step separately by giving the related commands that's how you do in the distributed mode but there is another mode which is very friendly called hub and node okay so this mode is there from the previous selenium versions also it's very suitable for small and middle sized grids so most of the companies rely on this hub and node kind of setup so basically what do you do here so here you simply start the hub okay there is one command which will trigger you to start the hub so once you start the hub automatically the components inside this hub will start okay you really need not give again separate commands to start each and every component here like router session map distributor everything will get triggered will get start automatically if you start hub which is a global one for this now in the documentation you can read that hub is the union of the following components so it is a set of router which will take your incoming request from your selenium test distributor which decides which uh node it has to execute session map which can store the session id and also the node physical machine address mapping related and new session queue we did not talk about this right so where does this come new session queue is something like when you initiate multiple sessions from your eclipse selenium so distributor can solve only one session at a time so all the remaining sessions which are waiting for distributor to check they will go in the queue so this comes between distributor and router there will be another component so that component will actually store all the new sessions in a queue so that distributor one by one will retrieve the item from the session queue and it resolve to check to which physical mission it has to provide okay so if you are sending only one request there is a no scope of session queue here but if you send multiple requests multiple test files execution so there will be one session queue where your sessions first will sit there and wait for a distributor to pick it up so that also you have to separately start as a component and event bus so event bus is a virtual here so all this communication whatever you are seeing in the arrows this is all they are communicating internally right so they are making communication through even bus channel here so to enable this communication there is another component that also you have to start so all this separately you have to start if you choose to uh set up selenium grid in your system on distributed mode but if you want to choose hub and node way to set up then simply start the hub with one single command here and that automatically sets up all these components for you which will be super cool so in this course i am going to show setting up the grid using this hub endnote which will be very simple and which is mostly used across than distributed mode okay now let's get started so first let's treat this local system where i am showing you all this is my hub so here where i'll start the hub and if i start hub in this my local system all these components also will get started in the same system okay but before you set up all these hub and nodes first you need to download one separate selenium jar which supports this selenium grid architecture so if you go to their official documentation here so go to downloads or you can just search selenium download in google which will take you to this page so you see that for grid there is one separate jar file you have which you have to download or you can go to maven and ask for selenium server dependencies it's up to you so i'll let's create one small project and we'll deploy this jars and we'll get started so first download the selenium server jar in your local system this will just come as a one single jar that's it okay so once you download this jar so make sure you create one folder called grid or any other and store that selenium jar inside it so i'm reason why i'm telling you this because so later when you run your web driver test or firefox test you know that there should be one chrome driver file and gecko driver to run firefox and edge driver file to run an internet explorer so we have different drivers right so these drivers should be in the same path where your selenium server is downloaded okay so that's why what i suggest you so start by downloading the selenium server and again download all the browser drivers now if you want to execute in chrome then you know that you need chrome driver to download that right to execute in chrome go ahead and simply say chrome driver download and get it based upon your local chrome browser version right and similarly get gecko driver also so all of them just place in one folder now i already did let me show you i think i created one folder called grid yeah here you go let me pull up that for you so yeah so i created a folder and here i downloaded the selenium server grid jar and then chrome driver gecko driver so by now you are already familiar with these two because this helps us to execute in chrome and firefox driver so once you have that in one folder now go ahead and your command prompt or terminal so if it's in windows we say it as a terminal sorry windows is command prompt and mac is terminal so go to the folder where you have all these files okay so right now i am actually in users so here let me go to grid okay so once you are on the path where your files are located so right now i came to the grid directory so from here you have to first start your hub you know if i say startup that means we are going to start all these components right now to start the hub i have given some step-by-step instructions here so we downloaded all these drivers located in the same path now to start the hub you need to give java hyphen jar tell what is your jar name space hub that's it this will start you the hub which eventually start all these components okay so let me show you so you can have that word document handy which you can download from this lecture java hyphen jar so here you have to provide your jar name so that means this is your jar right so you can copy that complete name exactly how it is and you need to paste it here so basically we are asking to invoke that jar file simple so when you give this command that jar file will be invoked but we want to invoke that jar file as a hub so that hub will start have processor will get start in your system so for this jar file we are sending hub as an argument so this argument will be sent to this jar file and internally the processing will be done and which eventually starts the hub and the related components that's it so after you give this just press and enter and you can see that that some logs generating but overall you can see that socket is ready event bus ready and your selenium hub is started okay cool so that means now 50 job is already done guys so this is set up in your system now if you want to check the status of your hub there is one way where you can check so if you go to the local host colon 4444 right if you give this then it will give the status of your running hub okay so if you are wondering how would i know that my grid hub started at this port number simply read your log here itself it's telling you that we started selenium hub at this port number 4444 okay so as it is in your local system instead of giving ip you can simply pass this localhost 444 port number and here you will see the details so now they are saying that you have a grid set up by registering the hub which is great but you don't have any registered nodes so that means um you have a hub ready but you have to attach physical machines to this hub right so when distributor is ready to assign the tests to your different physical node machines it has to know what are those nodes right so that means you have to create nodes and register to your hub then hub is aware that okay these are the currently these many nodes i have when tests are received then i will distribute to these nodes right without registering your node to this hub your hub will never know what nodes it has to delegate that's common common sense right so now our goal is to start a node and register to your hub and then hub will start listening to that node and whenever you receive any request then it will assign it to that node physical machine and you know what hub and node can be your local system also so in the same system you started hub and node also you can start in the same system so that means when hub receives request then it will assign to the node which is again your machine only so test will start getting executed in your machine again right so like that hub and node can be in the same machine also hub can act also also node to execute test but ideally in real time nodes are nothing but some different physical machines you will take for running your automation test okay so in this course i will tell you both ways first i will tell you how to start node in this local system only so that way hub and node both will be in my system next lecture i will take another physical machine and i will start there and i will register that physical machine node to this hub and we can see how tests are executed in another fiscal machine so we'll see different combinations don't worry now to start the node again go back to that word document so there are two ways starting in your machine starting in different mission so now to start in your same machine again from that folder where you have your jars you can again call that same jar file just like how we called earlier for hub but this time send argument as a node so that means it will know that it has to invoke as a node and you need to given flag called detect drivers so if you remember we have a driver sitting in your folder so we are asking to detect these because now node is nothing but a physical machine so this physical machine is ready to run your test when you ask to run test in chrome it will search for where is chrome browser chrome driver right so when you have it in the same folder and you ask to detect so later when this node receives a request of running in chrome browser it will use this existing chrome driver okay when it receives a firefox request then it will use the existing echo so that knowledge it will have only when you say detect drivers in the path with true okay so hub is already started here don't disturb this okay you won't have again terminal to give another command so keep in that way and you open another tab here this is in mac but in windows just open another command prompt window and there you start giving the another commands it's simple guys so no matter the mac or windows process is all same but just a different terminologies there you will see a screen with the black in windows you can still make it white also i'm just telling you there's no difference operating system independent that is the beauty of selenium right no matter you run in linux windows or mac code will be same steps will be same no difference okay so now let's start java hyphen jar and this time again give the name of the jar so in my case it is 4.1.2 dot jar so i want to now execute this as a node and i also want to detect drivers to set to true so that it will know what drivers it have in the folder okay so give this command so basically here we are starting the node in the same hub mission okay so you can read the description here it's saying that it started node successfully node is added so if you want to check whether node is successfully added now go back to your status i told you right here where you can check the status of grid and here you go so you see that one node is added and it is saying that it have ability of running 10 chrome browsers 10 firefox and one safari as it is the mac machine you are seeing a safari if it's in windows you might not see safari here right so your selenium grid detected this node have a capability of running in different different browsers and it can start up to 10 tests at a time in this node right so that means it can start 10 chrome 10 firefox and one in safari that's the capacity of your node right and your node is listening at this port 5555 okay so that information you will have it here you can see that started selenium node at this port 55555 your hub is listening at 4444 and now when you come back to your hub locks here you will see that a new log is added saying added node health check every 120 seconds so every two minutes it will make a ping to the node and see if it is still active to delegate right so once you added node here come back to your hub terminal and you will see that status updated that one node is added so that means now if you go back to your diagram so one node we attached but technically that node and have everything in same mission that doesn't matter for these people okay so as long as you set up grid if you assign node if it's a same machine or a different machine it will just assign the request and your test will execute okay so good now in the next lecture i will switch to the different machine and there let's go ahead and set up the node and we will register that node to the hub which is present in this local system it will be interesting guys just follow me okay all right guys so now i have opened another physical machine and i connected to that machine through teamviewer okay so i'm right now our actual hub is in this machine one where we have already started one node and one hub so now i am going to start another node in different machine okay you can see that we have connected to that machine through teamviewer so you have to repeat some things in the node machine before you start the node so if you go back to word document let me show you see in the node machine also you have to download again selenium server and download all the browser drivers and place in the same path where your selenium server is located so you have to repeat this step in all the machines where those machines are acting as node because those machines are ultimately responsible to execute your test cases right so from hub you are delegating the access now to run these test cases in these are different machines so in that machine selenium code should be there right without having selenium code how that a machine can know how to execute in that machine chrome browser should be there chrome driver should be there okay now most of the students make a mistake that they select a machine to execute but they don't install chrome browser in it and now they want execute test in chrome browser so if you don't have browser at all in that machine how does test execute there now in your local system if you don't have chrome will it execute similarly there also right so all the setup what required for automation you should first prepare in the machine what you selected to run your selenium test now so here the only setup what we need is download that selenium server jar and download the driver files and make sure you put it any folder it's up to you what folder you call so similarly what i did here i went to the grid folder um let me show you so i'll close this right so just like how i have created in the hub machine similarly in my node machine also i created one folder like this and here for now i installed selenium server jar and just a chrome driver assuming that firefox will not run in this mission so if that is the case if your test receives the request of firefox then your grid won't allocate that firefox related request to this machine okay to show you that i did that intentionally did not have firefox and later i will show a test running in firefox but that won't execute in this node machine okay right these two i installed great so in grid folder i created one folder and once you have that now go to the same folder path in your command prompt here and then you have to just give the same node command what you gave earlier in the hub machine to start same thing we'll do here also java hyphen jar and you have to give the name of your jar which is selenium hyphen server see this jar name will be different based upon what you download at that particular moment now 4.1.2 is version in your case it could be 4.9.5 anything just exactly get the same name and now here you are starting the node right so then you have to give node and tell detect drivers true so that way it will know that where chrome driver is present but this is just not enough so we gave the same command earlier in our local hub there it worked because here hub and node are in the same machine right so here node started at five five five five and if it want any help from hub it will simply make a call to four four four four port because in the same mission hub is started so it's easy but here this is entirely different machine if you want to ping a hub that's not present in this machine at all so if you make 444 call that will make a call to your node mission port where hub is not there so what do you want to do so basically you need to register your node to your hub mission okay how to do that so for that there are some internet transfer protocols you have to use just type this publish events so there are events exposed by your hub when it started initially for first time let's see that you can see that tcp triple four two and triple four three x pub x suv these are the transfer protocols okay so here where you will have all the information about your hub right so now you should make sure that your node is connecting to these binding socket protocols okay so these sockets are at triple four two port triple four three port now let's see how we can connect to those ports from your external node mission it's simple guys um you have to start like this tcp so what is tcp uh you can just take google very quick to understand so that way you know that i am not typing something rocket science transmission control protocol so that means it is used on top of ip to ensure reliable transmission so that means from our another physical machine we are making an information transfer from node machine to hub machine so to make that happen you have to start with tcp so that this protocol can transfer and ping the machine where you are looking for so you have to give the ip address of your hub first that's where you can reach there okay so you can get the ip address of your hub by if you go to hub you will see that this is where your hub is started right and this is the ip address 39 what you are seeing is 139 okay so first you have to reach this ip address once you reach this that means officially first you will be inside this uh hub machine from there you have to reach this ports x pub and accept which are sitting at triple four two and triple four three okay so first let's reach hub ip address dot yeah this is the ip address now here you have to reach triple four to port right so there are two events here publish events and subscribe events now if you check out your node again see x pub stands for publishing event so publishing event is triple 4 2 yes we are making a ping to that asking to register so similarly we have to make a ping to triple 4 3 also so that this is on the subscribing event so just like how you have written like this now hyphen hyphen subscribe because we are now making call to another event events and now give the ip address again to your machine but this time you will target what triple four three right so now if anybody ask you to explain this it's very simple guys i'm just explaining one more final time that there are two events two sockets basically publishing event and subscribing event so you have to ping to these sockets of your hub then node will be successfully registered right so to make that ping now the publishing event is listening at triple four two port but that triple four two port is not in your external node mission right this is hub is in different machine so that's why we are making a call tcp colon give ip address where your hub is sitting you can get the hub ip address here when your node started colon triple four two so you are making a call to that publishing event so that's why you have written like this hyphen hyphen publish events and next you have to make a call to subscribe events also suv stands for subscribe so that's why you have written hyphen hyphen subscribe events to triple fourth report and double slash missing here so tcp colon slash slash perfect so that's all you need to do and now when you hit an enter now it will create a node and also register this node to the hub which is sitting in another machine on this ip address and it will ping that triple four two port triple four three port okay let's see awesome looks like sockets are created you can see that it is reporting everything correctly here cool so now let's come back to our hub and when you refresh here you go there is another node added this node is now coming from different physical system right you can see that ip address is also different that way you can conclude that this is belongs to another position and if you observe by looking at these two nodes can you guess which is the node having coming from another physical machine this one how see there is no firefox here we don't have firefox driver installed right specifically in that folder as it could not find firefox driver it decided that this node do not have ability to run on firefox okay so because we didn't add gecko driver there so this is your local system where hub and node both are there this is another physical address which we are talking through the teamviewer we connected to that machine and we made that work cool so we have successfully set up two nodes this is enough for us to start testing so in the next lecture let's create one small project create test ng test and run them parallelly and see magic of distributing your tests to different machines okay welcome back thank you just to recap so in the last lecture we have successfully created a grid infrastructure setup so we created a hub which will automatically start session map distributor router right so these three are already started when you have created a hub right so similarly we created two nodes one node we started in the local system and one node we created in another physical machine so these are the two nodes and we have registered these two nodes to our hub right so we have given the enough commands to register with subscribe events and publish events so one of the node is installed in the same mission where hub is there so that's why there we don't need to give any publish and subscribe events we directly just start at the node okay so good so we have completed this setup the only thing remaining now is a client here is where you write your actual selenium code now and send your request and this will take care okay so now you might be wondered like where can i see a distributor where can i see router and session map see all that is taken care internally okay so once you started as a hub which comes with all these components so if you want to debug one by one you cannot see that from the outside when you have chosen hub node role right so there is another way called distributed system right if you choose that way of setting up your infrastructure then you can see the logs of all these router session map and distributor but that internal details are not really required to run this architecture okay so what all we need is to distribute our tests in different machines and we are good with that we have completed our setup so now let's solve the client process by going to eclipse creating one small project and create multiple tests and see how we can route our client information to router and once it reaches router router will take care of all distributing right so now i'll go back to eclipse i'll create one small java project here new project java java project so let's call it as a selenium grid and next finish don't create no so one java project is created now let's add selenium jars into this project so i'm not going with the maven project so i'll just simply add the jar into project java build path right so here under the class path add external jars and i already have selenium jar which i downloaded so i'm just going to use that jar that's it apply and close this one single jar will help us to run tests right so here i'll create one new class and let's call it as um google test awesome so here we are using testing g annotation so at the rate test and i'll start writing my code so public void let's call this method as home page check okay home page i'll write one simple selenium program that's it i'm not going in deep dive and writing any end-to-end automation here so our only goal is to see how it is running in different machines right so we can use small small validations um let's add test ng library all right the first step now generally let's say if you want to run in a chrome driver you will start like this right webdriver driver equals to new chrome driver but here problem is if you start like this your hub will never know that this test is getting executed why see where are you giving details to actually reach out your hub see now if you see the architecture your client has to first reach your hub so here it is first reaching router which is part of hub so when i say hub the whole three components constitutes a hub so we as a client our selenium we are reaching the hub asking to solve our request by running the program right so if you just start saying like this webdriver chrome driver you are sending the request to your chrome browser to run but you are not sending request to your hub so if whenever you are running using selenium grid there is a different way of initializing your driver so you have to use a class called new remote web driver this is how you have to initialize your driver so move your cursor and here you will have one package so name itself tells you that you can execute your test remotely in remote machines if you invoke and create your driver object as a remote webdriver class now so this remote webdriver class take two arguments one argument is the information where your hub is listening okay so that's where you have to send this request right so you have to make a request to your router router is sitting on which so that means where is your hub listening your hub is actually listening at this one starting selenium hub at this ip address column four four four four this is the port number where your hub is there right so now you have to give that url here but you cannot just pass url directly as a string you have to wrap your url in one class like this new the class name is url in this class argument you can pass the actual url where you want to send your request okay if you directly send it as a string like this this class still treats this url as a string but to tell this class that we are sending a url in java there is a class called url um and if you wrap your string in that url class then this recognizes is one of the url in your browser to execute right so move your cursor and import this url class this is coming from java.net package great so the first argument you have successfully provided in the remote web driver so what else is missing so your test know where to report asking to execute so whenever you run your test it automatically reaches the hub so that means a client make a request to router and then it will take it from there but when it reaches distributor for first time it still did not have the enough information like on what browser it has to execute or on what operating system it has to execute so earlier without grid we used to create chrome driver here so that your test knows that it has to invoke chrome browser but now this is a generic right and we still did not give the details like what operating system browser it has to run okay you might still write the code like invoke a google home page everything that's fine but it has to have some capabilities right like browser execution which version everything so for that when you are dealing with this remote webdriver class you have to create one more class called capabilities okay let me create that class so basically from this class object you're gonna define all the capabilities like what browser you want to execute everything and finally that object you are going to send as a second argument to your remote webdriver class so this way your test knows in what browser it has to run okay so it will simply give all the details to the router so as it is the first call router sends this information to distributor and distributor then check this caps object and see what is the required browser i want to know and it will distribute to the its related node okay so now here you can set your browser with caps dot set browser okay you can see that said browser name here where you can provide the information on what browser you want to execute you can also give the information like on what platform you can execute let's say if you want to execute this in the windows platform dot and when you type w you will get all the options so if you want to run windows 10 8 or anywhere okay so you set all your capabilities and then once it reaches the router this router guy as is the first session it will simply send to distributor to figure out distributor will check what operating system they are asking if it is windows it will immediately check how many nodes i have is there any node which have window platform okay if if let's say one is windows one is mac so that test what you are asking to run on windows that will go to windows node if you specifically mention mac and one of it node is mac then it will delegate access to the node which is having mac operating system so like that this capability object is key so this is where we decide to what local physical machine we want to assign or running the test okay so this is only for first time once the session is initiated now let's say in this case distributor uh checked this code and you are asking to run in chrome so it found a node which have a chrome browser so it delegated uh to run in that chrome node physical mission at the same time what distributor does is it creates entry in the session map so basically what is the session it got that session id and to which physical machine it allocated that ip address so next step when you make a driver.getgoogle.com as this is coming from the same session so this router guy first will check in the session map if there is already existing session yes it's there and then what is the ip address of the node it will simply find out the node and it will give the request directly to that node without disturbing the distributor okay that's the concept that's how internally it runs so to make this happen you need to send two important arguments here one is about where is your hub is sitting as a first argument and what are the capabilities you need to send to distributor to decide which physical address it has to execute i mean which node or physical mission these words can be interchanged here right so you can also set some advanced capabilities like this caps dot set capability and here you can mention what is your capability type let me show you so move your cursor and change to capability type and here when you say dot you will get lot of other options like in chrome if you want to accept ssl static certifications then accept it and set it to true so that if it find any ssl certifications by default it just accept it so like that you can set full capabilities on how you want your browser to behave you can go over it you can element scroll behavior profiling capability touch screen so lot of capability options if you want to set any proxy to your browser before invocation you can set that as well okay so you can also set your browser from here see you already did this this is one way they just directly provided one method to simply set the browser or you can come here and you can set here also this is another way to do that okay if you do in this way you have lot of other options also to set but in our case if your goal is to simply decide which platform and browser which is most of times um then you can use one this simple step and you can ignore this i'm just giving you from your knowledge purpose later if you want to have additional capabilities use this syntax just press dot and set the capabilities and finally after you set all these capabilities send this caps object here and that will take care all right so i'll write one simple step where we go to google.com home page and let's say i want to enter in that edit box by default google home page edit box will have one locator called name if you want you can check there will be one attribute with name and that attribute is q that's there from ages so we will simply enter rahul shetty and that's it and we will grab the title of the page and print it in the output okay so this is a simple step i want to do right so it will go to google home page get the title enter the some information in the edit box and then we will close the browser right so this is one test so similarly i'll create another test here copy and in the same source folder i'll paste it and here i will ask to go to rahul city academy website so rsca test so in this case what i will do i'll set browser to firefox so i'm asking to run this test on firefox right so don't worry how it handles once it reaches router whenever your rsa test session id reaches the router that will send it to distributor and distributor will open this capability object if it found firefox there then it will check which node have firefox and it will assign to it right so in our case so one of the machine do not have firefox okay see this is our another physical machine which we connected through teamviewer right this do not have firefox so that means the test rsa test will never run in this another node what you have installed remotely we can see that output later and here um i will just hit my website and my website do not have any name attribute cue so let's just get the title and close it that's it so i created two tests here so here maybe instead of using set browser name like this i can show something like this so i'm showing you different ways in first test i directly use that set browser name and here i'm using capability type dot and this is another way of writing syntax okay so one test on chrome one test on firefox two simple tests we have written and now let's convert this project uh to test ng so that you can run them parallelly at a time so right click test ng convert to test ng and here so these two are two different classes right so parallel mode yes we want to run classes so that means two classes google test and rsa test we want to trigger them parallelly so i selected that and when i click on next and finish it will create one xml file for me by default so that's the magic uh if you simply right click test ng and convert to test engine right go to this xml file go to source tab and this is your file so two classes are there okay remove this dot uh because we don't have any package here so this is a default package so these are the two classes and we are asking them to run parallely cool so in the next lecture let's trigger this testng xml file and see what's happening and how well that's playing the role now let's see what happens when i run this xml file so this xml file will trigger two classes rsa test and google test right so this test will be invoked and at the same time this test will be invoked but we are asking them to run parallelly so that means two sessions will start parallely but both these sessions will report to this ip address on this ip address your hub is ready right this is listening so those two requests will reach this router as these are the initial requests so router cannot get that information from session map to send it to the nodes to execute so it will give it to the distributor now distributor will open this capability object and it will see that okay one of the session is requesting to run on chrome so it will just see what chrome i have in the two different node physical missions and it will also see the another request this request is asked in firefox okay so now distributor will decide that okay this node machine do not have firefox so it will assign chrome related test to this external node machine and the firefox to this node okay this node is our local system only so that means execution will start in your this local system two nodes what we installed what are those one is your local system only we started where hub is there and one is externally through teamviewer we started right i am expecting that this chrome test will be picked by the external physical machine because uh that we don't have firefox and rsa test will run in my local mission that is how it should happen okay so i'll open the teamviewer we are ready here just to see if it is opening or not um there is my eclipse here it is so right click run test ng suite save and launch so where is the team viewer let's see here you go in teamviewer firefox sorry chrome started and you can see it's already done in your local system firefox started awesome i hope you saw that magic let's run again i'm curious to run two passes no failures and you can see that browser name google and rahul city academy titles are printed as expected firefox invoked in your local system chrome invoked in your team viewer other physical machine let's see run as testing just wait yeah teamviewer chrome and back in your local system firefox is running you can see that awesome so that's how you can distribute your test cool right and do remember that it is not necessary that your client which is your eclipse where you have installed and triggering this test this is not necessary that it should be in your hub mission okay because of infrastructure limitations in the recording i am showing you that a hub and the client where we are running our test in the same machine but at real time all the hub process can be in one machine your eclipse java code can be in another machine and your nodes can be again another physical machines okay see no matter from which machine you are running your test ultimately you are asking to reach this hub ip address right so it doesn't matter if the hub and your actual client is in same mission or different mission you are however making and sending that your test request to your hub with this address right so make sure this address of the hub is correct so if this is correct then this eclipse code you can write from any other mission so that's how you do in real time so what does people do in big projects is like you know they create multiple nodes they'll bring at least five to six physical machines in every physical machine they will go and set up the node just like how we did now through teamviewer yeah so here we configured the node right just like that you just need to ask your management give me four or five node missions because i want to run my selenium test parallelly in multiple machines so that way we will get faster feedback okay if you run them sequence in one machine it will take two three hours to get result but if you distribute them running in five to ten missions just imagine the results will be out in 10 minutes so that's how you need to talk to your management get at least five different uh servers five different physical servers login into them and set up node in all those machines just like how i did here and then set up hub in one system maybe you can do in your local system also and just trigger that's it just create all the desired capabilities and route them to your main hub and bring all tests parallely in your test ng and trigger it from here that's it so all the tests will run parallely in all different physical missions and it will finally report to your local machine output like this so that's the beauty of selenium grid okay so i didn't focus much on coding here guys so no matter you write two lines of code or ten lines of code overall this test will execute there based upon how much code you have okay hope this help and you can download the document and the repository from this next lecture okay thank you
Info
Channel: Rahul Shetty Academy
Views: 25,839
Rating: undefined out of 5
Keywords:
Id: P5CLXIC7x5c
Channel Id: undefined
Length: 65min 17sec (3917 seconds)
Published: Mon May 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.