java - 使用 Maven 模块和 Intellij 在测试自动化项目中导入 javax API

标签 java maven intellij-idea selenium-webdriver

我正在使用 IntelliJ + Maven 模块进行测试自动化。目前我想实现一个类来使用 selenium webdriver 检查电子邮件。当我将这些导入到我的项目中时:

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

然后编译器说:无法解析符号...

我想我缺少 Maven 项目的一些设置。我是否必须在 pom.xml 中添加任何特殊标签才能导入 java API?谢谢

我的 POM 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jsproject</groupId>
    <artifactId>jsproject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <selenium.version>2.48.2</selenium.version>
        <overwrite.binaries>false</overwrite.binaries>
        <browser>firefox</browser>
        <threads>1</threads>
        <remote>false</remote>
        <seleniumGridURL/>
        <platform/>
        <browserVersion/>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <directory>target</directory>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <!--<testSourceDirectory>src/test/java</testSourceDirectory>-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <mode>development</mode>
                                <url>${project.url}</url>
                                <key>value</key>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>                
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                <overWriteReleases>false</overWriteReleases>
                                <overWriteSnapshots>false</overWriteSnapshots>
                                <overWriteIfNewer>true</overWriteIfNewer>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>                
            </plugins>
        </pluginManagement>
    </build>

</project>

最佳答案

您应该将 javamail 依赖项添加到 pom.xml

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.2</version>
</dependency>

并使用您选择的版本而不是1.5.2

更新:

此依赖项包含 javamail api 和 com.sun.mail.util.MailSSLSocketFactory ,我猜这是另一个构建在 javamail 之上的库。

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.0-b01</version>
</dependency>

所以只要用上面的替换当前的就可以了。

关于java - 使用 Maven 模块和 Intellij 在测试自动化项目中导入 javax API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482805/

相关文章:

java - 使用 Maven 执行主类并保存输出

java - Maven 依赖项缺少 Artifact

java - 如何移动 IntelliJ 文件选项卡?

intellij-idea - Intellij Idea 13 未显示 "Update Resources and classes"选项

java - maven中如何继承其他库的依赖?

java - JUnit 中的测试方法阻止电子邮件发送

java - 设置与列表在java中的字母顺序

java - HttpServletRequest 和 getHeader() : How to handle case insensitive headers properly?

java - 无法动态更改面板

maven - 在 Google Cloud SQL 环境中执行 maven 单元测试