BaseTest.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.provectus.kafka.ui.base;
  2. import com.codeborne.selenide.Condition;
  3. import com.codeborne.selenide.Selenide;
  4. import com.codeborne.selenide.SelenideElement;
  5. import com.codeborne.selenide.WebDriverRunner;
  6. import com.provectus.kafka.ui.utilities.qaseIoUtils.DisplayNameGenerator;
  7. import com.provectus.kafka.ui.utilities.qaseIoUtils.TestCaseGenerator;
  8. import io.github.cdimascio.dotenv.Dotenv;
  9. import io.qameta.allure.Allure;
  10. import io.qase.api.annotation.Step;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.io.FileUtils;
  13. import org.assertj.core.api.SoftAssertions;
  14. import org.junit.jupiter.api.*;
  15. import org.openqa.selenium.Dimension;
  16. import org.openqa.selenium.OutputType;
  17. import org.openqa.selenium.TakesScreenshot;
  18. import org.openqa.selenium.chrome.ChromeOptions;
  19. import org.openqa.selenium.remote.RemoteWebDriver;
  20. import org.testcontainers.Testcontainers;
  21. import org.testcontainers.containers.BrowserWebDriverContainer;
  22. import org.testcontainers.containers.output.Slf4jLogConsumer;
  23. import org.testcontainers.utility.DockerImageName;
  24. import java.io.ByteArrayInputStream;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import static com.provectus.kafka.ui.base.Setup.*;
  29. import static com.provectus.kafka.ui.pages.NaviSideBar.SideMenuOption.TOPICS;
  30. import static com.provectus.kafka.ui.settings.Source.BASE_WEB_URL;
  31. @Slf4j
  32. @DisplayNameGeneration(DisplayNameGenerator.class)
  33. public class BaseTest extends Facade {
  34. private static final String SELENIUM_IMAGE_NAME = "selenium/standalone-chrome:103.0";
  35. private static final String SELENIARM_STANDALONE_CHROMIUM = "seleniarm/standalone-chromium:103.0";
  36. protected static BrowserWebDriverContainer<?> webDriverContainer = null;
  37. private static boolean isARM64() {
  38. return System.getProperty("os.arch").equals("aarch64");
  39. }
  40. @BeforeEach
  41. public void setWebDriver() {
  42. RemoteWebDriver remoteWebDriver = webDriverContainer.getWebDriver();
  43. WebDriverRunner.setWebDriver(remoteWebDriver);
  44. remoteWebDriver.manage().window().setSize(new Dimension(1440, 1024));
  45. Selenide.open(BASE_WEB_URL);
  46. naviSideBar.waitUntilScreenReady();
  47. }
  48. @BeforeAll
  49. public static void start() {
  50. DockerImageName image = isARM64()
  51. ? DockerImageName.parse(SELENIARM_STANDALONE_CHROMIUM).asCompatibleSubstituteFor(SELENIUM_IMAGE_NAME)
  52. : DockerImageName.parse(SELENIUM_IMAGE_NAME);
  53. log.info("Using [{}] as image name for chrome", image.getUnversionedPart());
  54. webDriverContainer = new BrowserWebDriverContainer<>(image)
  55. .withEnv("JAVA_OPTS", "-Dwebdriver.chrome.whitelistedIps=")
  56. .withCapabilities(new ChromeOptions()
  57. .addArguments("--disable-dev-shm-usage")
  58. .addArguments("--disable-gpu")
  59. .addArguments("--no-sandbox")
  60. .addArguments("--verbose")
  61. )
  62. .withLogConsumer(new Slf4jLogConsumer(log).withPrefix("[CHROME]: "));
  63. try {
  64. Testcontainers.exposeHostPorts(8080);
  65. log.info("Starting browser container");
  66. webDriverContainer.start();
  67. } catch (Throwable e) {
  68. log.error("Couldn't start a container", e);
  69. }
  70. }
  71. static {
  72. if (!new File("./.env").exists()) {
  73. try {
  74. FileUtils.copyFile(new File(".env.example"), new File(".env"));
  75. } catch (IOException e) {
  76. log.error("couldn't copy .env.example to .env. Please add .env");
  77. e.printStackTrace();
  78. }
  79. }
  80. Dotenv.load().entries().forEach(env -> System.setProperty(env.getKey(), env.getValue()));
  81. if (Config.CLEAR_REPORTS_DIR) {
  82. clearReports();
  83. }
  84. setup();
  85. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  86. if (TestCaseGenerator.FAILED) {
  87. log.error(
  88. "Tests FAILED because some problem with @CaseId annotation. Verify that all tests annotated with @CaseId and Id is correct!");
  89. Runtime.getRuntime().halt(100500);
  90. }
  91. }));
  92. }
  93. @AfterAll
  94. public static void tearDown() {
  95. if (webDriverContainer.isRunning()) {
  96. webDriverContainer.close();
  97. webDriverContainer.stop();
  98. }
  99. }
  100. @AfterEach
  101. public void afterMethod() {
  102. Allure.addAttachment("Screenshot",
  103. new ByteArrayInputStream(
  104. ((TakesScreenshot) webDriverContainer.getWebDriver()).getScreenshotAs(OutputType.BYTES)));
  105. browserClear();
  106. }
  107. @Step
  108. protected void navigateToTopics(){
  109. naviSideBar
  110. .openSideMenu(TOPICS);
  111. topicsList
  112. .waitUntilScreenReady();
  113. }
  114. @Step
  115. protected void navigateToTopicsAndOpenDetails(String topicName){
  116. naviSideBar
  117. .openSideMenu(TOPICS);
  118. topicsList
  119. .waitUntilScreenReady()
  120. .openTopic(topicName);
  121. topicDetails
  122. .waitUntilScreenReady();
  123. }
  124. @Step
  125. protected void verifyElementsCondition(List<SelenideElement> elementList, Condition expectedCondition) {
  126. SoftAssertions softly = new SoftAssertions();
  127. elementList.forEach(element -> softly.assertThat(element.is(expectedCondition))
  128. .as(element.getSearchCriteria() + " is " + expectedCondition).isTrue());
  129. softly.assertAll();
  130. }
  131. }