Allure依赖的Maven项目问题

xqk2d5yq  于 2023-06-21  发布在  Maven
关注(0)|答案(1)|浏览(149)
import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import io.qameta.allure.Step;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.ByteArrayInputStream;
import java.util.UUID;

public class ReportAllure {
    private static WebDriver driver;

    @BeforeClass
    public static void setUp() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\MARSHAL\\Desktop\\Framework\\untitled\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    @Step("UI Test")
    public void openLoginPage() throws InterruptedException {
        // Step 1
        driver.get("http://127.0.0.1:8000");

        WebElement element = driver.findElement(By.className("log"));
        element.click();
        WebElement element1 = driver.findElement(By.xpath("//a[contains(text(), 'Sign up')]"));
        element1.click();
        Thread.sleep(1000);

        // Step 2
        String generatedUsername = random();

        WebElement usernameInput = driver.findElement(By.name("username"));
        usernameInput.sendKeys(generatedUsername);

        String generatedPassword = random();
        WebElement passwordInput = driver.findElement(By.name("password1"));
        passwordInput.sendKeys(generatedPassword);

        WebElement passwordConfirm = driver.findElement(By.name("password2"));
        passwordConfirm.sendKeys(generatedPassword);

        WebElement registerButton = driver.findElement(By.cssSelector("input[value='Register']"));
        registerButton.click();
        Thread.sleep(1000);

        // Step 3
        WebElement spanElement = driver.findElement(By.id("un"));
        String actualText = spanElement.getText();
        String expectedText = generatedUsername;
        Assert.assertEquals(actualText, expectedText, "unexpected message");
        Thread.sleep(1000);

        // Step 4
        WebElement hpclick = driver.findElement(By.xpath("//a[contains(text(), 'HP')]"));
        hpclick.click();
        WebElement hpLaptop = driver.findElement(By.xpath("//a[contains(text(), 'Ноутбук HP Pavilion 14-dv0047ua')]"));
        hpLaptop.click();
        Thread.sleep(1000);

        // Step 5
        WebElement comment = driver.findElement(By.cssSelector("input[placeholder='Write your message here..']"));
        comment.sendKeys("Good Product");
        comment.sendKeys(Keys.ENTER);
        Thread.sleep(3000);
    }

    @AfterClass
    public static void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    private String random() {
        return UUID.randomUUID().toString().substring(0, 8).replace("-", "");
    }

    @Attachment("Screenshot")
    public static byte[] takeScreenshot() {
        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    }

    @Attachment("HTML Code")
    public static ByteArrayInputStream attachHtmlCode(String htmlCode) {
        return new ByteArrayInputStream(htmlCode.getBytes());
    }

    @Attachment("Request")
    public static String attachRequest(String request) {
        return request;
    }

    @Attachment("Response")
    public static String attachResponse(String response) {
        return response;
    }

    @Step("Step with Screenshot")
    public static void stepWithScreenshot() {
        takeScreenshot();
    }

    @Step("Step with HTML Code")
    public static void stepWithHtmlCode(String htmlCode) {
        attachHtmlCode(htmlCode);
    }

    @Step("Step with Request and Response")
    public static void stepWithRequestAndResponse(String request, String response) {
        attachRequest(request);
        attachResponse(response);
    }
}                                                                                                  
---pom.xml---
   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>untitled</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Archetype - untitled</name>
  <url>http://maven.apache.org</url>
  <properties>
    <aspectj.version>1.8.10</aspectj.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.12.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>6.1.7.Final</version>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>4.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.13</version>
    </dependency>
    <dependency>
      <groupId>org.xerial</groupId>
      <artifactId>sqlite-jdbc</artifactId>
      <version>3.36.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.8.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>javax.persistence-api</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>io.github.bonigarcia</groupId>
      <artifactId>webdrivermanager</artifactId>
      <version>4.4.3</version>
    </dependency>
    <dependency>
      <groupId>io.qameta.allure</groupId>
      <artifactId>allure-junit5</artifactId>
      <version>2.16.0</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.4.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <testFailureIgnore>false</testFailureIgnore>
          <argLine>
            -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
          </argLine>
          <properties>
            <property>
              <name>listener</name>
              <value>io.qameta.allure.junit4.AllureJunit4</value>
            </property>
          </properties>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

问题是

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

用红色突出显示(无法解决),我不知道是什么原因,因为所有的依赖关系都包含在我的pom.xml中,所以可能是什么问题?

k4aesqcs

k4aesqcs1#

您的包依赖已包含JUnit5依赖。

执行以下操作之一:

通过添加以下包来使用JUnit4依赖项:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

更新代码以使用以下内容:

  • @AfterClass@AfterAll
  • @BeforeClass@BeforeAll
  • Assert.asserEqulasAssertions.assertEquals

使用以下命令更新导入

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

相关问题