How to write a Generic Function (Code) to Handle DropDown in Selenium (Interview Question)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is nathan here welcome back to naveen automation labs so guys uh uh it's not about cucumber today it's about one very important scenario that is uh how to handle drop down with a generic a function that we have to write we know that okay we have a select class and then with the help of select class we could use select by visible text select by index and uh uh select the value also we can do that i'm talking about only drop downs having the select class html tag so today someone was asking the question in the telegram group that uh how can we create a simple one generic method which will handle all these three conditions instead of creating three generic functions over there so first i'll show you how to create three generic functions and then i'll show you we will convert all those three functions into a single function how to do that very important and i think it's really good interview question as well so first of all this is the application where you can see that we have two drop downs are available this is the industry drop down this is a country drop down you can see it and if you really want to inspect uh this guy you can see that these drop downs are having select tag over here and having multiple options like that so this is a select text so of course i could use my select html uh uh select class in selenium i can do that so what i'm going to do that first i'm going to create uh two by locators for this guy so these two by locators are already with me so i'm just going to copy paste these locators you can see that having some id is equal to this for country drop down same thing you can check it for the industry drop down id is available over here like that so i've created two by locators let me import these by locators now what i'm going to do i'm going to create a select class object is equal to a new select and then i'm going to simple create a web element over here so i'm simply creating see driver dot find element let's see for country i have created a web element and then let's see i'm giving select one over here and then you have to import select class from selenium and then i'm using select one dot let's see any method i'm using let's select by visible text which value that you want to select so simple writing india over here fine and if you run it india will be selected so let's quickly run it and let's see what happens so let me just click on proceed and you will see that browser is getting launched and india will be selected from the country over here you can see that right but what if tomorrow you have 10 drop downs are you going to create 10 times select class because let's see we have country drop down i have to create this particular country this particular select class once again let's see select 2 object i have to create and then this time we have to pass let's see for industry i want to select a different industry let's see from the industry i can select something let's see education so then i'm passing education over here let's see this is the industry drop down so from the select two i have to select education from the country drop down i have to select india over here so what if tomorrow you have 10 drop downs are you going to create 10 times select class object no so this is the worst way of writing the code so but it will work if you run it let's quickly run it and let's see what happens you see that browser is getting launched and it will select education as uh over here in industry drop down and india from the country drop down but this is not the right way of doing it so what i'm gonna do that i'm going to create a couple of generic functions over here to handle this particular scenario so first let's see first of all i need a web element so i quickly create public void a get element method let's see i'm making them static so that i can call them directly no need to create the object and this static web driver i have already defined over here guys uh we are not creating any page object model or right now we are not doing any panel execution just to avoid the object of this particular class i'm making it static so that we can call them directly and we can access this particular driver over here as well quickly so let's see this is the get element method and i'm passing a by locator over here now this method says that okay fine we will return driver.find element what is the locator that you are using it so we will return the find element so instead of wide i'll be writing web element over here so this is a method and then we have to import web element from selenium so this is the method which will return it will give you a web element and then i'm going to create a method over here that is let's see for the visible text i'm simply create a method that is public static void i simply say that okay do select by a visible a text over here and this guy says that okay you give me the buy locator so whatever the buy locator let's see i simply write by locator comma which value that you want to select fine and here i'm going to create the select class object select select is equal to a new select and then i have to get the element first so i'll be calling this particular method over here because select class constructor says you give me the web element you can see that that's why we are passing the web element over here we are creating the web element over here same thing i can call this particular get element method passing the same by locator over here so this is fine and by using this particular select a reference that we have created select dot select by visible text i'll be using and which value whatever the value that you are passing i'll be selecting that particular value simple now you don't need to write these lines again and again no need to create multiple objects you just need to call this particular method let's see this method which locator country locator which value that you want to select i want to select india from here same thing let's see i'll be passing the second guy is that a which locator i'll be passing industry locator and which industry you want to select let's see passing education so you can call these methods by passing the respective by locator and respective values and then this method at the runtime will create the gate element will get the web element from this particular method and then select the value on the basis of that by using select by visible text so i have created a wrapper function over here and i'm using this particular generic function to select the values so let's run it and let's see it is working or not okay so yeah browser is getting launched and you can see that education and india perfectly working fine so advantage with this function is that i'm just going to create only one function and can be called multiple times no need to create no need to write the select class objects again and again although we are calling this particular function two times right whenever it's required i can call them and then i'm just i'm just writing only once select equal to a new select over here if you're calling two times two times select class object will be created but no need to write the duplicate code or repetitive code again and again so you can maintain the script properly same thing we have another method also so i quickly write other two methods as well that select by index and select by value also you have that so those methods i have already created so let me just copy paste these two methods over here so this guy said okay select by index and select by value doing the same thing you give me the by locator pass the index that is integer and i'll be using select by index and pass the same index over here third method i have written select by value also so you can select the respective value it means whatever the option values are available series if you are passing value equal to arrow space the respective value will be selected from the drop down right select the value that we have to pass it over here so these are the three generic methods i have written but this is the perfect solution fine i would always recommend it okay fine we can create these three generic functions but the interview question the person was asking that can we convert all these three functions into a single function it means one single function will should handle select by visible text select by index and select by value can we do that of course we can do that right so what i'm going to do that i don't need to call this particular function now and what i'm going to do i'm going to create a method over here let's see my method name is simple i'm going to select public void select drop down value and here this guy says that okay fine you give me the by locator when you give me the buy locator and you give me what type of method you want to use select by value select the visible text or select by index so we'll say that okay we will pass the value and then which value that you want to select so i'll be saying this is the value india or education whatever the value that you want to select i'll be passing now what i'm gonna do i'm going to create the select class object over here so let me create select select is equal to new select over here and this guy will be calling get element method so let me call get element pass the same by locator so that we can create the object of select class now what i'm going to do i'm going to maintain one switch case statement over here i can handle multiple keys over here what will be the key the key is a type whatever type that you are passing and multiple cases i can handle my first case if i say that okay hey you have to give me the index if the case is equal to index then what i'm gonna do i simply write select dot select by index which index whatever the value that you are passing guys now see this value will be coming in the form of index and this guy says that okay you give me the integer index right it's coming in the form of string only and we have to use an integer so what i'm going to do i'm going to convert that particular a string into integer by using integer.parseind methods so i'll be using integer dot pass end whatever the value that you are passing convert that and then let's say i'm passing five over here with double quotes because i have to pass a string over here and the double quotes will be removed will be converted into an integer by using parseint method and i'll be using the same value over here and likewise i can create another case the second case i'll be writing what if if you are passing the value so in that case i'll be using select by value over here so i simply write select by value and the same value i want to select in that case i don't need to convert why because this is what i'm expecting from that particular value so select by value i'll be using it right the same value i'll be passing over here because select by value saying you give me the string value and then simple select the value case number three i'll be using what if we are passing a visible text over here so i'll be passing let's see a visible text and in that case i'll simply write select by visible text and whatever the text that you are passing in the form of value string you just select that particular value and then good to go and then i'll come inside the default part and i print a message over here let's see if you're passing someone is passing the wrong case i simply say that a nice message over here that please pass the correct selection criteria something like this i just okay i'm passing over here and then simple break the loop break the case every time fine now i simply call this guy select drop down value by locators we have already created and which by looking at a lh i would select country and the value is what i simple see this select drop down value we have to i'll do one thing once again yeah this method we have to call select drop down value and this guy says that give me the type which type that you want to select so i simply say that okay this time i'm passing by index comma which index you want to select so i simply pass let's see five over here right guys so here you see that this is the by locator here i'm passing the index in the string value five over here and let's make it static so that you can call it directly so i can call it directly no need to call them no need to create the object okay so i simply call this particular method passing the country drop down and i'm using index and then 5 over here 5 will be given to this guy it will check what is the type you are passing i'm passing select by index over here this case will be satisfied and we select the index convert that value into an integer and then select the value like that so let's see the fifth index is getting selected or not so let's run it and let's see okay so you can see that the fifth value got selected over here right so this is zero one two three four and five fifth value andorra got selected perfect now let's see some other simple using select drop down value this time i'm using let's see industry over here on the basis of what on the basis of visible text i'll be using so i simply pass the same case over here on the basis of visible text and which value that you want to select so from the industry let's say i want to select education and then let's see it is selecting or not okay so browser is getting launched so you can see that it's selecting industry as education and country as endura right now let's do one more time i'll be using select drop-down value i'm passing country once again and i simply say that use value option whatever this particular value it means select by value and which value that you want to select so let's see from the country i want to select india over here right and let's see finally it is selecting india or not so i'm just creating only one single method one single function which is handling all these three cases either you passing index visible value and visible text or the value now you can see that education and india got selected over here this is what the interview question and this is what the expectation people were expecting to convert these three functions into a single function over here you can use this also or you can create three different functions like that as well you can do that now one thing you can improve in this particular code that if you see that okay yeah this is a kind of hard coded value and let you tomorrow selenium guys they have added some more types to select the values what you can do you can maintain one either interface the static variables or you can maintain some enum also over here so let me do one thing let me create one enum i'll go to the same package i'm going to create on let's see um sorry not like this right click on it new go to class and i'm going to create let's see drop down and click on finish over here right and instead of this what i'm going to do that i'm going to create instead of class i'm maintaining one enum over here indium drop down and here i can do one thing but i can maintain some values like this so let's see index is my value and then i'm going to override my tostring method from the object class i simply write public a string or two string and then return what so if i'm accessing index it should return index over here like that okay so this is one of the way you can maintain enum you can use constant variables also so let's see this is for index this is for i will be writing let's see for value and this is if the value is there then you have to return value so i simply write let's see a value and if someone is saying that visible text so i'll be passing a visible text as well so let me put a first of all a comma over here and return value and one second here so a simple writing let's see first let me write it and then we will fix up error over here visible our text and yeah okay this is for the value and after this we have to okay close this particular index with a comma and close this particular value with a comma like that now it is fixed perfect so these are the three uh enum values that i have painted whenever i'm calling drop down dot index it should return index over here right so let's see i can remove this hard coded value and i can simply do one thing that that is this is what exactly we do it in real time projects drop down dot see you can see that index value and visible text so let's say i want to select index but index will return what it's a type of drop down only so what i have to do i have to use a two string again to convert into a string because here we are expecting what we are expecting one string type okay so we can do it like this same thing for visible text i can simply write that is a drop down dot a visible text dot two string i can do that and same thing for value i can use drop down dot a value dot to string i can simply do that so here it it's looking more elegant and you simply go to index it will return the index tomorrow if you have more options like index value and this is for visible text actually so i have to write visible text over here okay and let's see it is working or not okay so let's see first of all right click on it run as a java application browser is getting launched awesome it's selecting education and india if you really want to try one more app let's see i want to select something else i simply call this particular method select drop down value for let's see i want to select again for industry and which type so i simply say that drop down visible text for the industry we have already selected so let's see this time using drop down by value and which value that you want to select so if you go to this particular application and you open this particular industry drop down and here this is the value this is before the visible text and this is for the value so let's see i want to select um this time let's see health okay it should select health care from the drop down if i'm passing value as health over here so i simply pass health and let's see it is selecting or not so value dot tostring and run it again so if you don't want to maintain enum guys you can maintain some interface the static variables also and using uh uh interface name dot variable name also you can do that so can you see that it's selecting healthcare over here so it's absolutely working fine like this you can maintain one single function or multiple functions now the good thing is that these all these functions are absolutely working fine and you can use for any application any web-based application where the html tag is select for the drop down remember guys because we are using the select class concept over here it will work only and only for select class select class means if the html tag is select over here then only it will work it won't work for both strap drop downs or maybe jquery drop down accordingly you have to create a function for that okay but this is only applicable for a drop down having the select html tag so that's all for this particular video guys i really wanted to cover this thing so don't worry our kickball videos will be there and we will continue with the cucumber series but someone was asking the question and instead of uh giving the question in the mail i thought of creating a video so that it can be helpful for all of you guys thank you so much guys thanks for watching naveen automation labs please subscribe to the channel to watch something interesting or something new in the automation just like that and if you have any interview question with respect to uh any scenario feel free to tell me in the comment section i'll try to prepare a video on that thank you so much guys thanks for watching davine automation labs
Info
Channel: Naveen AutomationLabs
Views: 19,012
Rating: undefined out of 5
Keywords: selenium webdriver, dropdown handle using selenium, dropdown in webdriver, select class dropdown, bootstrap dropdown, Jquery drop down, how to handle dropdown in Selenium, Naveen AutomationLabs
Id: AqTUbO4v0h8
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Thu Dec 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.