Use reflection to inject the attempt number

This CL is to address the public static field in RepeatableRunner
for caller to get the current attempt number. We tried to have
a JUnit TestRule to achieve the purpose but it ended up with having
a RuleChain in each class where we already have multiple rules and
need to add the retry rule. This is because we have to make sure
the retry rule is the last one to wrap the test statement so that
the actual retry can include the actions defined in other rules.
Having a rule chain is not scalable and confuses engineer so we
gave it up.

Instead, we decided to expand the current RepeatableRunner to
use reflection to inject the attempt number to the test class.
Doing it this way can reduce the burden from the caller and it also
gets rid of the global state from the previous public static field.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=240789045
This commit is contained in:
shicong 2019-03-28 09:34:08 -07:00 committed by jianglai
parent 308d5eb76b
commit c8aa6005f2
9 changed files with 132 additions and 45 deletions

View file

@ -15,10 +15,11 @@
package google.registry.webdriver;
import static com.google.common.io.Resources.getResource;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.text.StringEscapeUtils.escapeEcmaScript;
import com.google.common.base.Preconditions;
import google.registry.webdriver.RepeatableRunner.AttemptNumber;
import java.net.URL;
import java.util.List;
import java.util.Map;
@ -68,6 +69,7 @@ public final class WebDriverRule extends ExternalResource
private static final String GOLDENS_PATH =
getResource(WebDriverRule.class, "goldens/chrome-linux").getFile();
private AttemptNumber attemptNumber;
private WebDriver driver;
private WebDriverPlusScreenDiffer webDriverPlusScreenDiffer;
@ -75,6 +77,11 @@ public final class WebDriverRule extends ExternalResource
// starts. Will be added a user-given imageKey as a suffix, and of course a '.png' at the end.
private String imageNamePrefix = null;
/** Constructs a {@link WebDriverRule} instance. */
public WebDriverRule(AttemptNumber attemptNumber) {
this.attemptNumber = attemptNumber;
}
@Override
public Statement apply(Statement base, Description description) {
if (imageNamePrefix == null) {
@ -163,7 +170,7 @@ public final class WebDriverRule extends ExternalResource
* @param element the element on the page to be compared
*/
public void diffElement(String imageKey, WebElement element) {
webDriverPlusScreenDiffer.diffElement(getUniqueName(imageKey), element);
webDriverPlusScreenDiffer.diffElement(element, getUniqueName(imageKey), attemptNumber.get());
}
/**
@ -192,7 +199,7 @@ public final class WebDriverRule extends ExternalResource
* the format of ClassName_MethodName_<imageKey> will uniquely identify golden image.
*/
public void diffPage(String imageKey) {
webDriverPlusScreenDiffer.diffPage(getUniqueName(imageKey));
webDriverPlusScreenDiffer.diffPage(getUniqueName(imageKey), attemptNumber.get());
}
@Override
@ -291,7 +298,7 @@ public final class WebDriverRule extends ExternalResource
}
private String getUniqueName(String imageKey) {
Preconditions.checkNotNull(imageNamePrefix);
checkNotNull(imageNamePrefix);
return imageNamePrefix + "_" + imageKey;
}
}