Selenium Integration with SauceLabs || Run Selenium Test on SauceLabs Cloud

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is naveen welcome back to naveen automation labs so in the last whiteboard video i told you that you can execute your test cases on different clouds like source labs browser stack lambda test aws google cloud and everything so aws microsoft azure cloud i have already practically also have shown you in my previous videos and today i'm going to show you about how exactly you can execute your selenium test cases on uh on bra let's see on source labs okay and later or i can tell you about browsers tag in the next video so sauce labs as i uh told you they are the cloud vendor providing company you can execute your web mobile application automation test cases on their platform as california-based san francisco based company so what you just need to do guys that you need to first sign up you have to create your account over here so sign up for a free trial you can go to sourcelabs.com sign up page just create your account first name last name and everything over here and then after that you have to sign in so let me click on sign in and i'm going to sign in directly over here so the moment i sign in that you can see this particular page and what you have to do you have to go to your account and go to user settings they will give you one important key that is called private key this is the private key you can see over here you can regenerate the private key or access key also this is very important this will help you and it will be unique for you for every account it will be different so you cannot use my private key i cannot use your private okay your private key and or something like that so you have to generate make sure that okay the private key is available over here and this is the uh hub url as i told you that this is the you can see saucelabs dot com 443. so there's hub is running on this particular machine wd hub over here so we have to use this particular uh this is your username that you have created so my username is this naveen animation20 you can see so this username will be created you have to give when you sign up when you register for the first time and you can see colon this is the access key the private key at the rate run my test cases on this particular cloud 443 wd hub so this is the cloud driver creation hub server over here if you really want to change your password and everything you can do that you can regenerate your access key that's it after that your test cases will be visible over here under automated test results and then i'll show you how to do that now back to this uh coding part i have already created one maven project i'm pretty much sure you all are aware of how to create a maven project we just need to go to palm.xml file and need to add couple of dependencies so i just copy paste couple of dependencies like webdriver manager test ng and selenium java i'm using over here you can see selenium java 3.141.59 web driver manager i'll be uploading this particular project in migrate repository also so you can refer from there now quickly go to srt test java what i'm going to do first i'm going to create a quick base test over here so that all the test classes will be extending this particular base test i don't want to write a plain script i just want to maintain at least some basic level of uh utilities and the base test i'm writing so base test and click on finish in this particular base chart i'll be defining my all the precondition methods like before method and after method okay all the setup and the ta down method i'm going to define over here so let's see i'm going to create one first of all one web driver reference over here okay at the class label so that it can be accessed by all the methods so let me import this after that i'm going to create one at the rate before method you can use before test also but let's see i'm taking before method and then i'll be using public avoid this is my setup method so let's see this is my setup method okay in this particular setup method i simply say that okay the setup method says you give me the browser name whatever the browser name that you are passing and on the basis of that okay let me print our pentalin i simply say that i print this particular browser name browser name is this plus browser name okay now this address method that we have to import from testing perfect now what i'm going to do that source labs provide different capabilities as i told you that whenever you have to send the request to the sourcelabs hub server we have to pass we have to use two things one is uh some desired capabilities second thing is that we have to use a remote webdriver concept instead of typical chrome driver so we have to send some capabilities so you have to create the object of mutable capabilities guys which is actually coming from selenium so let's see this is my source object that i'm creating whatever like reference i'm creating is equal to a new mutable sorry capabilities object that you have to create and then there are certain capabilities are there so i'm just going to copy paste those capabilities no need to write otherwise it will take some time so these are the capabilities that you have to use method name i'll tell you in some time now couple of information you have to pass the three in two important things that you have to pass your username and you have to pass your access key here so username is this although username password and xs key we should not hard code and we should not expose over here you can provide in jenkins if you're running from the jenkins you can provide in the jenkins password manager also as a global uh secret key that you can pass that we can do it later second thing is that you can maintain this username access key in your environment variables also and from there you can access that with the help of system.getproperty but but please don't assume that okay and why i'm using this when we use in the real-time projects that time you can remove these hard-coded values you can pick the values from the environment variable so i don't want to spend time over there so that's why coming over here so please let's not judge that okay why naveen is using username access key over here directly okay okay after that we have to use the concept of the desired capabilities we have to create the object or desired capabilities as well is equal to new desired capabilities object that we have to create and along with this particular desired capabilities dot set capability method is there so what is the capability name so this is the capability name that i got it from uh so source colon options what are the different source laps options that you want to use these are the source options that mutable capabilities that we have defined you can use that other than that you have to pass two more important things one is a browser version and the platform on which platform and on which browser that you want to run so i'm just going to copy paste those desired capabilities so you can use these capabilities as well like i want to use browser version is latest browser and what is the platform name so let's see platform name i want to use windows 10 i want to execute my test cases on windows 10 over here right okay now these are the common properties or desired capabilities that we have to use now i'll be checking a check over here that okay if browser name okay that you are using if browser name is equal to google chrome then what do you want so a browser name is equal to chrome first of all i'll be downloading the dot exe file so i'll be using web driver manager okay dot chrome driver dot setup using the bony ratio ap and dot set capabilities we have to use one more method dot set capability and what is the capability name so capability name guys that you have to use the browser name over here so browser version and the platform name i have already used but now you have to use the browser name so i simply write browser name so all these capabilities options everything you will get on the source labs okay documentation so from there only i had taken that what is the value the browser name obviously the condition is satisfied with chrome so i'll be using the chrome over here like that okay so second thing is that uh yeah platform that we have already mentioned now if you really want to maintain with the same thing with firefox so i'll be maintaining one else if condition over here with the uh let's see if the browser name is equal to firefox so instead of chrome driver i'll be using firefox driver setup and the browser name will be firefox over here that's it okay so these are the capabilities and this particular capabilities object name cap is having all the capabilities what kind of capabilities all the my username password sorry username access key tag build everything along with that some other desired capabilities like browser version platform and the browser name these are the mandatory fields mandatory capabilities that you have to pass along with that username and the access key make sure your username access key is exactly right you are taking it from here from your account username is this and this is the access key that you have to copy paste okay this access key you have to copy paste okay guys so make sure you're using the same access key over here now after that instead of writing uh web driver driver equal to new chrome driver i'll be using the driver driver is equal to this driver already defined over here is equal to the new remote web driver because i have to execute my test cases on the grid machine on the cloud machine so i'll be using remote webdriver along with okay on which remote address what is the remote address that you are going to use i want to use this remote new url and here we have to pass the url over here so what is the ui so the url it's defined over here okay so you simply copy this and this will be the url let me just paste it over here what i have to do just copy this over here up to 443 wd hub and one more thing guys first time it will ask you that okay which a cloud that okay in which region you want to take your cloud so i have used first time when you register your account with the source labs you have to select you want to use for europe or us so i have selected a usb site okay like that so you just need to copy this and here this is the url you have to write like this https colon double slash this wd hub on demand dot u swiss if you have selected european so it will be like this eu.saucelabs.com something like this and what is the capability capability object is cap that we have to pass okay perfect and then this new url that we have to first of all import from java.net package and then it will give you one track edge exception handle with the dry cage if any okay url exception or any something like this perfect okay and then i'll be writing this is my at the rate after method i'll be writing over here that public void this will be my teardown method and in teardown method i'll be writing simple my driver.quit right and i'll make one maintain one flag over here that always if you really want to use that first let me import this i simply write that always run is equal to 2 okay now so this is the first configuration and after that rest of the things will remain same so i'll pick one application and the application is also provided by sourcelabs you can pick any application that doesn't matter so i really found this particular application is really good sourcedemo.com you go to source demo and dot com okay yeah so they have given different username password over here you can see you can use the standard user locked out user and the password is same secret sauce so let's see when you log in okay and the password is secret source so this is the application that i'm going to use you can use any application guys freecrm.com or google.com or amazon or whatever you want to use it's up to you so this is a typical uh dummy uh one sample ecommerce website that they have created i'll be doing one thing that okay first i'll check how many items are there you can see six items are available in this particular table and how many add to cart buttons are available so i'll be writing two test cases with the assertions in testing g perfect now i'll be doing what now i'll be creating one [Music] i'll do one thing first of all let me create a package my package name is com. my test package and move this particular base tends to buy test okay in this particular package so that all the test cases are available over here now i'll be creating my class and my class name is i'm going to create source test whatever the name that you want to create and here i'll be writing my test cases and source test is actually extending base test so that it will take teardown and setup method directly from the parent class okay now what i have to do first what i'm going to do that i have to write my test cases now so i'll do one thing let me create a public avoid one method a common method let me i'll be writing do login method where i'll be launching the url first driver.get and the url is something like this let me do one thing let me log out so this will be the url sourcelabs.com index.html and then i have to let's see i'm playing driver dot okay i'll do one thing i'll go to my base test and i'll be applying some i don't want to use implicitly wait so let it be like this that's okay okay so i'll come over here and driver dot find element by a dot id and id of the username so let me check the id is available or not so let's inspect this guy id is using hyphen name yeah it's available and what is dot send keys ld passing directly standard underscore user so i'm not calling any page object model guys so that's why i'm directly calling driver api okay this is just for our practice point of view so that i can show you same thing you can apply in your page object model later on so don't judge why we are writing driver directly inside the test class and everything that's okay this is just for practice point of view just to show you that okay how exactly sauce lab okay we can execute selenium test cases on source labs now same thing for the password the password id is this password so let's copy this and paste it over here and the password is uh secret sauce secret underscore source that we have to use that and after that we have to click on login button so driver dot find element by dot id of the login button so id here you will see login hyphen button perfect that we have created so this is a common do login method i have created now what i'm going to do i'm going to write one enter a test annotation and i simply write public void so when you log in right guys with the standard user and the password is uh this secret source sorry yeah so when you login what happens that i want to check how many items are available so you can see six items are available over here so let me inspect view inspect over here and if you see that there is a class div class is equal to inventory item so you just check the css selector dot inventory item it's saying six it means this is the second item third fourth fifth and sixth item you can see this is first second like that so if i get the count of this i'm getting six it means there are six items are available so i'm just simply checking that okay check inventory item test something like this okay randomly i'm just checking at the retest annotation i'm going to use import and how will you check it first i have to log in so i'll be calling this particular guy do login and when you log in after that i will be using driver.find any men's and by dot i'll be using css selector that we have already taken copy paste and i'll be using dot size over here and then like that so what i'm going to do i'll directly write one assertion that assert dot sir true i'll be using if this particular condition i have to write a condition that if the size is equal to equal to six it means the test is passed otherwise the test is failed right a simple assertion you can write any assertion guys you can print all the values then you can check one by one that's not the target we just need to execute our test cases on the source labs machine that we have do that okay so this is the first test let's see i have written i'm going to write one more simple test over here that how many add to cart buttons are available so let me inspect this add to cart so you can see that okay all that to cart buttons are have having one and two card text over here so i can create one xpath xpath will be like this button text is equal to this so you can see one of six we are getting so let's copy this and i'll be using expert in that case okay is equal to equal to six and here i'm checking check add to cart button okay but until something like this let's see i have written do login and that's it i'll give a priority also if you really want to give priority is equal to 1 and priority is equal to 2 okay okay let's do one thing for the safer side i'll be using my implicit weight also later on if you really want to use explicitly weight that you can do that so driver dot manage dot timeout dot implicitly weight off let's see 10 seconds i'll be using it now let's make it to 20 seconds okay for the safer side it's implicitly weight will be applied for all the driver.find element whenever we are creating the finder okay a web element over here so two test cases i have written and what exactly it will do the prerequisites and the setup method is already defined over here along with the desired capabilities over here it will execute my test cases on this particular remote machine so tomorrow let's say you're running on aws cloud or docker cloud or let's see sauce labs or browser stack or lambda test every time you have to change this particular url you can read this particular url from your properties file also from your json file or from your xml file also that you can do it later okay first let's see it is working or not right now what i'm going to do to execute my test cases i have to create one uh src test resources i have to create an xml file also so let me do one thing let me create a a folder over here let's see this is my test run that's folder under this particular test runners i'm going to create one simple file let's say testing.xml file test ng underscore i will give any name regression.xml okay yeah so this file got created and here we have to get the number of tags and number of test blocks that we have to give so i have one sample with me i'll do one thing that uh i'll pick it from somewhere else okay because i don't want to write the xml codes so let me pick from here i just copy this entire stuff and simple paste it over here and we will modify according to our project so first of all let me do one thing that we don't need all these tests so let me remove that so what i want that i don't want to use any listener also so remove that i want to execute my test cases in parallel mode yeah that's good i give something some name this is my source labs test automation regression suite and what exactly you are going to test so i'm giving some name over here this is my source test on firefox source test on chrome right now here what i'm passing and passing the browser is equal to firefox and browser is equal to chrome and we have to give that our package name along with the class name so my source test is available under this partition package right dot whatever the file name the class name is source test please execute this particular test on firefox same class has to be executed on let's see on chrome over here now i'm passing this particular parameter name so this parameter will be given to base test under this particular method before method so here what we have to do here we have to write one parameter that is called at the right parameters annotation that we have to use that over here right guys so for doing this what we have to do we have to apply at the rate parameters and which parameter the parameter you have to pass in double quotes that parameter name is browser that you have to pass it like that okay so whatever the browser parameter that you are reading it from here this is the name of the parameter and the value is firefox will be given to this guy and the firefox will be given to this guy it means firefox okay we'll set all these properties it will check it is equal to firefox uh setup firefox set capabilities with firefox with browser name and let's see it is working or not right so i'm running in parallel mode so for the first test block it will prick browser as firefox and for the second one browser is equal to chrome so let's see it is working or not let's try so execution should happen on the soft slabs machine and we will see that if i can come over here so what you have to do you have to go to your source labs and here you have to go to automated section test results section you have to go and let's see you know test case is not picked yet it's giving some error that before method browser is required okay i think let me do so we have to pass it like that okay now let's run it again sorry i forgot i just put the double quotes before the curly brackets now let's run it again and let's see so now it's picking firefox and chrome and let's see it is picked over here now you can see that two jobs over here can you see that both are running on windows 10 one is chrome 85 windows 10 one is chrome 80. sorry firefox 80 you can click on any of the job and it will give you the video also it will give you the live preview over here see my firefox is getting launched and let's see it is picking the test over here or not perfect see you can see that it's entering the username password and it should click on login and then it should verify add to cart and those six items over there it's slightly slow but let's wait for few seconds and let's see meanwhile you can directly go to the test results over here in the new tab so that you can check what is happening with other jobs i hope our script is fine is it still in progress in the children queue okay so i think it's done now you can watch the video once again and uh you can refresh this that you can refresh this dashboard once again and yeah this is still in progress interest is still running it's saying the test is currently running you can see that okay on my chrome it's entering the username and the password i hope you have entered see we are not clicking on it see dot click is not working we have we forgot to add dot click right okay let's do one thing let me terminate this that is the mistake that we have done sorry about that dot click we just created the web element okay now let's run it again sorry about that so okay let's run it again this time it should work fine that's why i was like why it is not working i was surprised to see that so let's go to test results once again and now you can see two more jobs okay in progress you can see the latest jobs are this windows 10 this and chrome 85 and now you let's see on chrome what is happening and both are running in parallel why because in testing g dot xml file we have already given parallel equal to test with thread count equal to three and i think it's perfectly done see yeah it's awesome and let's check on uh go to previous page so i think almost done yeah so this is completed now it's running on firefox firefox is still running okay but here you can see the result all the four test cases got passed over here and here you can see yeah perfectly fine let's see launching the sauce laps and it's almost done perfect you can watch the video once again if you really want to watch that it took around 22 seconds to launch you can watch the video you can okay maximize the video something like this so see this is a full screen it's running on firefox and let's watch this particular quick video entering the username password perfect checking the things over there add to card and everything and then done perfect so you can see that as the results guys over here so we have four test cases y4 white is showing four because uh two test cases for chrome and two test cases for firefox that is how we have configured in our regression.xml file it means sourcelabs is having two test method running on firefox softlabs is having two test methods running on google group that is what you will get four jobs over here see one is chrome firefox 80 firefox 80 chrome 85 chrome 85 they all are completed absolutely working fine right so this is all see browsers are not getting launched on my local machine they are getting launched on the source labs just because of we have configured like that only just because of this thing okay make sure you are passing the right username and the right access key over there now second thing is that this these jobs are unnamed we are not able to identify that okay what is the method name what which test is getting executed and then let's see i want to i have already hard coded this windows 10 what if i want to execute on different different combinations of platform in the browser so that also we can do that so what i just need to do that go to your regression.xml i'll create one more parameter over here let's see on for firefox i want to use windows 10 right and for uh name i'm giving let's see platform name okay platform name something like this or platform name let me write it like this okay same thing over here that uh for firefox for sorry for google chrome i want to execute on the mac machine so platform name predefined mac machine name that you have to give so what we have to do guys just open google.com simply write source labs platform list so you have to pick the exact platform list okay platform name so go to this platform configuration okay what you just need to do that w3c api the which device you want to execute on mac machine so select mac machine and which operating system that you want to select let's see i'm going to execute on mac catalina and uh which browser so let's say i'm going to select on uh on chrome let's see on whatever the browser so not both i'm not bothered about the browser i'm bothered about this mac os 10.15 okay so let's pick this mac os 10.15 right so after that what we have to do that that's it so what will happen we will go to the base test and we have to pass one more parameter over here that comma what is a platform name right so platform name we are picking it from here platform name and platform name we have to store in this particular platform name variable as well so what is the browser name what is the platform name right to avoid the confusion what i'm going to do that okay instead of a name i simply write platform over here so that platform we are writing in the xml and here in the base test we are writing sorry not here just remove this okay this is my yeah here okay platform name that's the platform name and the same platform name will be given to this guy now you can use so browser will be given to browser name platform will be given to browser name sorry this particular platform name now same platform name i'll be using over here to remove this particular hardcoded value right so we have removed this hardcoded value with respect to platform name perfect and second thing is that guys that inside the setup method what you can do is you can pass one more parameter that is called method through the reflection api so method is coming actually coming from the java.reflection package java.lang.reflect and what you can do is that you can use this particular name dot uh let's see get i'll tell you one method name is there get uh get name what is the method name that you really want to use let's say you thought yeah you can use this and store in a particular string variable let's see this is my method name is equal to this in the same method name you pass it over here to the source options with the name key so same method name we are passing you over here so in the job when you run it whatever the test method name will be visible okay whatever test method name that you are passing it will be visible over there so let's run it and let's see it is working or not so let's run it again okay and i'll go to my sourcelabs dashboard go to test results once again can you see that check inventory item test running on mac with guru m85 check inventory item test running on windows 1080 so whatever the platform that you are passing it showing over here it means running on that particular machine and first it is executing inventory item test so you can see the test name over here earlier that we were executing that that was an unnamed job so we were not able to identify that okay hey what is the job name over here so this is a really good configuration that you can pass it programmatically from your script and then you can see it over here and now you can see that okay add to cart button test running on mac machine and running one windows machine on windows we have configured okay i want firefox on mac i want chrome 85 so likewise you can create multiple pairs in your xml file and you can decide accordingly you can pass accordingly you can write your logic accordingly and you can see that now the results are absolutely working fine on chrome you can see that on chrome our two test cases are working on firefox two test cases are absolutely working fine right guys so this is what we can do with respect to source labs okay later on if you really want to integrate with jenkins that also you can do that you just need to trigger the same build from the gen gains and it will be executed on the source wraps cloud so the purpose of the source class as i told you that we are running our test cases on the source labs not in the browser not in the sorry in the local browser right running on there and how exactly they are maintaining their vms or containers or whatever it's their problem we don't need to take the headache of this and there are multiple configurations are available you can go to this platform configurator and you can take all these platform name browser version source options you can pass 73 browser so browser version also see right now i'm passing what in my test if you see the browser version i'm passing take always latest version so you can pass the browser version also from the test engine.xml file that okay hey what is which browser version firefox and firefox version with platform also you can pass and the same thing you have to uh pick it over here in the form of parameter and pass the same instead of latest you have to pass 7374 which is coming from the xml file that you can do that okay so this also you can simply do that if you really want to if you really let me show you that also if you are getting confused so browser browser version so i simply write let's see what is the version that you are passing so version let's see for firefox let me quickly check that uh what are the different options we have let's see for firefox 78 version i want to use so 78.0 that you have to passwords so 78.0 on windows 10 i want to do that and for google chrome okay sorry this is the firefox 78 okay yeah this is good and then i'll be using one pc pc is let's see windows 10 what is the browser that i want to use so browser let's say i'm going to use 79 for firefox so 79.0 for firefox let me write it like this 79.0 and for mac for mac machine i'll be using mac this which browser chrome browser let's see i'll be using chrome 73.0 okay so i'll be using let's see the version 73.0 so this is the configuration that i want to use and the same version that we have to pick it over here platform version and the version we have to as a holding parameter also we have to use that version name right and the same version name that we have to write it over here instead of latest okay because you can see the same browser version they are also showing the same browser version over here so instead of doing the hard coded value we can directly pick from the xml file now let's see it is working or not okay so we will go to the source labs test results you can see test got started and now see the configuration check inventory item test running on mac machine 73 chrome perfect windows 79 firefox so this is the configuration that we have done and the execution is happening over here if any test is getting failed whatever the reason it's saying session could not be created because we are using very old uh browser so maybe it's saying that uh invalid capabilities with chrome we are using it okay it's showing invalid capabilities with mac because 73 is very old right now chrome 85 is running right so i can do one thing that let me terminate this let me terminate this we will pass something latest because 73 is very old for mac so i would say let's see with 85.0 and firefox 79.0 let's see let's refresh this page and let's see it again yeah now you can see that mac 85 it got picked okay and you can monitor the results but anyways so whatever the result will be there that is a different thing either it will be passed or failed but we can see that that tests are getting executed the browsers are getting launched on source labs so this is what i really wanted to cover in the next video i'll tell you that okay same exact thing you have to do with the browser stack and the lambda test here and there some capabilities and the private key and the host the ip address in the port number that we are passing will be different right now we are passing like on demand source labs dot com in uh in case of browser stack it will be browser stack dot com now see on the latest version everything is working fine and you can see the results over here you can watch the video you can share this particular dashboard with your team members and everything you can see that perfect so that's all for this particular video guys i know this is slightly long video but i really wanted to show you how exactly we can execute on sauce labs thanks for watching naveen automation labs please subscribe to the channel share this particular video with others with your colleagues and friends and if you really want to use the capability of soft labs please feel free to do that i'll be uploading this particular entire project the selenium cloud project on my git repository and you will get the get repository url in the description you can check it over there and you can clone the repository and please try let me know if you have any issues thank you
Info
Channel: Naveen AutomationLabs
Views: 10,418
Rating: undefined out of 5
Keywords: selenium, saucelabs, Selenium Integration with SauceLabs, run selenium test on saucelabs, saucelabs vs browserstack, selenium webdriver, selenium grid on saucelabs, selenium by naveen, saucelabs by naveen
Id: Q3JrBokM4FQ
Channel Id: undefined
Length: 40min 12sec (2412 seconds)
Published: Fri Sep 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.