Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Fix bugs detected by Sonarcloud after merging branch 250 #263

Merged
merged 3 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ private void removeComponentByContextMenu(WebElement webElement) {
private WebElement getClickableParsys() {
wait.withTimeout(Timeouts.SMALL)
.until(webDriver -> getParsysStream().count() >= 1, Timeouts.MINIMAL);
return getParsysStream().findFirst().get();
return getParsysStream()
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Clickable parsys element not found."));
}

private Stream<WebElement> getParsysStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ private boolean pageActivationStatusMatches(ActivationStatus status, SiteAdminPa

private boolean pageStatusMatches(PageStatus status, SiteAdminPage page,
String pageTitle) {
bobcatWait.withTimeout(Timeouts.SMALL).until(ignored -> page.getGrid().isPageOnTheList(pageTitle));
bobcatWait.withTimeout(Timeouts.SMALL)
.until(ignored -> page.getGrid().isPageOnTheList(pageTitle));
SiteAdminGridRow row = page.getGrid().selectPageByTitle(pageTitle);
return row.getPageStatusToolTip().contains(status.getStatusCss());
}
Expand All @@ -167,7 +168,7 @@ public SiteAdminGridRow selectPageByTitle(String title) {
}

/**
* Opens the page with provided title by doubleclicking on its row.
* Opens the page with provided title by double-clicking on its row.
*
* @param title title of the page
*/
Expand Down Expand Up @@ -205,7 +206,7 @@ public boolean isPageOnTheList(String title) {
public boolean isTemplateOnList(String title, String template) {
boolean isOnList;
SiteAdminGridRow row = getRowByTitle(title);
isOnList = template.equals(row.getTemplateName());
isOnList = row != null && template.equals(row.getTemplateName());
if (!isOnList) {
LOG.debug("there is no template {} with title {} on the list", template, title);
}
Expand Down Expand Up @@ -247,7 +248,8 @@ public SiteAdminGrid deactivatePage(String title) {
public SiteAdminGrid waitForLoaderNotPresent() {
bobcatWait.withTimeout(Timeouts.BIG).until(
CommonExpectedConditions.elementNotPresentOrVisible(LOADER_LOCATOR));
bobcatWait.withTimeout(Timeouts.SMALL).until(ExpectedConditions.elementToBeClickable(scrollPane));
bobcatWait.withTimeout(Timeouts.SMALL)
.until(ExpectedConditions.elementToBeClickable(scrollPane));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

Expand All @@ -48,7 +49,10 @@ public class Select implements DialogField {
public void setValue(Object value) {
selectField.click();
List<WebElement> options = selectField.findElements(By.xpath(SELECT_OPTIONS_XPATH));
options.stream().filter(o -> value.toString().equals(o.getText())).findFirst().get().click();
options.stream().filter(o -> value.toString().equals(o.getText()))
.findFirst()
.orElseThrow(() -> new NoSuchElementException(
String.format("Option with text %s not found", value.toString()))).click();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.cognifide.qa.bb.aem.touch.siteadmin.aem62;

import java.util.List;
import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void submit() {

elements.stream()
.findFirst()
.get()
.orElseThrow(() -> new NoSuchElementException("\"Done\" button not found"))
.click();
}

Expand All @@ -99,7 +100,9 @@ private WebElement getButtonByLabel(String label) {
n -> n.findElement(By.cssSelector("coral-button-label")).getText()
.equals(label)
)
.findFirst().get();
.findFirst()
.orElseThrow(() -> new NoSuchElementException(
String.format("Button with label \"%s\" not found", label)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.cognifide.qa.bb.provider.selenium.BobcatWait;
import com.cognifide.qa.bb.qualifier.PageObject;
import com.google.inject.Inject;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
Expand All @@ -51,12 +53,13 @@ public class NavigatorDropdown {

public void selectByPath(String path) {
wait.withTimeout(Timeouts.SMALL).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
revealNavigatorDropdownBtn.click();
getDropdownOptions().stream()
.filter(webElement -> webElement.getAttribute(PATH_ATTR).equals(path))
.findFirst()
.get()
.orElseThrow(() -> new NoSuchElementException(
String.format("Dropdown option not found using path \"%s\"", path)))
.click();
}

Expand All @@ -66,26 +69,27 @@ private List<WebElement> getDropdownOptions() {

public void selectByTitle(String title) {
wait.withTimeout(Timeouts.SMALL).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
revealNavigatorDropdownBtn.click();
getDropdownOptions().stream()
.filter(webElement -> webElement.getText().equals(title))
.findFirst()
.get()
.orElseThrow(() -> new NoSuchElementException(
String.format("Dropdown option not found using title \"%s\"", title)))
.click();
}

public List<String> getAvailablePaths() {
wait.withTimeout(Timeouts.SMALL).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
return getDropdownOptions().stream()
.map(webElement -> webElement.getAttribute(PATH_ATTR))
.collect(Collectors.toList());
}

public List<String> getAvailableTitles() {
wait.withTimeout(Timeouts.SMALL).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
ExpectedConditions.presenceOfElementLocated(By.cssSelector(DROPDOWN_ITEMS_SELECTOR)));
return getDropdownOptions().stream()
.map(webElement -> webElement.getAttribute(HtmlTags.Properties.INNER_HTML))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -225,11 +227,11 @@ public boolean isLoaded() {
@Override
public SiteadminActions waitForPageCount(int pageCount) {
boolean conditionNotMet = !webElementUtils.isConditionMet(webDriver -> {
try {
return pageCount == getChildPageWindow().getPageCount();
} catch (StaleElementReferenceException e) {
webDriver.navigate().refresh();
return false;
try {
return pageCount == getChildPageWindow().getPageCount();
} catch (StaleElementReferenceException e) {
Objects.requireNonNull(webDriver).navigate().refresh();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, Guice can't inject nulls.

return false;
}
}, Timeouts.SMALL);
if (conditionNotMet) {
Expand All @@ -240,11 +242,11 @@ public SiteadminActions waitForPageCount(int pageCount) {

private void waitForExpectedStatus(final String title, ActivationStatus status) {
wait.withTimeout(Timeouts.MEDIUM).until(webDriver -> {
webDriver.navigate().refresh();
ChildPageRow childPageRow = getChildPageWindow().getChildPageRow(title);
PageActivationStatus pageActivationStatusCell = childPageRow.getPageActivationStatus();
ActivationStatus activationStatus = pageActivationStatusCell.getActivationStatus();
return activationStatus == status;
Objects.requireNonNull(webDriver).navigate().refresh();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

ChildPageRow childPageRow = getChildPageWindow().getChildPageRow(title);
PageActivationStatus pageActivationStatusCell = childPageRow.getPageActivationStatus();
ActivationStatus activationStatus = pageActivationStatusCell.getActivationStatus();
return activationStatus == status;
}, Timeouts.MINIMAL);
}

Expand All @@ -255,8 +257,8 @@ private ChildPageWindow getChildPageWindow() {

private void retryLoad() {
conditions.verify(webDriver -> {
webDriver.navigate().refresh();
return isLoadedCondition();
Objects.requireNonNull(webDriver).navigate().refresh();
return isLoadedCondition();
}, Timeouts.MEDIUM);
}

Expand All @@ -275,7 +277,7 @@ private void navigateInteractively(String destination) {
}
wait.withTimeout(Timeouts.SMALL).until((ExpectedCondition<Object>) input ->
"0".equals(((JavascriptExecutor) driver).executeScript("return $.active").toString())
);
);
navigateInteractively(destination);
}
}
Expand All @@ -289,11 +291,13 @@ private void goForwardToDestination(String currentUrl, String destination) {
getChildPageWindow().getChildPageRows().stream()
.collect(Collectors.toMap(Function.identity(),
childPageRow -> StringUtils.difference(currentUrl, childPageRow.getHref())
))
))
.entrySet()
.stream()
.min(Comparator.comparingInt(a -> a.getValue().length()))
.get()
.orElseThrow(() -> new NoSuchElementException(
String.format(
"Failed to find a child page while trying to reach %s", destination)))
.getKey());
closestPage.click();
}
Expand All @@ -304,11 +308,12 @@ private void goBackUsingNavigator(String destination, String currentUrl) {
.filter(path -> !currentUrl.equals(path))
.collect(Collectors.toMap(
Function.identity(), path -> StringUtils.difference(path, destination)
))
))
.entrySet()
.stream()
.min(Comparator.comparingInt(a -> a.getValue().length()))
.get()
.orElseThrow(() -> new IllegalArgumentException(
String.format("Unable to find a path to destination \"%s\"", destination)))
.getKey();
navigatorDropdown.selectByPath(closestPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
Expand All @@ -39,7 +40,7 @@ private PageObjectProviderHelper() {
}

/**
* Gets selector from {@link PageObject} if class annoted by this annotation is used in list
* Gets selector from {@link PageObject} if class annotated by this annotation is used in list
*
* @param field class field
* @return selector
Expand Down Expand Up @@ -77,13 +78,13 @@ public static Class<?> getGenericType(Field field) {

private static By retrieveSelectorFromPageObject(Field field, boolean useGeneric) {
String cssValue = useGeneric
? PageObjectProviderHelper.getGenericType(field).getAnnotation(PageObject.class).css()
? Objects.requireNonNull(PageObjectProviderHelper.getGenericType(field)).getAnnotation(PageObject.class).css()
: field.getType().getAnnotation(PageObject.class).css();
if (StringUtils.isNotEmpty(cssValue)) {
return By.cssSelector(cssValue);
}
String xpathValue = useGeneric
? PageObjectProviderHelper.getGenericType(field).getAnnotation(PageObject.class).xpath()
? Objects.requireNonNull(PageObjectProviderHelper.getGenericType(field)).getAnnotation(PageObject.class).xpath()
: field.getType().getAnnotation(PageObject.class).xpath();
if (StringUtils.isNotEmpty(xpathValue)) {
return By.xpath(xpathValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ class StatisticsHelper {

private static final Logger LOG = LoggerFactory.getLogger(StatisticsHelper.class);

private int getNumberOfTests(File file) {
return getNumberOfTests(file, 0);
}
private static final int DEFAULT_NUMBER_OF_TESTS = 0;

private int getNumberOfTests(File file, int defaultValue) {
int returnValue = defaultValue;
private int getNumberOfTests(File file) {
int returnValue = DEFAULT_NUMBER_OF_TESTS;
try {
Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name());
returnValue = nextInt(scanner);
Expand All @@ -50,13 +48,8 @@ private int getNumberOfTests(File file, int defaultValue) {
}

int getNumberOfFailedTests(File file) {
return getNumberOfFailedTests(file, 0);
}

int getNumberOfFailedTests(File file, int defaultValue) {
int returnValue = defaultValue;
try {
Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name());
int returnValue = DEFAULT_NUMBER_OF_TESTS;
try (Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name())) {
if (scanner.hasNext()) {
scanner.nextLine();
returnValue = nextInt(scanner);
Expand All @@ -73,7 +66,7 @@ int getNumberOfFailedTests(File file, int defaultValue) {
if (numberOfTests == 0) {
result = 0.0;
} else {
result = ((double)getNumberOfFailedTests(file) / numberOfTests) * 100;
result = ((double) getNumberOfFailedTests(file) / numberOfTests) * 100;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void canReceiveLatestEmails() {
client.removeLastEmails(emailsNumber);
client.close();

Assert.assertTrue("email data objects should be equal", sentEmails.equals(receivedEmails));
Assert.assertEquals("email data objects should be equal", sentEmails, receivedEmails);
}

@Test
Expand Down Expand Up @@ -169,7 +169,7 @@ public void canRemoveLastEmails() {

List<EmailData> limitedList =
sentEmails.stream()
.limit(emailsNumber - emailsToDelete)
.limit(emailsNumber - (long) emailsToDelete)
.collect(Collectors.toList());

Assert.assertEquals(latest, limitedList);
Expand Down
Loading