Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/test/java/aquality/selenium/utils/ElementActionRetrierTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package aquality.selenium.utils;

import aquality.selenium.configuration.Configuration;
import aquality.selenium.logger.Logger;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.InvalidElementStateException;
import org.openqa.selenium.StaleElementReferenceException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.testng.Assert.*;

public class ElementActionRetrierTests {

private static final int attemptsCount = Configuration.getInstance().getRetryConfiguration().getNumber();
private static final long pollingInterval = Configuration.getInstance().getRetryConfiguration().getPollingInterval();

@DataProvider
private Object[][] handledExceptions() {
return new Object[][] {
{ new StaleElementReferenceException("") },
{ new InvalidElementStateException("")}
};
}

@Test
public void testRetrierShouldWorkOnceIfMethodSucceeded() {
Date startTime = new Date();
ElementActionRetrier.doWithRetry(() -> Logger.getInstance().info("text message from successful method"));
assertTrue(new Date().getTime() - startTime.getTime() < pollingInterval);
}

@Test(dataProvider = "handledExceptions")
public void testRetrierShouldWaitPollingTimeBetweenMethodsCall(RuntimeException handledException) {
Date startTime = new Date();
AtomicBoolean isThrowException = new AtomicBoolean(true);
ElementActionRetrier.doWithRetry(() -> {
if (isThrowException.get()) {
isThrowException.set(false);
throw handledException;
}
});
long duration = new Date().getTime() - startTime.getTime();
assertTrue(duration >= pollingInterval && duration < 2 * pollingInterval);
}

@Test(expectedExceptions = InvalidArgumentException.class)
public void testRetrierShouldThrowUnhandledException() {
ElementActionRetrier.doWithRetry(() -> {
throw new InvalidArgumentException("");
});
}

@Test(dataProvider = "handledExceptions")
public void testRetrierShouldWorkCorrectTimes(RuntimeException handledException) {
Date startTime = new Date();
try {
ElementActionRetrier.doWithRetry(() -> {
throw handledException;
});
} catch (RuntimeException e) {
assertTrue(handledException.getClass().isInstance(e));
}
long duration = new Date().getTime() - startTime.getTime();
assertTrue(duration >= pollingInterval * attemptsCount && duration < pollingInterval * (attemptsCount + 1));
}

@Test(expectedExceptions = IllegalAccessException.class)
public void testShouldNotBePossibleInstantiateRetrier() throws IllegalAccessException, InstantiationException {
ElementActionRetrier.class.newInstance();
}

@Test(expectedExceptions = InvocationTargetException.class)
public void testShouldNotBePossibleInstantiateRetrierEvenIfUsingReflection() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Constructor<ElementActionRetrier> constructor = ElementActionRetrier.class.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
}

@Test
public void testRetrierShouldReturnValue() {
Object obj = new Object();
assertEquals(ElementActionRetrier.doWithRetry(() -> obj), obj);
}

@Test(dataProvider = "handledExceptions", timeOut = 10000)
public void testRetrierShouldNotThrowExceptionOnInterruption(RuntimeException handledException) throws InterruptedException {
AtomicBoolean isRetrierPaused = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
ElementActionRetrier.doWithRetry(() -> {
isRetrierPaused.set(true);
throw handledException;
});
}
});
thread.start();
while (!isRetrierPaused.get()) {
Thread.sleep(pollingInterval / 10);
}
Thread.sleep(pollingInterval / 3);
thread.interrupt();
}

}
1 change: 1 addition & 0 deletions src/test/resources/TestSuite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<class name="tests.integration.ElementTests"/>
<class name="tests.integration.ElementStateTests"/>
<class name="tests.integration.HiddenElementsTests"/>
<class name="aquality.selenium.utils.ElementActionRetrierTests"/>
</classes>
</test>
<test name="Use cases tests" parallel="methods" thread-count="10">
Expand Down