How to Automate Chrome Extension In Selenium | Add Extension In Selenium Test During Execution

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
can you run my selenium automated test with some extension or can I run my automated test which is running on the web with some extension this is a very common question you will find on the internet and on the community side so answer is yes you can run your automated test with some extension for example you have certain plugins that you need during the execution now when you run your selenium automated test by default you will not be getting getting any extension you will not get any browser extension proxies cookies it's a fresh profile altogether but let's say now I want to run my automated test with some extension one of the great use case is let's say I want to start my selenium test with selectors Hub extension selector Hub is a great plugin tool you can say which will help you a lot and every qm must have this plugin so let's take this example so when you render automated tests and let's see our automation is stuck somewhere maybe it's failing because of some exception and now you want to inspect that element or you want to debug that particular script in that browser so one solution is you go back to your existing browser see where it is failing restore the point and then use the selector sub or you can start a selenium test with select a sub extension whenever it's failing immediately right click and start using selector sub in the new session which is open by selenium right so in this case I will show you how to get started how to work with one plugin how to work with two plugins how to work with multiple plugins and the process is very easy only thing is you need to download the CRX file now what is CRX file let's say so in order to understand this I'm going to use one plugin called get CRX plugin so basically this plugin will help you to download any CRX of that particular plugin so just like we have exe files we have jar files we have our files so similar way whenever you have any extension on Chrome the extension is dot CRX file so basically in order to run our test we will mention the CRX file first of all willing to download then we will mention it and then we will run our automated test so I already have this extension so in case if you don't have just search for get CRX or I will give you this link in the description just click here add to Chrome and you're good to go so right now in it is already available in my system so it says remove from chrome and this is what you will get so once you install this plugin basically if you have to download any extension just right click and you will get this option called get CRX of this extension so step number one you have to check this one step number two let's say I want to use selectors Hub again it's up to you which plugin you want to use but for example I'm using electric Hub which I use a lot whenever I'm running my automated test so let's say I'm searching for selectors hub right again it has pro version I will go with the community one and let's say this is the plugin that I want to use so right now it is already available in my uh you know Chrome so you can see a lot of extensions but when you run your automated test you will not get even a single extension so let's say I want to download this I will right click and you can see this option called get CRX of this extension so I will click on it and I will keep somewhere in the specific location then I will copy paste so let's say I will save it somewhere and this CRX file is downloaded can you see this now let's go back and let's write the test for this now I'm using a selenium 4.10.0 but the concept will remain same irrespective which version you're using so I will create a new class and I will say get selenium test with extension I'm using public static white main I'm using plain Java application but in case we're using K unit testing G please continue because ultimately I need to use something called Chrome options okay before we go ahead I have already covered uh two dedicated examples how to use Chrome options what are the different capabilities it has so I will give the give that video link in the description or you will find somewhere on the screen you can go ahead and check it out so I'm assuming uh starting from the scratch so let me create one object of chrome options so the moment you type Chrome and control space from the keyboard you will get this option called Chrome options class so the first example which you will see is what we need so you can use Chrome options to add any extension while running your test on Chrome browser so we'll quickly go ahead and create object so I will give the object name is opt but you can give any other name of your choice now once your object is ready you just type opt Dot so you can see the multiple methods that we can use we don't need all these methods the method that I need is ADD extensions so the moment you put dot operation type add e just type EXT so you will get two methods one is ADD extensions now this will give you two methods basically these methods are overloaded here we are using method overloading because we have the same method ADD extension ADD extension two times but if you see the signature is different the first method is asking you file where X basically you can pass multiple files so you can see file dot dot dot means variable length argument you can pass one file two piles multiple files second option is list of files so here basically you will be using collection so suppose you have three extensions all extension will be in a file then you can create a list and pass the lit I will show you both the options in this example so let's continue in the first one now it is expecting a file so we are going to use a file class in Java so either you can create object directly here and pass it or you can create separately and pass the reference here I will create directly on the fly so I will say new file control space and it will ask me give me the file path where this file located in our case the file is the CRX file that we have downloaded so right now it is available in some other location now I want to keep that file as part of my project and the reason is let's say today I'm running on my system tomorrow I will be running on ec2 instance or maybe I'm running on a different virtual machine right in that case the file all the files all the resources should be part of my project now again you have two options here either you can dump all the resources inside SRC trust resource or you can create a folder called resources here or in my case I will give this folder name is extensions so right now I will take one example but let's say if you have multiple extensions you can keep all the extensions in this folder so just allow me a minute quickly I will copy the extension from that location to this location okay so I have copied uh we have paused the recording because I had some other files as well now you can see the CRX file is here just you need to copy and paste here now I will do the renaming part okay so I will keep it straightforward selector sub.crx file now what is next I need to provide the file path again multiple options you can give the relative path absolute path absolute path I generally try to avoid because it will keep changing if I run my test on different systems so I will keep it relative path so relative path is basically either I can use dot dot means current working directory which will focus here after this I want to go to this folder so I will say forward slash extensions okay then I will provide the file path which is selectors hub.crx file so let me save this copy this now our Chrome options is ready only one thing you need to do is start your session web driver driver equal to new and now I will create object of chrome driver now again we have Constructor overloading concept here you can see default Constructor Constructor with chrome driver service Constructor with chrome options and this is what we have created today Chrome options and pass this opt object here and now we are good to go so now I can continue with whatever application and it will continue so let me show you let me open any random application you can continue with your own application so let me do one thing first let me remove this and let me show you what is the current behavior when we don't pass opt here if I right click run as Java application okay and yes it's starting so this is the fresh profile basically when I say fresh profile you don't have anything not even a single extension right let me close this or let's wait so until this is not loading completely it will not move to the next step this is what is happening still running once the page load is done yes it will stop now let me pass opt here and let me quit this one and execute it once again this time you will see the session which is running will be coming with extension so yes started now you will see one option here can you see this option this is because of the plugin that we have downloaded now click on this and you will see select this out okay so now let's do one thing let's right click on a specific element first of all you can see this option selectors up you can directly continue using this but let's say you want to use it completely now it will take some few seconds and it will start the developer tool okay let me just keep it you can see these three dots right click on the bottom one so it will show you here Now by default uh selector's Hub comes to the last so if you see this two arrows click on this and you will see the selectors Hub you can simply drag and drop here and you can start using it now you don't need to go back to the previous browser inspect again and come back here now you can continue with the current active window that's a very interesting use case and I have taken example of selector sub but you can take any example and you can continue now let me give one more example so let's say you have one more extension call test case Studio I want to load test case studio and selectors hub together in the new browser which is open so I will just go back here and again get the CRX of this plugin copy here now allow me a movement let me copy this file and let me keep in the extension folder so yes I have copy pasted so now once I got the CRX file of test case Studio now I can use that as well for example we have added only one extension here now I have one more extension so if you put Mouse over again you can see we can pass multiple files files dot dot dot so I'll put comma here and again new file and I can provide the separate extension so this part will remain same right because in the current working directory in the extensions folder and now I can copy this name okay let me just remove this here and I can paste here that's all now it will be starting a session with two extension like this you can continue with number of extensions and it will start so let me execute once again and let me close the previous one this is also previous one so now if you click on this extension you will be getting to selectors HUB and test case studio now in case if you're new to test case Studio I made a dedicated video test case Studio it's amazing it can increase the productivity because it has lot of features which I don't want to reveal in this video check out the video and you will get to know amazing features of test case Studio as well so that's all about this video uh that's a quick video again I want to give you one more hint that let's say you don't want to give a file you want to use list of files so in that case the concept will remain same only thing is in terms of writing the syntax will change for example if I want to show you copy this as I'm creating the separate example but you can continue with the same example so I created one more copy in this way let's say now I don't want to use file where X I want to use list of files so I will say ADD extension now let's go with the second one list of files means I need to create a list now I have multiple options to create list either I can create arraylist uh sorry list why I release list why link list or I can use arrays class so I can use arrays dot as list and now here also it is asking T dot dot dot right basically I can pass multiple options depends on what options I will pass I will get the list of that type for example if I give multiple options as string I will be getting list of string if I pass everything as an integer I will get list of integer here I will pass everything as a file so I will get list of files and this is what this ADD extension requires so it says arrays.list new file then we will continue with second file and so on so in this case you just need to copy this file path here and file path here that's all so here I have used arrays.list which will return me list of files which will be added to the extension now again the second syntax if you think it's a little complicated you can still continue with this first approach now there is a small assignment for all of you right now I am using dot dot for current working directory there is a dedicated way where if you don't want to use dot because sometimes dot doesn't work in different operating systems so you can use system.getproperty that also you can give a try so that's a enhancement for you take it as an assignment remove this Dot and try to use system.getpropertyuser.dir and let me know the output so yeah that's all for this video in case you find this use case interesting little different then please share your love show some comment like this video share with your colleague friends those who are working in selenium yeah in case if you need more scenarios like this let me know in the comment section and I will see in the next video till then bye take care
Info
Channel: Mukesh otwani
Views: 12,958
Rating: undefined out of 5
Keywords: How to Automate Chrome Extension In Selenium, Add Extension In Selenium During Execution, selenium webdriver, selenium webdriver tutorial, selenium tutorial
Id: TOlmh0kz9mc
Channel Id: undefined
Length: 15min 1sec (901 seconds)
Published: Thu Jun 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.