Skip to content

Commit

Permalink
fix: ignoring exceptions when checking secured web elements that are …
Browse files Browse the repository at this point in the history
…not in the currently displayed page
  • Loading branch information
giulong committed Jan 2, 2025
1 parent 82edc79 commit f49f376
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.giulong.spectrum.it.pages;

import io.github.giulong.spectrum.SpectrumPage;
import io.github.giulong.spectrum.interfaces.Endpoint;
import lombok.Getter;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

@Getter
@Endpoint("inputs")
@SuppressWarnings("unused")
public class InputsPage extends SpectrumPage<InputsPage, Void> {

@FindBy(css = "input[type=number]")
private WebElement input;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.giulong.spectrum.it.tests;

import io.github.giulong.spectrum.it.pages.InputsPage;
import io.github.giulong.spectrum.it.pages.LoginPage;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.WebElement;

import java.util.Objects;
import java.util.stream.Stream;
Expand All @@ -15,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.openqa.selenium.By.id;
import static org.openqa.selenium.Keys.ARROW_UP;
import static org.openqa.selenium.support.ui.ExpectedConditions.urlContains;

@Slf4j
Expand All @@ -23,6 +27,7 @@
public class LoginFormIT extends BaseIT {

private LoginPage loginPage;
private InputsPage inputsPage;

@BeforeEach
public void beforeEach() {
Expand Down Expand Up @@ -61,4 +66,18 @@ static Stream<Arguments> valuesProvider() {
arguments("giulio", false, "/login")
);
}

@Test
@DisplayName("even if we have pages with @Secure web elements (password in LoginPage), tests that don't use them must not fail")
void inputs() {
final String number = "2";
final WebElement input = inputsPage.getInput();

inputsPage.open();

input.sendKeys(number);
input.sendKeys(ARROW_UP);

assertEquals("3", input.getDomProperty("value"));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package io.github.giulong.spectrum.utils;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import static io.github.giulong.spectrum.extensions.resolvers.DriverResolver.DRIVER;
import static java.time.Duration.ZERO;

@Slf4j
public class TestContext {

private final Map<String, Object> store = new ConcurrentHashMap<>();
Expand All @@ -30,6 +38,26 @@ public void addSecuredWebElement(final WebElement webElement) {
}

public boolean isSecuredWebElement(final WebElement webElement) {
return securedWebElements.contains(webElement);
final WebDriver driver = (WebDriver) store.get(DRIVER);
final WebDriver.Timeouts timeouts = driver.manage().timeouts();
final Duration implicitWaitTimeout = timeouts.getImplicitWaitTimeout();

timeouts.implicitlyWait(ZERO);

final boolean secured = securedWebElements
.stream()
.filter(securedWebElement -> {
try {
securedWebElement.getAccessibleName();
return true;
} catch (NoSuchElementException | UnsupportedOperationException ignored) {
log.error("Skipping SecureWebElement not in current page -> {}", securedWebElement);
return false;
}
})
.anyMatch(webElement::equals);

timeouts.implicitlyWait(implicitWaitTimeout);
return secured;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,46 @@
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static io.github.giulong.spectrum.extensions.resolvers.DriverResolver.DRIVER;
import static java.time.Duration.ZERO;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class TestContextTest {

@Mock
private WebDriver driver;

@Mock
private WebDriver.Options options;

@Mock
private WebDriver.Timeouts timeouts;

@Mock
private Duration implicitWaitTimeout;

@Mock
private Map<String, Object> store;

@Mock
private Function<String, String> function;

@Mock
private WebElement webElement;
private WebElement webElement1;

@Mock
private WebElement webElement2;

@InjectMocks
private TestContext testContext;
Expand Down Expand Up @@ -70,22 +90,40 @@ void computeIfAbsent() {
@Test
@DisplayName("addSecuredWebElement should add the provided webElement to the internal list")
void addSecuredWebElement() {
testContext.addSecuredWebElement(webElement);
testContext.addSecuredWebElement(webElement1);

assertEquals(List.of(webElement), Reflections.getFieldValue("securedWebElements", testContext));
assertEquals(List.of(webElement1), Reflections.getFieldValue("securedWebElements", testContext));
}

@SuppressWarnings("DataFlowIssue")
@Test
@DisplayName("isSecuredWebElement should return true if the provided webElement is a secured one")
@DisplayName("isSecuredWebElement should return true if the provided webElement is a secured one, skipping secured web elements that are not in the currently displayed page")
void isSecuredWebElement() {
Reflections.setField("securedWebElements", testContext, List.of(webElement));
Reflections.setField("securedWebElements", testContext, List.of(webElement1, webElement2));

when(store.get(DRIVER)).thenReturn(driver);
when(driver.manage()).thenReturn(options);
when(options.timeouts()).thenReturn(timeouts);
when(timeouts.getImplicitWaitTimeout()).thenReturn(implicitWaitTimeout);
when(timeouts.implicitlyWait(ZERO)).thenReturn(timeouts);

assertTrue(testContext.isSecuredWebElement(webElement));
when(webElement1.getAccessibleName()).thenThrow(NoSuchElementException.class);
when(webElement2.getAccessibleName()).thenReturn("");

assertTrue(testContext.isSecuredWebElement(webElement2));

verify(timeouts).implicitlyWait(implicitWaitTimeout);
}

@Test
@DisplayName("isSecuredWebElement should return false if the provided webElement is not a secured one")
void isSecuredWebElementFalse() {
assertFalse(testContext.isSecuredWebElement(webElement));
when(store.get(DRIVER)).thenReturn(driver);
when(driver.manage()).thenReturn(options);
when(options.timeouts()).thenReturn(timeouts);
when(timeouts.getImplicitWaitTimeout()).thenReturn(implicitWaitTimeout);
when(timeouts.implicitlyWait(ZERO)).thenReturn(timeouts);

assertFalse(testContext.isSecuredWebElement(webElement1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private void commonChecksFor(final String url) {

driver.get(url);

assertEquals(20, extentReportPage.getTestViewTests().size(), "Total tests");
assertEquals(17, countTestsWithStatus("pass"), "Passed tests");
assertEquals(21, extentReportPage.getTestViewTests().size(), "Total tests");
assertEquals(18, countTestsWithStatus("pass"), "Passed tests");
assertEquals(1, countTestsWithStatus("skip"), "Skipped tests");
assertEquals(2, countTestsWithStatus("fail"), "Failed tests");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testSteps() throws IOException {
.map(Path::toFile)
.peek(f -> log.info("Found '{}'", f))
.toList();
assertEquals(20, files.size());
assertEquals(21, files.size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ItVerifierTest {
private static final FailsafeReportsVerifier FAILSAFE_REPORTS_VERIFIER = FailsafeReportsVerifier.getInstance();
private static final Path BASE_DIR = Path.of(System.getProperty("user.dir")).getParent();

private static final int COMPLETED = 21;
private static final int COMPLETED = 22;
private static final int ERRORS = 2;
private static final int FAILURES = 0;
private static final int SKIPPED = 1;
Expand Down Expand Up @@ -48,5 +48,8 @@ public void logFile() throws FileNotFoundException {

// we indirectly check that the slack handler tried to consume the event with a regex in the reason
assertTrue(logFile.contains("SlackConsumer is consuming Event(primaryId=primaryId, secondaryId=null, tags=null, reason=secondReason, result=null)"));

// we check values sent to @Secured web elements are masked
assertTrue(logFile.contains("Sending keys [***] to id: password"));
}
}

0 comments on commit f49f376

Please sign in to comment.