'Need to check results after page refresh 5 times
Hello I am trying to automate this test case where i need to first search an item, then store the count of the total results, then store the first 5 counts from the result and then refresh the page 5 times. At every refresh i need to verify that the results are same, and pass the case if and if only after each refresh results are same, else fail the result. Below is the my code which i am trying, using if and for loop, the get.attribute() is giving stale element exception. Stuck here as how to get it done.
<div class="ui-lib-sidebar-grid__content">
<div class="ui-lib-margin-b_md ui-lib-margin-t_md totalResultsText">
<p class="totalResultsText">Showing 1-10 of 316 results</p>
</div>
<div class="" style="">
<div class="ui-lib-category-list ui-lib-category-list-dummy">
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24">
<title></title>
<circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle>
</svg><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Collection and Transfer of Green Waste from Residential Areas" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/WasteCollectionandTransportation/RequestforCollectionandTransferofGreenWastefromResidentialAreas"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Collection and Transfer of Green Waste from Residential Areas</h4></a>
<div class="ui-lib-category-list-item__description">Through this service, you can request the collection and transfer of green wastes from residential areas.</div>
<div class="ui-lib-category-list-item__tags">
<div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Expat</span></div>
<div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Visitor</span></div>
<div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
List<WebElement> TotalSearchResults = driver.
findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div"));
int Total_Count = TotalSearchResults.size();
List<WebElement> FirstFive = driver.
findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div//a")).
stream().limit(5).collect(Collectors.toList());
int First_Five = FirstFive.size();
System.out.println(FirstFive.size());
List <WebElement> After_refresh = driver.
findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div//a")).
stream().limit(5).collect(Collectors.toList());
//Thread.sleep(10000);
driver.navigate().refresh();
Thread.sleep(10000);
for (WebElement webElement : FirstFive){
String link = webElement.getAttribute("href");
System.out.println(link);
Solution 1:[1]
I still didn't understand your complete code. But on seeing the exception, you need to find the elements all again.
After you have refreshed, the FirstFive elements are no longer attached to DOM. So, after refresh, if you want to get the FirstFive elements, you need to again find the elements.
Something like this:
//Just retained the locator which you have mentioned as it is
By byCategoryElements = By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div//a");
@Test
public void refreshValidation5Times() throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
List<String> firstFiveData = getFirstFiveData(driver);
for(int counter = 0; counter < 5; counter ++) {
driver.navigate().refresh();
Thread.sleep(10000); //10 seconds is a huge time. Instead try to wait for some element presence.
List<String> nextDataSet = getFirstFiveData(driver);
if(! compareTwoLists(firstFiveData,nextDataSet)) {
throw new Exception("Data is not matching after refresh");
}
}
System.out.println("Data is matching after 5 refresh attempts");
}
public List<String> getFirstFiveData(WebDriver driver) {
return driver
.findElements(byCategoryElements)
.stream()
.limit(5)
//From your above code, it seems you want to validate href data
.map(we -> we.getAttribute("href"))
.collect(Collectors.toList());
}
public boolean compareTwoLists(List<String> list1, List<String> list2) {
return list1.stream().allMatch(list1Data -> list2.contains(list1Data))
&& list2.stream().allMatch(list2Data -> list1.contains(list2Data));
}
Edit 1: You need to traverse the FirstFive to get the link and print. Modified the above code. Please check it now. Please note that, what am mentioning here is just an outline. Request you to please modify it as per your need.
Edit 2: Updated as per your latest comment. Is this what you are expecting.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
