Repository: provectus/kafka-ui
Issue: [e2e]TopicTests.clearMessageOfTopic : Clear message of topic
Labels: scope/QA
scope/AQA

Autotest implementation for:

https://app.qase.io/case/KAFKAUI-239
Pre-conditions :
1)Create a Topic
2)Add messages to Topic

Steps:
1)Navigate to Topics
2)Select "Clear messages" from 3dot menu icon for Topic
3)Press "Confirm"

Expected results:
1)"Confirm the action" should appear
2)Number of messages should become 0 ; 
   Success message should be displayed

Issue comments:

Pull request review comments:
why do we need this link to the task?
getNumberOfMessage -> getNumberOfMessages
assertions don't work, because assertAll() missed
btw, I'm not sure we really need this method. we can call getTopicItem(topicName).getNumberOfMessages() directly from test
Fixed
Fixed.
Removed.
we need to wait until button is visible while dot menu is opening: clickByJavaScript(clearMessageBtn); -> clickByJavaScript(clearMessageBtn.shouldBe(visible));
Fixed.

Patch that resolved the issue:
diff --git a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/BasePage.java b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/BasePage.java
index cf559af1e2c..67bf5a7c590 100644
--- a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/BasePage.java
+++ b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/BasePage.java
@@ -18,6 +18,7 @@ public abstract class BasePage extends WebUtils {
   protected SelenideElement dotMenuBtn = $x("//button[@aria-label='Dropdown Toggle']");
   protected SelenideElement alertHeader = $x("//div[@role='alert']//div[@role='heading']");
   protected SelenideElement alertMessage = $x("//div[@role='alert']//div[@role='contentinfo']");
+  protected SelenideElement confirmBtn = $x("//button[contains(text(),'Confirm')]");
   protected ElementsCollection allGridItems = $$x("//tr[@class]");
   protected String summaryCellLocator = "//div[contains(text(),'%s')]";
   protected String tableElementNameLocator = "//tbody//a[contains(text(),'%s')]";
@@ -65,6 +66,11 @@ protected boolean isAlertVisible(AlertHeader header, String message) {
     return result;
   }
 
+  protected void clickConfirmButton() {
+    confirmBtn.shouldBe(Condition.enabled).click();
+    confirmBtn.shouldBe(Condition.disappear);
+  }
+
   public enum AlertHeader {
     SUCCESS("Success"),
     VALIDATION_ERROR("Validation Error"),
diff --git a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicDetails.java b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicDetails.java
index 70d5934d048..bd8cbd3d02c 100644
--- a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicDetails.java
+++ b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicDetails.java
@@ -36,7 +36,6 @@ public class TopicDetails extends BasePage {
   protected SelenideElement selectFilterBtnAddFilterMdl = $x("//button[text()='Select filter']");
   protected SelenideElement editSettingsMenu = $x("//li[@role][contains(text(),'Edit settings')]");
   protected SelenideElement removeTopicBtn = $x("//ul[@role='menu']//div[contains(text(),'Remove Topic')]");
-  protected SelenideElement confirmBtn = $x("//div[@role='dialog']//button[contains(text(),'Confirm')]");
   protected SelenideElement produceMessageBtn = $x("//div//button[text()='Produce Message']");
   protected SelenideElement contentMessageTab = $x("//html//div[@id='root']/div/main//table//p");
   protected SelenideElement cleanUpPolicyField = $x("//div[contains(text(),'Clean Up Policy')]/../span/*");
@@ -77,7 +76,7 @@ public TopicDetails openDotMenu() {
   }
 
   @Step
-  public boolean isAlertWithMessageVisible(AlertHeader header, String message) {
+  public boolean isAlertWithMessageVisible(AlertHeader header, String message){
     return isAlertVisible(header, message);
   }
 
@@ -127,8 +126,7 @@ public TopicDetails clickDeleteTopicMenu() {
 
   @Step
   public TopicDetails clickConfirmBtnMdl() {
-    confirmBtn.shouldBe(Condition.enabled).click();
-    confirmBtn.shouldBe(Condition.disappear);
+    clickConfirmButton();
     return this;
   }
 
diff --git a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicsList.java b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicsList.java
index 1a28050369d..73e2610b7e9 100644
--- a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicsList.java
+++ b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topic/TopicsList.java
@@ -1,5 +1,6 @@
 package com.provectus.kafka.ui.pages.topic;
 
+import static com.codeborne.selenide.Condition.visible;
 import static com.codeborne.selenide.Selenide.$x;
 
 import com.codeborne.selenide.CollectionCondition;
@@ -22,11 +23,12 @@ public class TopicsList extends BasePage {
     protected SelenideElement deleteSelectedTopicsBtn = $x("//button[text()='Delete selected topics']");
     protected SelenideElement copySelectedTopicBtn = $x("//button[text()='Copy selected topic']");
     protected SelenideElement purgeMessagesOfSelectedTopicsBtn = $x("//button[text()='Purge messages of selected topics']");
+    protected SelenideElement clearMessagesBtn = $x("//ul[contains(@class ,'open')]//div[text()='Clear Messages']");
 
     @Step
     public TopicsList waitUntilScreenReady() {
         waitUntilSpinnerDisappear();
-        topicListHeader.shouldBe(Condition.visible);
+        topicListHeader.shouldBe(visible);
         return this;
     }
 
@@ -38,7 +40,7 @@ public TopicsList clickAddTopicBtn() {
 
     @Step
     public boolean isTopicVisible(String topicName) {
-        tableGrid.shouldBe(Condition.visible);
+        tableGrid.shouldBe(visible);
         return isVisible(getTableElement(topicName));
     }
 
@@ -59,6 +61,12 @@ public TopicsList openTopic(String topicName) {
         return this;
     }
 
+    @Step
+    public TopicsList openDotMenuByTopicName(String topicName){
+      getTopicItem(topicName).openDotMenu();
+      return this;
+    }
+
     @Step
     public boolean isCopySelectedTopicBtnEnabled(){
       return isEnabled(copySelectedTopicBtn);
@@ -76,6 +84,23 @@ public TopicsList clickCopySelectedTopicBtn(){
       return this;
     }
 
+    @Step
+    public TopicsList clickClearMessagesBtn(){
+      clickByJavaScript(clearMessagesBtn.shouldBe(visible));
+      return this;
+    }
+
+    @Step
+    public TopicsList clickConfirmBtnMdl() {
+    clickConfirmButton();
+    return this;
+    }
+
+    @Step
+    public boolean isAlertWithMessageVisible(AlertHeader header, String message) {
+      return isAlertVisible(header, message);
+    }
+
     private List<SelenideElement> getVisibleColumnHeaders() {
       return Stream.of("Replication Factor","Number of messages","Topic Name", "Partitions", "Out of sync replicas", "Size")
           .map(name -> $x(String.format(columnHeaderLocator, name)))
@@ -110,12 +135,12 @@ private List<TopicGridItem> initGridItems() {
       return gridItemList;
     }
 
-  @Step
-  public TopicGridItem getTopicItem(String name) {
-    return initGridItems().stream()
+    @Step
+    public TopicGridItem getTopicItem(String name) {
+      return initGridItems().stream()
         .filter(e -> e.getName().equals(name))
         .findFirst().orElse(null);
-  }
+    }
 
     @Step
     public List<TopicGridItem> getNonInternalTopics() {
@@ -149,7 +174,7 @@ public TopicsList selectItem(boolean select) {
       public boolean isInternal() {
         boolean internal = false;
         try {
-          element.$x("./td[2]/a/span").shouldBe(Condition.visible);
+          element.$x("./td[2]/a/span").shouldBe(visible);
           internal = true;
         } catch (Throwable ignored) {
         }


Test patch:
diff --git a/kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/suite/topics/TopicMessagesTests.java b/kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/suite/topics/TopicMessagesTests.java
index f18150f8347..22b3b6e97b8 100644
--- a/kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/suite/topics/TopicMessagesTests.java
+++ b/kafka-ui-e2e-checks/src/test/java/com/provectus/kafka/ui/suite/topics/TopicMessagesTests.java
@@ -5,6 +5,7 @@
 import static com.provectus.kafka.ui.settings.BaseSource.CLUSTER_NAME;
 import static com.provectus.kafka.ui.utilities.FileUtils.fileToString;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import com.provectus.kafka.ui.base.BaseTest;
 import com.provectus.kafka.ui.models.Topic;
@@ -33,11 +34,15 @@ public class TopicMessagesTests extends BaseTest {
       .setName("topic-with-clean-message-attribute-" + randomAlphabetic(5))
       .setMessageKey(fileToString(System.getProperty("user.dir") + "/src/test/resources/producedkey.txt"))
       .setMessageContent(fileToString(System.getProperty("user.dir") + "/src/test/resources/testData.txt"));
+  private static final Topic TOPIC_TO_CLEAR_MESSAGES = new Topic()
+      .setName("topic-to-clear-message-attribute-" + randomAlphabetic(5))
+      .setMessageKey(fileToString(System.getProperty("user.dir") + "/src/test/resources/producedkey.txt"))
+      .setMessageContent(fileToString(System.getProperty("user.dir") + "/src/test/resources/testData.txt"));
   private static final List<Topic> TOPIC_LIST = new ArrayList<>();
 
   @BeforeAll
   public void beforeAll() {
-    TOPIC_LIST.addAll(List.of(TOPIC_FOR_MESSAGES));
+    TOPIC_LIST.addAll(List.of(TOPIC_FOR_MESSAGES, TOPIC_TO_CLEAR_MESSAGES));
     TOPIC_LIST.forEach(topic -> apiService.createTopic(CLUSTER_NAME, topic.getName()));
   }
 
@@ -108,7 +113,7 @@ void copyMessageFromTopicProfile() {
         .getRandomMessage()
         .openDotMenu()
         .clickCopyToClipBoard();
-    Assertions.assertTrue(topicDetails.isAlertWithMessageVisible(SUCCESS, "Copied successfully!"),
+    Assertions.assertTrue(topicDetails.isAlertWithMessageVisible(SUCCESS,"Copied successfully!"),
         "isAlertWithMessageVisible()");
   }
 
@@ -134,6 +139,41 @@ void checkingMessageFilteringByOffset() {
     softly.assertAll();
   }
 
+  @DisplayName("TopicTests.clearMessageOfTopic : Clear message of topic")
+  @Suite(suiteId = SUITE_ID, title = SUITE_TITLE)
+  @AutomationStatus(status = Status.AUTOMATED)
+  @CaseId(239)
+  @Test
+  void checkClearTopicMessage() {
+    navigateToTopicsAndOpenDetails(TOPIC_TO_CLEAR_MESSAGES.getName());
+    topicDetails
+        .openDetailsTab(TopicDetails.TopicMenu.OVERVIEW)
+        .clickProduceMessageBtn();
+    produceMessagePanel
+        .waitUntilScreenReady()
+        .setContentFiled(TOPIC_TO_CLEAR_MESSAGES.getMessageContent())
+        .setKeyField(TOPIC_TO_CLEAR_MESSAGES.getMessageKey())
+        .submitProduceMessage();
+    topicDetails
+        .waitUntilScreenReady();
+    navigateToTopics();
+    topicsList
+        .waitUntilScreenReady();
+    assertThat(topicsList.getTopicItem(TOPIC_TO_CLEAR_MESSAGES.getName()).getNumberOfMessages())
+        .as("getNumberOfMessages()").isEqualTo(1);
+    topicsList
+        .openDotMenuByTopicName(TOPIC_TO_CLEAR_MESSAGES.getName())
+        .clickClearMessagesBtn()
+        .clickConfirmBtnMdl();
+    SoftAssertions softly = new SoftAssertions();
+    softly.assertThat(topicsList.isAlertWithMessageVisible(SUCCESS,
+        String.format("%s messages have been successfully cleared!",TOPIC_TO_CLEAR_MESSAGES.getName())))
+        .as("isAlertWithMessageVisible()").isTrue();
+    softly.assertThat(topicsList.getTopicItem(TOPIC_TO_CLEAR_MESSAGES.getName()).getNumberOfMessages())
+        .as("getNumberOfMessages()").isEqualTo(0);
+    softly.assertAll();
+  }
+
   @AfterAll
   public void afterAll() {
     TOPIC_LIST.forEach(topic -> apiService.deleteTopic(CLUSTER_NAME, topic.getName()));