Skip to content

Commit 88fff13

Browse files
committed
chore: fix integration tests
Signed-off-by: Stephane Bouchet <[email protected]>
1 parent 3e32f0f commit 88fff13

File tree

9 files changed

+43
-30
lines changed

9 files changed

+43
-30
lines changed

.github/workflows/sonar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
./gradlew build sonar
4747
fi
4848
shell: bash
49-
49+

build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ tasks {
4545
property("sonar.projectKey", "redhat-developer_intellij-common-ui-test-library")
4646
property("sonar.organization", "redhat-developer")
4747
property("sonar.host.url", "https://sonarcloud.io")
48-
property("sonar.sources", "src")
48+
property("sonar.junit.reportsPath", layout.buildDirectory.dir("test-results").get().asFile.absolutePath)
4949
}
5050
}
51-
5251
}
5352

5453
publishing {

src/main/java/com/redhat/devtools/intellij/commonuitest/fixtures/dialogs/FlatWelcomeFrame.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ private void resizeWelcomeWindow(RemoteRobot remoteRobot, int newHeight) {
192192
""", newHeight));
193193
Thread.sleep(5000);
194194
} catch (Exception e) {
195-
LOGGER.log(Level.WARNING, "Failed to resize the Welcome window: " + e.getMessage());
195+
LOGGER.log(Level.WARNING, "Failed to resize the Welcome window: {0}", e.getMessage());
196+
/* Clean up whatever needs to be handled before interrupting */
197+
Thread.currentThread().interrupt();
196198
}
197199
}
198200

src/main/java/com/redhat/devtools/intellij/commonuitest/fixtures/dialogs/project/pages/NewProjectFirstPage.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
import com.intellij.remoterobot.utils.WaitForConditionTimeoutException;
2626
import com.redhat.devtools.intellij.commonuitest.UITestRunner;
2727
import com.redhat.devtools.intellij.commonuitest.utils.constants.XPathDefinitions;
28+
import com.redhat.devtools.intellij.commonuitest.utils.screenshot.ScreenshotUtils;
2829
import com.redhat.devtools.intellij.commonuitest.utils.texttranformation.TextUtils;
2930
import org.jetbrains.annotations.NotNull;
3031

3132
import java.time.Duration;
33+
import java.util.Arrays;
34+
import java.util.HashMap;
3235
import java.util.List;
33-
import java.util.Optional;
36+
import java.util.Map;
3437

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

135138
ComboBoxFixture projectJdkComboBox = getProjectJdkComboBox();
136139
String currentlySelectedProjectSdk = TextUtils.listOfRemoteTextToString(projectJdkComboBox.findAllText());
137-
if (currentlySelectedProjectSdk.contains(targetSdkName)) {
140+
if (currentlySelectedProjectSdk.startsWith(targetSdkName)) {
138141
return;
139142
}
140143

@@ -154,8 +157,20 @@ public void setProjectSdkIfAvailable(String targetSdkName) {
154157
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);
155158
JPopupMenuFixture projectSdkList = parentFixture.jPopupMenus(byXpath(XPathDefinitions.HEAVY_WEIGHT_WINDOW)).get(0); // issue https://github.com/JetBrains/intellij-ui-test-robot/issues/104
156159
List<String> sdkItems = projectSdkList.jList().collectItems();
157-
Optional<String> item = sdkItems.stream().filter(s -> s.startsWith(targetSdkName)).findFirst();
158-
item.ifPresent(s -> projectSdkList.jList().clickItem(s, true));
160+
System.out.println("Items" + sdkItems);
161+
Map<String, String> foundItems = new HashMap<>();
162+
sdkItems.forEach(item ->
163+
Arrays.stream(item.split(" ")).filter(s ->
164+
s.startsWith(targetSdkName)).findFirst().ifPresent(s -> foundItems.put(s, item))
165+
);
166+
if (!foundItems.isEmpty()) {
167+
String label = foundItems.values().stream().findFirst().get();
168+
System.out.println("About to click on "+label);
169+
projectSdkList.jList().clickItem(label, true);
170+
} else {
171+
ScreenshotUtils.takeScreenshot(remoteRobot);
172+
}
173+
159174
});
160175
}
161176

src/main/java/com/redhat/devtools/intellij/commonuitest/utils/screenshot/ScreenshotUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020
import java.nio.file.Files;
21+
import java.nio.file.Path;
2122
import java.nio.file.Paths;
2223
import java.time.LocalDateTime;
2324
import java.time.format.DateTimeFormatter;
@@ -48,12 +49,13 @@ private ScreenshotUtils() {
4849
public static File takeScreenshot(RemoteRobot remoteRobot, String comment) {
4950
try {
5051
BufferedImage screenshotBufferedImage = remoteRobot.getScreenshot();
51-
boolean doesScreenshotDirExists = Files.exists(Paths.get(SCREENSHOT_LOCATION));
52+
Path path = Paths.get(SCREENSHOT_LOCATION);
53+
boolean doesScreenshotDirExists = Files.exists(path);
5254
if (!doesScreenshotDirExists) {
53-
Files.createDirectory(Paths.get(SCREENSHOT_LOCATION));
55+
Files.createDirectory(path);
5456
}
55-
String screenshotFilename = getTimeNow("yyyy_MM_dd_HH_mm_ss");
56-
String screenshotComment = comment == null || comment.equals("") ? "" : "_" + comment;
57+
String screenshotFilename = getTimeNow();
58+
String screenshotComment = comment == null || comment.isEmpty() ? "" : "_" + comment;
5759
String screenshotPathname = SCREENSHOT_LOCATION + screenshotFilename + screenshotComment + "." + FILETYPE;
5860
File screenshotFile = new File(screenshotPathname);
5961
ImageIO.write(screenshotBufferedImage, FILETYPE, screenshotFile);
@@ -74,8 +76,8 @@ public static File takeScreenshot(RemoteRobot remoteRobot) {
7476
return takeScreenshot(remoteRobot, "");
7577
}
7678

77-
private static String getTimeNow(String timeFormat) {
78-
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timeFormat);
79+
private static String getTimeNow() {
80+
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss");
7981
LocalDateTime localTimeNow = LocalDateTime.now();
8082
return dateTimeFormatter.format(localTimeNow);
8183
}

src/main/java/com/redhat/devtools/intellij/commonuitest/utils/testextension/ScreenshotAfterTestFailExtension.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,32 @@
3636
3737
*/
3838
public class ScreenshotAfterTestFailExtension implements AfterTestExecutionCallback {
39-
private final RemoteRobot remoteRobot;
4039
protected static final Logger LOGGER = Logger.getLogger(ScreenshotAfterTestFailExtension.class.getName());
4140

42-
public ScreenshotAfterTestFailExtension() {
43-
this.remoteRobot = UITestRunner.getRemoteRobot();
44-
}
45-
4641
/**
4742
* Take screenshot right after a test has failed and perform a cleanup to ensure no dialog or window is opened
4843
*
4944
* @param extensionContext test run data
5045
*/
5146
@Override
5247
public void afterTestExecution(ExtensionContext extensionContext) {
48+
RemoteRobot remoteRobot = UITestRunner.getRemoteRobot();
5349
if (remoteRobot == null) {
54-
LOGGER.log(Level.SEVERE, "Can take a screenshot, remoteRobot is null!");
50+
LOGGER.log(Level.SEVERE, "Can't take a screenshot, remoteRobot is null!");
5551
return;
5652
}
5753
boolean testFailed = extensionContext.getExecutionException().isPresent();
5854
if (testFailed) {
59-
step("Take a screenshot after a test has failed", () ->
60-
ScreenshotUtils.takeScreenshot(remoteRobot)
55+
step("Take a screenshot after a test has failed",
56+
() -> ScreenshotUtils.takeScreenshot(remoteRobot)
6157
);
6258
step("Return to the 'Welcome Frame' dialog",
63-
this::cleanAfterTestFail
59+
() -> cleanAfterTestFail(remoteRobot)
6460
);
6561
}
6662
}
6763

68-
private void cleanAfterTestFail() {
64+
private void cleanAfterTestFail(RemoteRobot remoteRobot) {
6965
// New Project Dialog is visible -> close it
7066
try {
7167
NewProjectDialogWizard newProjectDialogWizard = remoteRobot.find(NewProjectDialogWizard.class, Duration.ofSeconds(10));

src/main/java/com/redhat/devtools/intellij/commonuitest/utils/texttranformation/TextUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.redhat.devtools.intellij.commonuitest.exceptions.UITestException;
1515

1616
import java.util.List;
17-
import java.util.stream.Collectors;
1817

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

4140
return String.join("", listOfStrings);
4241
}

src/test-project/src/test/java/com/redhat/devtools/intellij/commonuitest/fixtures/test/dialogs/project_manipulation/NewProjectDialogTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ public void cancelButtonTest() {
332332
@Test
333333
public void setProjectSdkIfAvailableTest() {
334334
if (ideaVersionInt >= 20242 && remoteRobot.isWin()) {
335-
newProjectFirstPage.setProjectSdkIfAvailable("Download JDK");
335+
newProjectFirstPage.setProjectSdkIfAvailable("Download");
336336
try {
337-
ContainerFixture downloadJdkDialog = remoteRobot.find(ContainerFixture.class, byXpath("//div[@title='Download JDK']"), Duration.ofSeconds(10));
337+
ContainerFixture downloadJdkDialog = remoteRobot.find(ContainerFixture.class, byXpath("//div[@title='Download JDK...']"), Duration.ofSeconds(10));
338338
downloadJdkDialog.find(ActionButtonFixture.class, byXpath(XPathDefinitions.label(ButtonLabels.CANCEL_LABEL)), Duration.ofSeconds(5)).click();
339339
} catch (WaitForConditionTimeoutException e) {
340340
fail("Download JDK button was not pressed and Download JDK dialog was not found");

src/test-project/src/test/java/com/redhat/devtools/intellij/commonuitest/utils/test/screenshot/ScreenshotUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.nio.file.Files;
2323
import java.time.Duration;
2424

25-
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
2626
import static org.junit.jupiter.api.Assertions.fail;
2727

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

0 commit comments

Comments
 (0)