Java 8 for QA - Part 15 - Using Lambda in Explicit Wait - Handle Stale Element Reference Exception

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends hi friends welcome back to my channel tastic money bites i am your friend absolute satival and in this video we're going to see about how we can use function interface in the explicit weight that we normally use in selenium again we have been seen how we can what is function interface what is the method signature apply you know all those stuff and in this video we will head towards directly in using our in our test automation framework right so let's go to the intellij workspace and i have already created some basic uh skipper of code um that basically you know uh creates a driver uh for you and then uh it it you know goes to the google page and it finds the element that is queue which is basically your search box in the in the google uh right google website good and i am trying to send the keys a moment so before that i am doing an explicit wait whether i am checking whether the visibility of element is true and then you know if it is the case then i want to send the keys uh my name right maybe i can use something else okay good so this is a simple piece of code and let's check whether it is working okay so it launched the chrome and it entered this and my test got passed everything was working fine so uh here how can we use you know a function interface implementation in this particular piece of code right so let me copy the same code and let me paste it here if you if you closely observe this until method is is actually takes in a function interface implementation okay let's um let's okay remove this and if you notice the function it takes a function interface implementation the the method accepts driver reference and it can return anything right in our case it can written web element or it can return a true if the element is present there anything it's up to you so what i'm going to actually do it takes in d so d represents your driver i know and uh basically i want to find the element again right so you can do that okay by dodge name queue you can do it do like this or you can also do you know even though i i take in this argument it is not you know necessarily that i have to use it so you can also use something like this okay and if it is displayed okay basically i want to click on that particular element or i need to send the keys to it element same keys and then you can send whatever the value so maybe some value i want to send okay good so this is very simple piece of code right instead of writing like this you can write like this what is the advantage i i i don't see any big advantage of using like this uh what is the biggest advantage that you are telling or speaking about see guys here it is restricted you cannot extend this to you know cat or any other needs let's say we introduce some scale element reference exception that we normally face and we will try to avoid that with the help of function interface implementation within the explicit weight itself okay we are not writing extra try catch blocks or we're not handling it externally we will try to handle everything with the help of the lambda expression here itself right let's try to understand first you know the state element reference exception let's copy this piece of code um so after i find this elements from from the web page so basically i'm importing this from javascript executor so what i'm actually doing okay so once i find the element i am removing the element uh same element from the dome okay to do that i am using javascript executer javascript executer is a way where you can inject your javascript commands directly into the code selenium code so what basically i am doing in the in the document i am trying to find the elements by the class name this search box class name is uh this particular value so i am finding the you know it basically returns you a list right get elements will return you the list i am finding the first element of that inside the list and then i'm removing that from the dome completely now if you try to execute this particular piece of code that we have written before it will throw you stale element reference exception let's check whether it is throwing that okay so if you notice okay my test has failed absolutely this is what we want and if you notice this is stale element reference exception the element is not attached to the page document yes because we have removed it from the page document right using a javascript code this normally we you don't have to explicitly remove it this normally happens in a website that is having lot of ajax calls these ajax calls will will change the property of those elements or basically remove them completely from the dome when you try to reference the elements that are already removed then you will get a scale element reference exception okay amazon how we are going to handle this okay the first thing that comes in inbuilt is ignoring so okay scale element reference exception okay there is a method called ignoring the takes in a class okay i am giving hey when you are trying to pull and then find this element okay you have a maximum of five minutes five seconds and you pull every 500 milliseconds during that time okay within this five seconds don't try to throw me a scale element reference exception okay will this work no this will not completely work what this will do it will only ignore it okay it will not throw the exception but it will throw other exceptions okay let's try to run the code and see what's happening basically since the element is not there it cannot throw the scale element now but it will throw that no such element exception right or it will tell the expected condition doesn't match see if you notice waiting for the visibility of that particular element which is not actually possible he cannot throw the stair element reference exception because we have ignored that so this will not exactly handle the scenario so what you have to do maybe after doing this you have to write your statement driver dot navigate dot refresh so you can refresh the page and then you can try to find the element then it will work but if you notice this piece of code that we are writing it you know you know explicitly ourselves and this this may be needed every time what is there is a way that we can handle this inside the explicit weight itself is there any way and that's when the writing of the lambda helps us okay let's try to do what you instead of you know we manually refreshing and doing it what is the explicit weight itself doing it okay we're going to do that so let's remove this and uh let's come on to this piece of code now so so we uncomment this okay so this until statement uh basically you know uh is checking justice is displayed what i'm going to do now is i'm going to write okay and a multi-line lambda okay a multi-line lambda like this and this going to return me driver dot uh what sorry d dot find element whatever by dot name and q okay but before returning that it will try to do d dot navigate dot refresh so now you have an option to handle this within the lambda itself or within the explicit weight itself so this is when the lambda is really good okay good so you can also add any more thing like if you want to add some system statements in in order for you to debug you know how long it takes for the element to be find all those stuff you can normally introduce some logging statement here okay searching um to switching to find the element all those all those locks if you want to add some blocks you know you can also do that so it will pull every 500 seconds every 500 seconds this will be long okay let's let's try to check whether this particular piece of code is working okay basically i shouldn't uh do this maybe i can directly this will return me a web element so i can change them directly here and then testing okay something like this and let's try to run the code so guys this is just a function interface implementation okay if you notice this has entered testing here and you also notice my test got passed so everything we are handling internally okay so if if you guys want to you know handle state element reference exception you before itself you know that on clicking this you get a scale so whenever you are applying a weight or you know explicit weight you can use this particular waiting strategy to return a web element and then you can do whatever the operation you want to do again if you want to be more generic you also want to handle normal cases as well as the state element cases then you can write some if conditions here let's take um again if you are comfortable using only here do it but you know if you want to be this to be more generic then you can also add a few more lines of code by by dot name whatever whatever the by locator strategy that you want to follow you can use that so i am trying to find all the elements uh with with q i am doing find elements because i don't want that to throw null sorry there's no such element exception so find elements will basically return you list with zero if you couldn't find any elements so i am actually uh doing that and doing it is empty check whether if it is empty i don't want that to do something if it is not empty okay then you directly return it okay then you directly return driver dot find elements what are the locator strategy okay i'm hardcoding the located strategy but in your case you can you can you can pass the buy directly okay ah dot get off zero so that's it so if the list is not empty you directly return it because there is no state element reference exception there is no problem so you directly return it else if else okay then you handle it like this okay that's it very simple you find if there is no problem you are in finding the web element you return the directly the web element itself otherwise you search for the element do the refresh and then return it so in this case you are actually you know solving the scale element reference exception and guys so you you might also be wondering right how we can use this effectively in a framework in those cases like let's say i will not be having a generic one you know here let me just handle the scale element reference exception alone okay we don't have to handle everything and let's try to use this in our code uh if you notice we have already a selenium automation framework that we've been using in that suppose if you want to click on a welcome button or link then what i do i will tell what is the locator that i want to do pass and what is the waiting strategy that i want to use so i will check for the presence of this particular link before clicking it and this is for the logging purpose if you go to this method click so we are doing explicit weight directory dot perform explicit weight so it basically do do that particular form of waiting strategy for you and then returns to the web element right and we also log in the you know reports into the console yeah first we are we'll focus only on this let's get here okay if you notice there are different waiting strategies that we have been using here so if you notice uh we are using a clickable strategy where we will wait for the element to be clickable whether present strategy where you're waiting for the elements to be present and the same like visibility so if you don't want any kind of waiting strategy in that case you can use a waiting strategy of none it will directly find the element and return you in this case uh we have already find a new weighting strategy which actually handle the uh fail element reference exception for us so let me include that in the enum type as well so instead of none maybe i can at least handle steel little steel element maybe okay something like this so i have introduced introduced a new waiting strategy and here i can add one more line of code uh else if uh the feed strategy equal to equal to weight strategy dot handle scale element then i want to handle it differently how we want to handle it so this is how i want to handle it okay here in this framework wherever i want to use driver i have to use driver manager.getdriver right and the eclipse wait timeout is coming from my framework constants so i'll fetch it from there so yeah and now this is just going to return me a web element yeah so maybe i don't have to mention this here i'll just tell elements that's it so now we have introduced a new waiting strategy and this can handle the scale element reference exception for you again if you want to do further operations you can also do that the same way if i have to call it from my page class okay what i have to do instead of presence okay i i think or i foresee that you know while clicking the link will come i see there is a straight element refresh reference exception that can happen then i can just simply tell if you wait okay for for the stale element exception you handle it to yourself and then click on it i don't bother about how you click it but i don't want to see any scale element reference exception so this now makes your code more readable and we could able to achieve all these things with the help of you know handling this using a lambda which is actually your functional interface uh you know available in java good guys i hope this is this video if i will find this useful please let me know your feedback and we will see another great video until then tata bye
Info
Channel: Testing Mini Bytes
Views: 862
Rating: undefined out of 5
Keywords: rest assured, Consumer interface in testing, lambda expression in rest assured framework, java, testing, testing mini bytes, explicit wait, how to handle stale element reference exception in selenium using explicit wait, function interface, selenium, exception handling
Id: qmuIBWo4p1k
Channel Id: undefined
Length: 14min 11sec (851 seconds)
Published: Sat Jul 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.