Skip to content

Commit

Permalink
[extension-selenium] Add ability to handle exceptions for locator fil…
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Dec 29, 2021
1 parent e05821c commit 9e0edc5
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;

import javax.inject.Inject;
Expand All @@ -28,10 +27,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.ui.action.search.ElementActionService;
import org.vividus.ui.action.search.IElementFilterAction;
import org.vividus.ui.action.search.IElementSearchAction;
import org.vividus.ui.action.search.Locator;
import org.vividus.ui.action.search.LocatorType;
import org.vividus.ui.action.search.SearchParameters;
import org.vividus.ui.context.IUiContext;

Expand All @@ -47,29 +44,8 @@ public List<WebElement> findElements(SearchContext searchContext, Locator locato
{
SearchParameters searchParameters = locator.getSearchParameters();
IElementSearchAction searchAction = elementActionService.find(locator.getLocatorType());
List<WebElement> foundElements = searchAction.search(searchContext, searchParameters);
for (Entry<LocatorType, List<String>> entry : locator.getFilterAttributes().entrySet())
{
IElementFilterAction filterAction = elementActionService.find(entry.getKey());
for (String filterValue : entry.getValue())
{
int size = foundElements.size();
if (size == 0)
{
break;
}

List<WebElement> filteredElements = filterAction.filter(foundElements, filterValue);

LOGGER.atInfo().addArgument(() -> size - filteredElements.size())
.addArgument(size)
.addArgument(entry::getKey)
.addArgument(filterValue)
.log("{} of {} elements were filtered out by {} filter with '{}' value");

foundElements = filteredElements;
}
}
List<WebElement> foundElements = searchAction.search(searchContext, searchParameters,
locator.getFilterAttributes());
List<Locator> childLocators = locator.getChildLocators();
for (Locator attributes : childLocators)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
Expand All @@ -39,6 +40,7 @@ public abstract class AbstractElementAction implements IElementAction
private IWaitActions waitActions;
@Inject private IExpectedConditions<By> expectedConditions;
@Inject private ElementActions elementActions;
@Inject private ElementActionService elementActionService;
private Duration waitForElementTimeout;
private boolean retrySearchIfStale;

Expand All @@ -55,18 +57,19 @@ public LocatorType getType()
return type;
}

protected List<WebElement> findElements(SearchContext searchContext, By locator, SearchParameters parameters)
protected List<WebElement> findElements(SearchContext searchContext, By locator, SearchParameters parameters,
Map<LocatorType, List<String>> filters)
{
if (searchContext != null)
{
return findElements(searchContext, locator, parameters, false);
return findElements(searchContext, locator, parameters, filters, false);
}
LOGGER.error(IElementAction.NOT_SET_CONTEXT);
return List.of();
}

private List<WebElement> findElements(SearchContext searchContext, By locator, SearchParameters parameters,
boolean retry)
Map<LocatorType, List<String>> filters, boolean retry)
{
List<WebElement> elements = parameters.isWaitForElement()
? waitForElement(searchContext, locator)
Expand All @@ -77,16 +80,16 @@ private List<WebElement> findElements(SearchContext searchContext, By locator, S
.log("Total number of elements found {} is {}");
if (elementsFound)
{
Visibility visibility = parameters.getVisibility();
try
{
return Visibility.ALL == visibility
? elements
: filterElementsByVisibility(elements, visibility, retry);
List<WebElement> filteredElements = filterElementsByTypes(elements, filters);
Visibility visibility = parameters.getVisibility();
return Visibility.ALL == visibility ? filteredElements
: filterElementsByVisibility(filteredElements, visibility, retry);
}
catch (StaleElementReferenceException e)
{
return findElements(searchContext, locator, parameters, true);
return findElements(searchContext, locator, parameters, filters, true);
}
}
return List.of();
Expand Down Expand Up @@ -119,6 +122,42 @@ protected List<WebElement> filterElementsByVisibility(List<WebElement> elements,
}));
}

private List<WebElement> filterElementsByTypes(List<WebElement> foundElements,
Map<LocatorType, List<String>> filters)
{
List<WebElement> filteredElements = foundElements;
for (Map.Entry<LocatorType, List<String>> entry : filters.entrySet())
{
IElementFilterAction filterAction = elementActionService.find(entry.getKey());
for (String filterValue : entry.getValue())
{
int size = filteredElements.size();
if (size == 0)
{
break;
}
try
{
filteredElements = filterAction.filter(filteredElements, filterValue);
}
catch (StaleElementReferenceException e)
{
if (retrySearchIfStale)
{
throw e;
}
LOGGER.warn(e.getMessage(), e);
}

int filteredElementsCount = size - filteredElements.size();
LOGGER.atInfo().addArgument(filteredElementsCount).addArgument(size)
.addArgument(entry::getKey).addArgument(filterValue)
.log("{} of {} elements were filtered out by {} filter with '{}' value");
}
}
return filteredElements;
}

private List<WebElement> waitForElement(SearchContext searchContext, By locator)
{
return waitActions.wait(searchContext, waitForElementTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.vividus.ui.action.search;

import java.util.List;
import java.util.Map;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
Expand All @@ -29,8 +30,9 @@ public ByLocatorSearch(LocatorType type)
}

@Override
public List<WebElement> search(SearchContext searchContext, SearchParameters parameters)
public List<WebElement> search(SearchContext searchContext, SearchParameters parameters,
Map<LocatorType, List<String>> filters)
{
return findElements(searchContext, getType().buildBy(parameters.getValue()), parameters);
return findElements(searchContext, getType().buildBy(parameters.getValue()), parameters, filters);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,13 @@
package org.vividus.ui.action.search;

import java.util.List;
import java.util.Map;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

public interface IElementSearchAction extends IElementAction
{
List<WebElement> search(SearchContext searchContext, SearchParameters parameters);
List<WebElement> search(SearchContext searchContext, SearchParameters parameters,
Map<LocatorType, List<String>> filters);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.vividus.testdouble;

import java.util.List;
import java.util.Map;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
Expand All @@ -27,7 +28,8 @@
public class TestElementSearch implements IElementSearchAction
{
@Override
public List<WebElement> search(SearchContext searchContext, SearchParameters parameters)
public List<WebElement> search(SearchContext searchContext, SearchParameters parameters,
Map<LocatorType, List<String>> filters)
{
return List.of();
}
Expand Down
Loading

0 comments on commit 9e0edc5

Please sign in to comment.