Skip to content

Commit

Permalink
chore: fix integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stephane Bouchet <[email protected]>
  • Loading branch information
sbouchet committed Feb 6, 2025
1 parent 3e32f0f commit 88fff13
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
./gradlew build sonar
fi
shell: bash


3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ tasks {
property("sonar.projectKey", "redhat-developer_intellij-common-ui-test-library")
property("sonar.organization", "redhat-developer")
property("sonar.host.url", "https://sonarcloud.io")
property("sonar.sources", "src")
property("sonar.junit.reportsPath", layout.buildDirectory.dir("test-results").get().asFile.absolutePath)
}
}

}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ private void resizeWelcomeWindow(RemoteRobot remoteRobot, int newHeight) {
""", newHeight));
Thread.sleep(5000);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to resize the Welcome window: " + e.getMessage());
LOGGER.log(Level.WARNING, "Failed to resize the Welcome window: {0}", e.getMessage());
/* Clean up whatever needs to be handled before interrupting */
Thread.currentThread().interrupt();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import com.intellij.remoterobot.utils.WaitForConditionTimeoutException;
import com.redhat.devtools.intellij.commonuitest.UITestRunner;
import com.redhat.devtools.intellij.commonuitest.utils.constants.XPathDefinitions;
import com.redhat.devtools.intellij.commonuitest.utils.screenshot.ScreenshotUtils;
import com.redhat.devtools.intellij.commonuitest.utils.texttranformation.TextUtils;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Map;

import static com.intellij.remoterobot.search.locators.Locators.byXpath;
import static com.intellij.remoterobot.stepsProcessing.StepWorkerKt.step;
Expand Down Expand Up @@ -134,7 +137,7 @@ public void setProjectSdkIfAvailable(String targetSdkName) {

ComboBoxFixture projectJdkComboBox = getProjectJdkComboBox();
String currentlySelectedProjectSdk = TextUtils.listOfRemoteTextToString(projectJdkComboBox.findAllText());
if (currentlySelectedProjectSdk.contains(targetSdkName)) {
if (currentlySelectedProjectSdk.startsWith(targetSdkName)) {
return;
}

Expand All @@ -154,8 +157,20 @@ public void setProjectSdkIfAvailable(String targetSdkName) {
CommonContainerFixture parentFixture = waitFor(Duration.ofSeconds(20), Duration.ofSeconds(2), "Wait for the 'Project SDK' list to finish loading all items.", "The project JDK list did not load all items in 20 seconds.", this::didProjectSdkListLoadAllItems);
JPopupMenuFixture projectSdkList = parentFixture.jPopupMenus(byXpath(XPathDefinitions.HEAVY_WEIGHT_WINDOW)).get(0); // issue https://github.com/JetBrains/intellij-ui-test-robot/issues/104
List<String> sdkItems = projectSdkList.jList().collectItems();
Optional<String> item = sdkItems.stream().filter(s -> s.startsWith(targetSdkName)).findFirst();
item.ifPresent(s -> projectSdkList.jList().clickItem(s, true));
System.out.println("Items" + sdkItems);
Map<String, String> foundItems = new HashMap<>();
sdkItems.forEach(item ->
Arrays.stream(item.split(" ")).filter(s ->
s.startsWith(targetSdkName)).findFirst().ifPresent(s -> foundItems.put(s, item))
);
if (!foundItems.isEmpty()) {
String label = foundItems.values().stream().findFirst().get();
System.out.println("About to click on "+label);
projectSdkList.jList().clickItem(label, true);
} else {
ScreenshotUtils.takeScreenshot(remoteRobot);
}

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -48,12 +49,13 @@ private ScreenshotUtils() {
public static File takeScreenshot(RemoteRobot remoteRobot, String comment) {
try {
BufferedImage screenshotBufferedImage = remoteRobot.getScreenshot();
boolean doesScreenshotDirExists = Files.exists(Paths.get(SCREENSHOT_LOCATION));
Path path = Paths.get(SCREENSHOT_LOCATION);
boolean doesScreenshotDirExists = Files.exists(path);
if (!doesScreenshotDirExists) {
Files.createDirectory(Paths.get(SCREENSHOT_LOCATION));
Files.createDirectory(path);
}
String screenshotFilename = getTimeNow("yyyy_MM_dd_HH_mm_ss");
String screenshotComment = comment == null || comment.equals("") ? "" : "_" + comment;
String screenshotFilename = getTimeNow();
String screenshotComment = comment == null || comment.isEmpty() ? "" : "_" + comment;
String screenshotPathname = SCREENSHOT_LOCATION + screenshotFilename + screenshotComment + "." + FILETYPE;
File screenshotFile = new File(screenshotPathname);
ImageIO.write(screenshotBufferedImage, FILETYPE, screenshotFile);
Expand All @@ -74,8 +76,8 @@ public static File takeScreenshot(RemoteRobot remoteRobot) {
return takeScreenshot(remoteRobot, "");
}

private static String getTimeNow(String timeFormat) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timeFormat);
private static String getTimeNow() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss");
LocalDateTime localTimeNow = LocalDateTime.now();
return dateTimeFormatter.format(localTimeNow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,32 @@
* @author [email protected]
*/
public class ScreenshotAfterTestFailExtension implements AfterTestExecutionCallback {
private final RemoteRobot remoteRobot;
protected static final Logger LOGGER = Logger.getLogger(ScreenshotAfterTestFailExtension.class.getName());

public ScreenshotAfterTestFailExtension() {
this.remoteRobot = UITestRunner.getRemoteRobot();
}

/**
* Take screenshot right after a test has failed and perform a cleanup to ensure no dialog or window is opened
*
* @param extensionContext test run data
*/
@Override
public void afterTestExecution(ExtensionContext extensionContext) {
RemoteRobot remoteRobot = UITestRunner.getRemoteRobot();
if (remoteRobot == null) {
LOGGER.log(Level.SEVERE, "Can take a screenshot, remoteRobot is null!");
LOGGER.log(Level.SEVERE, "Can't take a screenshot, remoteRobot is null!");
return;
}
boolean testFailed = extensionContext.getExecutionException().isPresent();
if (testFailed) {
step("Take a screenshot after a test has failed", () ->
ScreenshotUtils.takeScreenshot(remoteRobot)
step("Take a screenshot after a test has failed",
() -> ScreenshotUtils.takeScreenshot(remoteRobot)
);
step("Return to the 'Welcome Frame' dialog",
this::cleanAfterTestFail
() -> cleanAfterTestFail(remoteRobot)
);
}
}

private void cleanAfterTestFail() {
private void cleanAfterTestFail(RemoteRobot remoteRobot) {
// New Project Dialog is visible -> close it
try {
NewProjectDialogWizard newProjectDialogWizard = remoteRobot.find(NewProjectDialogWizard.class, Duration.ofSeconds(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.redhat.devtools.intellij.commonuitest.exceptions.UITestException;

import java.util.List;
import java.util.stream.Collectors;

/**
* Static utilities that assist and simplify data conversion and transformation
Expand All @@ -36,7 +35,7 @@ public static String listOfRemoteTextToString(List<RemoteText> data) {
List<String> listOfStrings = data
.stream()
.map(RemoteText::getText)
.collect(Collectors.toList());
.toList();

return String.join("", listOfStrings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ public void cancelButtonTest() {
@Test
public void setProjectSdkIfAvailableTest() {
if (ideaVersionInt >= 20242 && remoteRobot.isWin()) {
newProjectFirstPage.setProjectSdkIfAvailable("Download JDK");
newProjectFirstPage.setProjectSdkIfAvailable("Download");
try {
ContainerFixture downloadJdkDialog = remoteRobot.find(ContainerFixture.class, byXpath("//div[@title='Download JDK']"), Duration.ofSeconds(10));
ContainerFixture downloadJdkDialog = remoteRobot.find(ContainerFixture.class, byXpath("//div[@title='Download JDK...']"), Duration.ofSeconds(10));
downloadJdkDialog.find(ActionButtonFixture.class, byXpath(XPathDefinitions.label(ButtonLabels.CANCEL_LABEL)), Duration.ofSeconds(5)).click();
} catch (WaitForConditionTimeoutException e) {
fail("Download JDK button was not pressed and Download JDK dialog was not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.nio.file.Files;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand All @@ -38,7 +38,7 @@ public void takeScreenshotTest() {
int numberOfScreenshotBefore = getNumberOfSavedScreenshot();
File screenshotFile = ScreenshotUtils.takeScreenshot(remoteRobot);
int numberOfScreenshotAfter = getNumberOfSavedScreenshot();
assertTrue(numberOfScreenshotAfter == numberOfScreenshotBefore + 1, "Screenshot should be already saved but is not.");
assertEquals(numberOfScreenshotAfter, numberOfScreenshotBefore + 1, "Screenshot should be already saved but is not.");
try {
Files.delete(screenshotFile.toPath());
} catch (IOException e) {
Expand Down

0 comments on commit 88fff13

Please sign in to comment.