Automate CRUD (Create/Retrieve/Update/Delete) UI Use Cases with Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is navini welcome back naveen automation labs and back to the selenium question series so very good interesting scenario that we are going to cover today that is how to automate current uh use cases on ui application correct means create retrieve update and delete for example add edit and delete these kind of things that we have together so for example i have on this dummy addressbook.com application where you can just sign up you can enter your username password and then again you can sign up with the same username password so i've already created the account on this site and if you go to the addresses link here you can see number of you know addresses entries that i have created this is a list of addresses if you really want to create a new address just click on new address and you have to fill this particular form and then click on create address and the same address will be displayed over here as a new entry duplicate entries are allowed because this is just a dummy website you can just again show it and see the content here you really want to edit that also you can do it you can edit for a specific row also let's say i want to edit this guy click on this edit you can destroy also so let's say for example i want to destroy just time and delete are you sure you want to delete click on ok and the entry will be deleted with a message over here so the question is that it looks very easy right we have to click on this new address fill this particular form and then click on create address but how will you design a proper use case with a proper code maintenance and then how what are different best practices to automate the crud based scenarios right we have to add we have to retrieve we have to delete and we have to edit and update it's a very famous interview question also design some test cases what will be your design and we have to avoid writing the boilerplate code means duplicate codes so let's see how to do that so uh i'll be using selenium with java with test ng so this project is already there with me and then i'm going to create a separate package for that and i'll be using a puzzle approach the plain object uh plain old java object approach where we will be using getter setter constructors and uh instead of passing so many parameters for example let's see if you want to click on new address and you have to pass let's see five six parameters to add the values mandatory or non-banded reveal if you really want to add it in that case uh better to pass the cons in the form of object instead of uh you know passing seven or eight parameters to a particular function it looks ugly and let's see by using getter and setter by using constructor how can we update and get the values as well okay so i'll do one thing without wasting our time let me create a package here and let's see the package name is crud and then after that click on finish in this particular src main java i'll be writing some basic implementation classes and my uh some other data classes or puzzle classes so let's create a class over here and we have to fill this form and then if you click on directly on create address without filling anything it's saying okay these are the five attributes or five uh elements our mandatory fields are available it cannot be blank so we'll just focus on the mandatory field if you really want to add some more values that also you can add it doesn't matter so for example let's see i'm going to create one puzzle class that is my address class no main method click on finish and in this particular address class what are different other uh class variables or private encapsulation that i want to use it i simply say okay these are the private buyer private locators for example let's see fine this is my string first name that i want to enter first and likewise i have a first name the last name address one city and zip code so i quickly create some private buy locators like that so first name this is my uh last name perfect so these are the private variables that i have declared and then after that what exactly i'm going to do that i'm going to create one public constructor of this class so let's go to source and then click on generate constructor using fields select all these fields to create the constructor so one constructor will be created here you can see that i don't need any super keyword so here you can see the constructor got created constructor will remain same as the name same as the class name after that i need some getter and setter also because at the runtime i want to fetch the respective values and then after that we have to capture and update the values also at the runtime so i want to create a template of the address class in the form of puzzle so let's do one thing right click on it and go to source and generate getters and setters select all and then click on generate so here you can see the getter and setters are ready you can use lombok also if you want to use it so i don't want to use lombok right now then you can avoid some boilerplate code like getter insider in the constructor so let it be like that okay so this is a class template that i have created now i'm going to create a page class or for example the implementation class for let's see this is my class and then this is my address page class that i'm going to write and don't select the main method if you're using the page object model uh okay we can achieve that also so here you have to maintain your locators and the page actions and everything for example let's see first my bilocator that i'm going to create here so this will behave like your selenium code and the private y locators that you have to maintain it means for example whenever you want to add the values how exactly you are going to add it over here so right so uh the implementation will be we have to enter the values and we have to maintain some private buy locators so let's see i'm maintaining some private buy locators for the first name click on inspect i just want to fill only mandatory fields i want to take much amount of time just to fill the values because that really doesn't matter and let's see this is my private buy locator and this is my first name actually so let's see first name is equal to buy dot id and then i'll be using this id and uh here like that so this is my first private buy locator and then first name and then what we have last name address one city so address one perfect and then import your buy locator from here okay after that i'm going to create one constructor of this class so that we can get the driver so i'm going to create let's see this is my address up page and then this guy says fine you give me the web driver reference here and then i'm going to create one private a web driver reference over here so that we can use this a driver okay with the help of this keyword i'm going to use this.driver is equal to driver it means this class is not responsible for creating the webdriver initialization whatever driver that you are giving same driver will be given to this guy with the help of this keyword so that i can use those things in my uh public page actions so i'm going to create the proper concept of encapsulation where private by locators and then the public page action that i'm going to write so i have to create four important features four important features means uh create the address and then uh update the address delete the address and then the get the address like that so four method that i have to create so first i'm going to write let's see add address over here right and this add address is saying fine you if you really want to add the address let's see initially it is void in nature if you want to add the address you have to click on new address and then you have to fill this particular form perfect so what i have to do is that first of all that i'm going to create another method over here for example this is my public avoid and let's see click address a link method that i have created and this method is saying okay fine that you have to click on new address i'm going to inspect this guy also and uh let's see here i'm not going to create two pages guys that you can do it later i'm not following the exactly page object model uh you can create two separate page classes also one is for the uh list page another is for the new address page but i'm maintaining inside same page locator same page class so that i don't have to write this you know multiple classes like that so let's see this is my what this is new address link that i'm going to create so new address a link which is equal to by dot link text i can use it and this is the thing and then i'll be using this new address link to click on this address link so i'll be using what i'll be using a driver dot find element and this is new address link and then dot click perfect and then we have to add the address so first i have to call this particular method then only i can right then only i can go to this particular page so if you really want to add the page chaining model also this method should return this method should return the next landing page class object but i'm not doing page chaining model here simply click on it and fill the form perfect so i'm just going to fill this form now driver dot finally meant and what exactly you want to add the first name right the first name locator that you want to add but dot send keys which value how exactly you will pass the value so let's see i want to pass first name last name address it is zip code it means five attributes i have to pass comma separated five parameters that i have to use it here no it's like not recommended and then too much parameters tomorrow let's see you have to fill all these form 10 different uh values that i have to fill 10 10 comma separated parameters and argue arguments we have to pass so in this case i'll do one thing that better you give me the complete object of the address class and then at the run time i'll take it so i just use this address class reference here and this address class i say okay find address dot and i'll be using the getters here so see this i'll be using get first name so when i call this method that i'll be passing the address class object in that address class object with the help of constructor or with the help of setter i'll try to fill the values and then i'll just pass the object reference here and from this object reference you can just get the first name last name and everything so same thing i'll be doing this guy also driver dot finally meant and then that is for last name dot send keys i'll be using my address dot uh get last name perfect so when i fill this entire form then you have to click on create address so you have to create one more locator so let me just quickly ins inspect it and the name i can use it is equal to commit there is no id available so i'm going to maintain one more private buy locator and this is my what this is the button name is create address so i'll be writing okay create address which is equal to by dot name locators are very straightforward in this application that's good and then create address i have to click so let's do that driver dot find all meant create address dot click here so this method is is when you fill this form so what will happen for example let's see i'm entering something over here first name last name then i'm writing address one and then i'm writing city then i'm writing some zip code here that's it and then i'm clicking on create address so it will give you a message over here that address was successfully created so i just go to check this locator and then using the get text or just check okay this method this message is displayed or not or maybe first name is displayed or not something like this or any important thing that you want to uh check it that you can do it so after clicking on it i'm going to create a simple driver.find element by dot i'll directly do one thing i'll just write a validation on this success message that is your alert three classes are available so i can create one a div dot these three classes i can use one css selector and then i'll be using the get text over here so i'll be writing by dot creating one private by locator here once again and this is my i would say success a message is equal to by dot css selector and then this one perfect and then on the basis of this success message i'll be writing this success message and what exactly you want dot get text and then whatever text is that you are getting then you exactly return it so instead of void i'll be writing string and then when i write my testing g class right that time whatever that you're returning the string that you're returning right i'll be validating that so that should be part of your test case right not part of your page objective method page object method should never assert anything so let's see this is my first method is ready likewise i'm going to create another method that is update address right nice and you see that i'm not using any single hardcoded values but see this carefully i'll go back to the addresses i want to update a specific entry i want to update let's see edit click on this edit but what exactly most of the people they do mistake here that existing record which is already available today and then they just click on edit and then try to update what if this record is not available what if this record is deleted from the database so always a best practice is that whenever you are going to edit edit should be an independent test case first for edit first we have to add it and then edit it same thing you want to delete first you added and delete the same record same thing for update same thing for uh you want to get it first you add it and get the same record do not create unnecessary dependency between two test cases or three test cases otherwise uh the maintenance and the data is mismatched it will always give you a problem okay so that's why we are following the best practices over here so let's see i want to update that you'd first click on new address create an address and then the same address you update for example let's see first i create this guide naveen123 and click on edit so i'll be clicking on edit and then i'll update something let's see your last name is one two three something like this updated and then your city is something like this and then you click on update address and then i'll say okay yeah address successfully updated this message or maybe some validation i can do it over here as well so in this case also saying that okay fine you give me the address uh you know object reference and then on what basis for which um you know for which record so you have to pass at least some unique key over here like you know update by id update by first name update by last name or whatever so let's see i'll just click on this address i say okay fine if the first name is equal to naveen123 then only i want to edit so i have to click multiple edits out of all these multiple edits have to click only one edit right so how will you decide in that case so in that case we have to use only for naveen123 so we have to pass some unique key something like this this unique you could be first name last name city or any email id or whatever it depends on your application okay so i'll do one thing that okay fine you give me the first name update address by the first name and give me the address object and inside this address object you tell me what exactly you want to update so in this case what exactly i'm going to do that fine i'll first of all again click on the address link so that we will click on it and then we will land on this particular page so this is my initial thing and then i want to what so it will come over here and then i want that you have to select the specific uh first name and then click on edit so you have to use a proper xpath concept over here so for example let's see i want to click edit of naveen123 record so how will you do that so in this case i'll start from here and here you can see the td is having one two three so i'll create one custom x path over here with the help of xpath xs which is like text is equal to this one and then i'm going to use the following sibling that because i have to reach over here to this particular td where a edit is available so i'll do one thing this is my following sibling right and sorry following a sibling colon colon td is giving you six and then i'm writing that okay you have to go to this particular td where a is available so i'll write a where the text is equal to edit so see it is pointing to this so you just this is a variable part so this is a hard credit value that you have to remove it so i'll do one thing that okay fine if i'm passing the respective value for example let's see if i'm passing money see it is showing the money for this edit button for money only if i'm passing this r e r e r so in that case what r e r e r is showing edit so i got a pattern here right so in this case what exactly i'm going to do that okay fine that this is your driver dot find element by dot x path i'll be using and this is my expert uh uh expression and the only thing is that you should not use a hard code value that's why i'm passing the first name here and then appending here so i'm going to append it over here like this simple string concatenation that i'm going to use it means first you have to do what find this element and then dot click click on this edit button or edit link or whatever for the respective given first name that has to be given by the user from the test case no hardwood values here and when you click on it and then you have to edit how will you add it again you have to fill this particular form and respective values that you want to add it that you will be passing with the help of address object here so i'll do one thing that fine that i can simply call this particular add address method once again that also i can do it or i can do one thing um see i can remove this line first of all or just let it be like that or i can just take this entire code once again from here to here and later on you can create a method also for separately for this and then using this and i'm using the driver dot finder element here and then instead of return void i'll be writing a string perfect so get first name last name address one this and that and then you have to click on create address and then that's it perfect so here uh this is your first name so this first name is also actually same i'll do one thing i'll simply say this is my first name or you can use this also here perfect so this f name i have to append over here like this so it means you click on this first address link click on edit and then fill this particular form on the basis of the given address object values perfect so this is my update address is also ready you can avoid this duplicate code same code is written over here you can create a common method just to fill the address and then use this code and then call that method that also we can do that if you really want to do it right now let's do it i simply write okay public void fill address form and the fill address form says that you give me the address reference and this uh duplicate code that i'll be using first name last name this and that and up to return just cut it from here and then paste it then this guy will give you a string whatever the successful message is coming and this fill address form we have to call it from here that's it now it looks very elegant pass the address object that's it and then same thing you have to return from this guy perfect and that's another thing is that i don't need all these things it means i mean after this i can remove this code also click on address click on edit and after this just fill the address form once again okay so common code that we have written here okay so this is update address so add is done update is done then i'm going to write a delete address so i'll write public void uh delete the address so how will you delete it so same thing that you have to delete a specific uh you know on the basis of a specific first name so you again you click on addresses and let's say i want to click on delete this particular money and click on destroy and it asks you okay fine are you sure you want to click on it right so pop up that you have to click so in this case again i'll be clicking starting from here this guy and then you have to tell me that for which uh address that you want to delete so i'll say okay fine find on the basis of the first name it means find that particular row on the basis of first name naveen123 money or this re rer and then click on destroy so i'll be one doing one thing that exactly same thing i'll be using it here instead of edit i'll be using destroy perfect and then you just simply click on it destroy and when you click on destroy then the pop-up is coming so i can use a webdriver weight utility for example you see let's see uh it is coming after a few seconds the pop-up is coming after a few seconds so i'll be writing my web driver uh explicitly wait that i'll be writing it let's see i'll be saying that for example uh driver comma for 10 seconds that you have to wait and then wait dot until i am writing my expected conditions dot if alert is present you simple do what that accept it that's it okay and when you accept it and then click on it means okay and the value got deleted and then you are just verifying that address was successfully destroyed or not so i can do one thing i can return the same message uh from here okay like this and then instead of white i'll be writing a string here because every message is having the exactly same css selector this success message right that you have created having the same css selector for create update and delete also so this delete address is also ready see the see the code guys it looks very elegant no hardcore values nothing and uh which is like proper example of crud operation that we are doing it and then finally what we have to do we have to get the address so i'll write okay public i'll be writing void get address detail or whatever so in this case you have to sell okay find that which address that you're looking for get address right and i can do one thing that uh specific address that you're looking for so you have to click on show this show and then you can just see the for couple of information here okay so i'll first i'll do get address again i have to create the address so again let me just click an address link like that okay but see this carefully i have to get the address on the basis of what so first i'll click on it and then you have to click on show for the respective first name on the basis of that so i'll be writing okay fine you take this particular first name and you just need to uh click on show so i'll be writing instead of destroy i'll be writing show here and then you click on it so i'm just going to click on it and the respective value so let's see i'm just verifying the first name is displayed or any other thing that you want to check it that you can do that so i do one thing okay fine that return what driver dot finally meant and uh what so driver.finally meant if the first name is money here so let me just uh inspect this guy and uh this is span with money right so let's see this is my first name i can use the data test over here so this is my span and the rate data test which is equal to first name like this and i can capture the get text over here so or you can maintain one quick buy locator here as well so i'll be writing this is my private by a first name not as a field first name i would say the value which is equal to uh buy dot xpath and this is the expert that i have created so maintain one private buy locator if first name value i'll be writing it over here and then you return the dot get text here perfect and uh and simple return and install void the simplest one so get address also you just need to pass this first name so you need to you don't need to pass any address complete thing here that you can avoid it okay so these are the four crud operation that i have created add update delete and get fine and then one utility method i have created now it's time to write the test cases and let's see one by one it's working or not so i'll go to src test java creating a separate package here okay let's see this is my address test cases and creating one address test class okay don't select the main method i'll be using test ng here so make sure that you are using test ng i'm not designing complete page object model so let's see i'll start with the before test annotation start with public void and the first method that i'm going to write that is what the setup method right so let's see this is my uh setup method in this setup method i'll say that first you import before test i'm going to initialize the web driver manager and all such things so i'll do one thing that user name password that we have to enter so this code is already with me so like this so first time what is happening here that uh you first time sign out we will start from the here we have to enter the username password and click on sign in this is what exactly i'm doing it so first you uh maintain one uh web driver also so you can maintain one private web driver here okay lend this scenario guys but try to understand it's very interesting learning for you guys equal to chrome driver or you can write across browser logic all so that we are not bothered about it i'm using implicitly weight that also you can avoid it later on you can provide some explicitly weight launch the url enter the username password and then after that click on sign in so for clicking on sign in that uh what you just need to do is that you just simply write this one driver dot find element buy dot name comment and dot click so fill the values and then click on sign in and then after that uh that's it okay and what you have to do is that you have to start writing the test cases and let me write first at the rate after test also that teardown method that public void teardown in tier down i'll write okay fine driver dot quit here just close the browser once everything is done okay now i'm gonna do that that i'm going to create my first at the rate test annotation here i'll be writing okay fine this is my public void and i'm going to write create address test okay now who which method is responsible for creating the address in my address page add address method is available right you just give me the object of this and then just simple create the address of this so how will you do this in that case so we have to call this method and we have to pass this so first we have to create the object of address class so let's create it first here so i'm going to create one address class object address address is equal to new address and now your puzzle class will be applicable here so here you can see address over here and this one first name last name address one city this will be your test data that you have to pass and then the test data will be given to the constructor and with the help of this keyword all these class variables will be initialized so i'll do one thing that okay fine my first name this data you can read it from your excel file or whatever from your test data source so right now i'm doing the hard coded values here so for example let's see my first name i'm writing that naveen and the last name i'm writing let's see automation and then address one i'm writing everything is in a string let's see i'm writing uh whatever let's see new uh address city is equal to bangalore for example and let's see i'm writing some zip code something like this five six zero zero one one or whatever okay so this is the address object that i have created now see this carefully i have to call this method so to call this method i have to create the object of address page class then only i can create the then only i can call this add address method and just pass the address reference here so inside the setup method i can do one thing first before that at the class level i'm going to create one address page okay reference and then i'm going to initialize this particular reference with an object is equal to new address page and then pass the driver the driver we have already captured so this method will initialize the driver and the same driver will be given to this address page and address page is expecting one driver here you can see the driver will be given to this guy and the same driver will be given to this guy and then so that i can use the same driver in my public actions methods here perfect this is a typical example of page object model and then object is created after that what should i do the address page i can use it and i can call all the methods of the address page dot which method you want to use add address so when i call this add address method okay it will click on this click address link and again fill the address so i can make it private because i don't want to call this method directly so let's make it private in fact see this click address a link method also we have uh created which is uh perfectly fine right so this method will be called click address link and fill this particular entire form i'm making it private because i don't want to give this particular uh you know method exposure to the uh directly to the test line so just call this particular method and internally this method is calling this one perfect so but where is the address object this is address object and that's it so now your test case is looking very elegant and let's see this is actually adding this object or not so i'm just going to run it run as a test ng let's see so browser is getting launched enter the username password and after that it should click on addresses let's see okay see it is not clicking on addresses okay so it's saying let's see i think we missed one step no such element exception new address okay so we have missed one step that after clicking on uh commit and then you are going to new address and then click on addresses a link and the new addresses that you have to click it means first you have to click on addresses over here first okay so what we have to do is that not new addresses actually we have to use addresses link so let me just inspect that because then only it will click on new addresses right so addresses text we have to use instead of new addresses okay and then after that it will click on i'll write this one that is your address a link here so click on this particular address link and then you start filling the form perfect and then when you start filling the form then after that before that you have to click on new address also so this new address also we have to maintain so i'll do one thing i'm going to create one more by locator for the new address link so i'll be writing new address link and this is new address actually okay so one extra locator that we have to maintain so new address link where exactly you will be clicking on it so after that you just come here and before filling the form you have to click on it so driver dot find element by dot what buy dot um sorry this one driver.find element i'll be writing this the locator is a new addresses link and then dot send keys and uh sorry not just trying keys we have to do a dot click here it's a link dot click perfect now the flow is perfectly fine let's see it is working or not so run as test ng okay so okay it's still not clicking on it new address actually no worries we will see where exactly the problem see again it's saying that uh new addresses actually sorry again my mistake guys it's not new addresses actually it's only new address okay you can see new address not addresses okay let's run it again sorry about that so here you can see that fine sign in click on it and then fill the form and then done perfect the test is absolutely fine and here you can crosstrack you can refresh it and you can see the naveen entry got created here right so this is perfectly fine you can add data provider also and multiple test data that you can pass and then it should work perfect now i'm going to write another test and for example i'm going to write public void the next test i'm going to write the update address for update address also as i told you first what you have to do for updating the user also do not believe on this entry that you have recently created because you never know i just want to execute this guy so do not create any dependency between create address and the update address for update address also first you create a fresh user and then after that you always update the same user so let's see i'm going to create another user here the same line of code that i'm going to write it here and this time let's see i'm going to create instead of naveen automation i'm writing naveen automation labs and then let's see the first name also marine labs new address and labs i'm just adding one single line and the address is also let's see bangalore labs and then 5602 create this so this guy will add the address and then from the same address page i'll be writing dot what i have to update so when you create this user right with this thing you want to update something so let's see i'm just going to update this guy so how will you update let's say i want to update its last name or address one or city instead of bangalore some other city so i'll be writing setter over here now see this so address uh whatever the address object that we have to use so how will you get the address object now so i'll be using the same address object dot i'll be writing my setter here setter will help me to update the specific entry so for example let's see i want to update its last name so initially it was automation lapse so i'm writing let's see my last name is change now um whatever so let's say i'm writing k-u-n-t-e-t this is my surname and then i want to address a dot set city also instead of bangalore let's say i'm writing new york perfect and these two things i want to update and then i'll be writing my address page dot this update address method so this updated address object i'll be passing and for which user the username is first name is naveenlabs so i can do one thing this address dot get first name also i can use it instead of this hard coded values perfect so this is also the first name will be added so this add address method also initially be returned right it is returning this fill address form and retain the successful message after filling this if it is displayed then it's successfully fine so if you really want to use get text that also you can do that so i'll say one thing that okay when i um let's you go to address click on new address and then i'm writing something like this address one city is this i'm writing some random zip code and then click on create address so you will be getting this message address successfully created okay so when you create the ad address create user right without assertion your test is not completed so i'll be writing one assert dot assert equals okay it means add address method will return what it will return one string here so i'll be storing inside the string that actual or i would say that uh success message is equal to this if this success message this one is expected message is this what this one address successfully created or not it means my test is created successfully otherwise tests fail same thing i'll be writing for update also but in this case address was successfully updated actually okay you can cross check once again let's say i'm going to add it this guy added only let's see address two here and then update address so it's saying only updated here perfect and i have to use the success message after updating this guy perfect so that is the use case of update address that's why we are returning fill address form and whatever the success message is coming you capture the text and return it perfect okay so let's see this update user is working or not so i'm going to create one update address test okay so first it will create see it will be very fast see naveen automation laps and then again naveen kuda in new york got added but it's saying that update address is saying updated and the created okay but we found created okay so i think we have to go to update address and uh click address link which is fine it's clicking on edit and then fill the address form and see that is the problem here that what exactly will happen that fill address form it will again click on new address link it means it will start creating the new user that i don't want so in this case what should i do that i have to ignore this line so i'll do one thing just like cut it from here and then i'll just use it here only only this is only for uh you know add address so i don't want in a common code section here perfect so let's do one thing once again i'll do one thing let me just refresh it and whatever the naveen labs got created right let me just destroy it okay here now you can see there is no naveen lapse or something like this and now let's run it again so initially it will be bangalore and bangalore to new york the surname will be updated okay see naveen and then same user got updated here and this time it's absolutely working fine you can refresh it and here you can see naveen labs naveen labs bangalore new york the only thing is that i'm not going i'm not clearing the data so that's why in the same entry it's getting created so i'll do one thing that uh when you update the address right here it means before filling the values you have to keep you have to clear the data also so i'll be writing dot clear method here same thing for this guy also so before filling the value first you clear it okay perfect and that's it now let me just delete this record okay now see this time it's not creating two entries only one entry first create and update the same entry now let's run it again see uh we have to keep enhancing the code keem enhancing the whatever the script or the logic that we have written so we have to keep running it and we have to make it stable this is how automation works see now this you can see city is equal to new york and it's absolutely working fine and it will create only one entry initially it was naveen labs with the different surname now its surname is updated to curator and then the city is new york and the same entry got updated here which is absolutely working fine now okay so this is update also working and with the proper assertion then i'm going to write my fourth test case that is delete so say let's see public void delete address test for deleting the user again first you create a new user so let's create that new user so this is the user that i'm going to create let's see naveen i'll deliberately writing a name naveen delete automation labs delete new address delivery writing like this and let's see bangalore delete and with the same zip code it's fine so first you add it and then i'm going to delete it okay so dot delete address which name naveen delete that you have created on the basis of that you just simply delete this user so what will happen it will come here the moment you see you cannot be in labs or whatever it will just click on destroy this is the this is what that we have written and click on destroy wait for 10 seconds to get the alert and then successfully message that you are getting it read it successfully and then you validate that so i'm going to validate now so i'm going to store inside the string success message which is equal to this writing assertions and then changing the expected value it should be deleted let me just check that how exactly the message is coming it's saying destroyed actually not deleted so i'll be writing destroyed here awesome this is also ready let's see it is working or not so delete address test i'm going to run it see here don't worry about the hardware values of the data here you can use data provider excel sheet various ways of doing it see naveen delete and then it will be deleted perfect so first it got added and deleted and here you refresh it that's why you are not getting any entry for naveen delete so this is also absolutely working fine right then i'm going to create another test at the rate test annotation for retrieving that address so i'll be writing let's see get address test or show address test or whatever that you want to call this is just like you know the api calls that we are doing it so first again i'm going to create the address okay so let's create the address this time you can see okay naveen get so you can manipulate the data whatever data that you want to write let's see if this is up to you what which data and let's see this is my get bangalore get here and let's see five six zero three three so first you add it add the address and then after that what address page dot uh get address which address that you want i want the data that you have created for naveen get as a first name here that's it so get address method will be called first name it will just click on show and that's it so after clicking on address link it will come over here and let's see naveen labs got created first and then click on show and then you can validate that if naveen labs is there or not so that's why on the basis of that it will capture the text right so it will give you the first name here or you can validate multiple things you can return one array or error list also so i'll simply returning the first name naveen get and i'm storing inside this let's see this is my actual a first name and then i'm going to write an assertion that is what the assertion is that assert dot assert equals that is your actual first name and what is your expectation the expectation is uh the first name should be this naveen get so you can write the hard code value also or you can write the getter also here so address dot uh get first name if both are match it means address successfully created and successfully displayed on the page so let's run this guy also let's see it is working or not so see first it will create the user naveen get and see it's saying you can come to this guy naveen get got created and it's click on show and the same naveen get that we are getting it and on the basis of that we are asserting it so now you see the code and then see the maintenance of the code looks very elegant very clean no duplicate code no boilerplate code and then everything is properly designed in the form of utilities and the page object and the page actions the respective method is doing the respective work and the maintenance of the framework maintenance of the page classes also will be very easy these are my private buy locators and then these are my page important page actions that we are using it this is my add update delete and the get returning something and asserting insider test class do not write assert in the page class this is a violation of the page class page object model and then this method should call add method update method delete method and the get method your test change you should call these methods and these method will give you the actual result and then you validate that so it's your test client responsibility to validate whatever the actual thing that you're getting with respect to the with the comparison of the expected result so these are my successful deleted created updated created and all such things these are my expected messages or expected validations or expected first name on the basis of that i am asserting and finally quitting the data and this dot java is behaving like a template a poso for me right a kind of dto for me which is like uh maintaining the address objects here so instead of passing so many parameters first thing that do not pass so many parameters for example for add address and then i'm again calling this particular so i'm just creating the object and passing the reference and from this reference whatever you want so simple write get first name last name address one city zip code like that this is how the developers although they design the backend code for their rest apis for crud operations cred rest apis okay and it's your test responsibility now how exactly the data that you are maintaining so here heart code value it's fine you can maintain some data provider or something like this perfect now for example let's see i'm just going to do this one the application is in this application the bug is that it's allowing the duplicate data so let me just destroy some data what you can do whenever you start the automation right you just destroy everything through automation and on maybe delete everything all the entries from the from the database and then you can uh start running your test cases all the test cases together okay so i'm going to run all the test cases together for these four test cases and let's see but please do not create any dependency so let's quickly run it and let's see so first create perfect and then this is for update or delete or whatever then this is for get alphabetically it will we are not giving any priority alphabetically it will be executed and four test cases are absolutely working fine create address delete address get address update address in the console also four test cases are absolutely working fine so this is how i see the neat and clean code and please so guys do not write the hard-coded values like hardwood values mean the existing user that you are going to use it see for example naveen get is already there and you want to run this naveen get hard code value here directly to update the user to update the address it means whatever after two months of naveen get is not there or after next day also naveen get somebody has deleted this it means this entire code will not work in that case so for every test is important to me first you it's having its own creating the address over here or you can move this particular update address in your before class also before method also that you can do that okay that is up to you how exactly you're designing your before method and before class in test ng right like this same thing for delete address also i first have to create the user and the same username delete only i'm going to delete it i don't want to create any dependency that okay this guy i have created now this naveen i'm going to delete no don't do that because what if your manager says okay fine or your requirement is just to execute this so i don't want to create any you know dependency between these two test cases like i can write depends on method on this also but that i don't want to create it right because for time being let's see this uh create user is or create address is not working so in that case it will never participate so whenever i'm going to run this delete address so it will always giving a fair chance that first you add the address with this user and this time is working and then delete the same user this is the way that we should always automate these registration page or cred operation pages create user create address create employee data or something like this update the same user delete the same user get the same user or same address or something like this we should always automate in such fashion okay now tomorrow the newcomer is coming they can easily understand the code they can easily write the test cases and keep adding number of test cases on the basis of that okay so that is the approach for crud operation i hope you liked the video please subscribe the channel press the bell icon to get a notification for more and interesting videos on test automation java and different tools and technologies this is naveen here and thank you so much i'll see you next video till then take care and god bless you all
Info
Channel: Naveen AutomationLabs
Views: 8,806
Rating: undefined out of 5
Keywords: Automate CRUD (Create/Retrieve/Update/Delete) UI Use Cases with Selenium, CRUD with selenium, CRUD operation automation using selenium, Add/Edit/Delete/get automation, form ui automation using selenium, selenium automation, selenium interview questions and answers for experienced, selenium interview, selenium by naveen automationlabs
Id: UlIoIPZccec
Channel Id: undefined
Length: 48min 51sec (2931 seconds)
Published: Sun Oct 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.