【问题标题】:How to run using terminal on IntelliJ Idea using Maven?如何使用 Maven 在 IntelliJ Idea 上使用终端运行?
【发布时间】:2019-05-06 01:50:19
【问题描述】:

我是 IntelliJ IDEA 的新手,我想通过终端线路运行我的测试。

我已经使用“运行”按钮运行了我的测试,并且我的测试全部通过了。现在,我在项目中的要求是使用命令行/终端运行。我真的不知道该怎么做。

我使用 Appium 作为我的移动自动化工具。

这就是我的测试所在的所有位置,C:\Users\ereyne\IdeaProjects\ioappium\src\test\java\io

Tests location

我已经在谷歌上看过,但都是高水平的,谁能提供我一步一步的过程? Test java classes

这是我的 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>io.appium</groupId>
    <artifactId>io.appium</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>

</project>

这是我在 IntelliJ 中的代码:

//package io;//package appium;

import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.MobileElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URL;
import java.net.MalformedURLException;

public class appium {

public AndroidDriver<MobileElement> driver;
public WebDriverWait wait;
DesiredCapabilities dc = new DesiredCapabilities();
int CustomerNumber = 10088214;
int AccessCode = 44445555;
String SignIn = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.Button[3]";
String MakeaPayment = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View[2]/android.view.View[2]/android.widget.Button[4]";

@BeforeMethod
public void setUp() throws MalformedURLException {
    dc.setCapability("deviceName", "Pixel XL API 28");
    dc.setCapability("udid", "ce041714789604830d"); //DeviceId from "adb devices" command //Tab S3-674ce8f9 S8-ce041714789604830d
    dc.setCapability("platformName", "Android");
    dc.setCapability("platformVersion", "9.0");
    dc.setCapability("skipUnlock","true");
    dc.setCapability("appPackage", "com.loans.smartmoney");
    dc.setCapability("appActivity","com.loans.smartmoney.MainActivity");
    dc.setCapability("noReset","true");
    dc.setCapability("automationName", "UiAutomator2");
    driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"),dc);
    wait = new WebDriverWait(driver, 30);
}

@Test
public void testLoginTest() {

    //Login
    wait.until(ExpectedConditions.visibilityOfElementLocated
            (By.xpath(SignIn)));
    driver.findElement(By.xpath(SignIn)).click();

    driver.findElement(By.className("android.view.View")).click();
    driver.findElement(By.className("android.widget.EditText")).sendKeys(String.valueOf(CustomerNumber));
    driver.findElements(By.className("android.widget.EditText")).get(1).click();
    driver.findElements(By.className("android.widget.EditText")).get(1).sendKeys(String.valueOf(AccessCode));
    driver.findElements(By.className("android.widget.Button")).get(1).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated
            (By.xpath(MakeaPayment)));
}

@AfterMethod
public void tearDown() {
    driver.quit();
}
}

我运行的是终端:C:\Users\ereyne\IdeaProjects\ioappium>mvn appium.java

错误:[错误] 未知的生命周期阶段“appium.java”。您必须以 : 或 :[:]: 格式指定有效的生命周期阶段或目标。可用的生命周期阶段是:验证、初始化、生成电子源、流程源、生成资源、流程资源、编译、流程类、生成测试源、流程测试源、生成测试资源、过程测试资源、测试编译、过程测试类、测试、准备包、包、预集成测试、集成测试、集成后测试、验证、安装、部署、预清理、清理、清理后、站点前、站点、站点后、站点部署。 -> [Help 1] [ERROR] [ERROR] 要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。 [错误] 使用 -X 开关重新运行 Maven 以启用完整的调试日志记录。 [ERROR] [ERROR] 有关错误和可能的解决方案的更多信息,请阅读以下文章:

我也尝试在我的终端中运行它:C:\Users\ereyne\IdeaProjects\ioappium\src\test\java\io>javac appium.java

结果是一切都不存在,只是下面的一个示例错误。

appium.java:2: 错误:包 io.appium.java_client 不存在 import io.appium.java_client.MobileBy;

【问题讨论】:

  • 在idea中打开一个shell/终端工具窗口,运行mvn test。您可能需要安装 bash 插件。
  • 嗨@bmargulies,是的,已经看到那个终端了,但我不知道在上面写什么。因为我是手动测试员,所以我不是真正的编码员类型。
  • 如果是maven项目,进入终端(Alt+F12 in intellij)调用mvn test。这是假设您已经添加了测试文件。
  • @Sachin - 添加我的测试文件是什么意思?我尝试了“mvn 测试”,它说它不是被识别为内部或外部命令。我也试过 'java 但没有用。
  • C:\Users\ereyne\IdeaProjects\ioappium\src\test\java\io>java appium 错误:无法找到或加载主类 appium C:\Users\ereyne\IdeaProjects\ioappium\ src\test\java\io>mvn test 'mvn' 不是内部或外部命令、可运行程序或批处理文件。

标签: maven intellij-idea


【解决方案1】:

IntelliJ-IDEA 实际上有一个内置终端,您可以在项目级别运行 maven 命令。只需单击最底栏上的“终端”按钮。如果您没有看到它,请尝试“Alt+F12”。如果您的终端已禁用,请按两次 Shift,键入“终端”并打开它。

从那里,您可以运行 maven 命令。有效的生命周期阶段是:

  • 干净
  • 验证
  • 编译
  • 测试
  • 验证
  • 安装
  • 网站
  • 部署

因此,在该终端中,您已经位于正确的目录中,并且可以运行有效的 maven 命令。例如

  • mvn clean
  • mvn 验证
  • mvn 测试 -e

您将需要 Maven-Compiler-Plugin,以及 Maven-Surefire-Plugin 或 Maven-Failsafe-plugin。对于我自己的个人项目,这是我使用的包含这些插件的 pom.xml。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
        </plugin>
    </plugins>
</build>

“源”和“目标”表示我的项目使用的 Java 版本。这些插件允许我将我的项目作为 maven 项目运行,并在生命周期的不同实例中发生特定的事情。

就我而言,在“验证”阶段,maven 会触发我的插件来记录结果并创建报告。

如果遇到错误

'mvn' 不是内部或外部命令、可运行程序或批处理文件。

这意味着 Maven 未正确添加到您的 PATH 中。这可能有点令人困惑,但this video 帮助了我很多。

如果你想检查你的 maven 安装,打开一个新终端,然后运行以下命令

mvn -v

您应该得到响应(带有您的系统详细信息)

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
Maven home: C:\Program Files\apache-maven-3.6.0\bin\..
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_201\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

编辑:为了更详细地回答您的问题,我使用了一个为 Cucumber 框架设计的 TestRunner 类,而我的程序中根本没有 main 方法。我的测试类如下:

package Runner;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;


@CucumberOptions(
        plugin = {"pretty", "html:target/cucumber-pretty", "json:target/Cucumber.json"},
        features = {"src/test/resources/Features"},
        glue = {"StepDefinitions"},
        tags = {"@smoke"},
        monochrome = true
)
public class AppiumTest extends AbstractTestNGCucumberTests {


}

如您所见,我的 Test 类中没有任何代码,只是指向我构建的特定测试并执行这些测试。在使用 TestNG 作为测试套件的情况下,它看起来会大不相同,并且更容易理解正在测试的内容,但请查看这些资源以确保您了解要做什么。

https://testng.org/doc/documentation-main.html#testng-xml https://howtodoinjava.com/testng/how-to-execute-testng-tests-with-maven-build/

根据您的回复,很有可能您已经构建了一个 TestNG 套件,只需将其绑定到 maven 中即可。

pom.xml 的结构相当简单,因此您可以使用它来帮助您将所需的插件放入您的插件中。我已经为使用 Cucumber 和 Appium 的测试套件包含了我的 pom.xml 的副本,以便您可以查看格式。我强烈建议您保留包含存储库信息的 cmets,以备不时之需。

<?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>groupID</groupId>
    <artifactId>MobileAutomation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>4.6.0</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>Test</projectName>
                            <outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
                            <inputDirectory>${project.build.directory}</inputDirectory>
                            <jsonFiles>
                                <param>Cucumber.json</param>
                            </jsonFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <failIfNoSpecifiedTests>false</failIfNoSpecifiedTests>
                    <failIfNoTests>false</failIfNoTests>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>4.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.0-M1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>
</project>

如您所见,在我的 pom.xml 中,我有一个在 Maven 的“验证”阶段生成测试报告的插件。当我想运行测试时,我会运行

mvn clean verify

这会清除我的任何旧测试结果,并运行 maven“验证”周期。验证将运行我指定的测试,然后根据我的测试结果,maven 构建将成功或失败。

如果您看到 maven 构建失败,这并不意味着您的代码无法执行,这意味着 maven 有一个构建失败的原因。阅读特定的 maven 插件和生命周期阶段,了解导致构建失败的原因,以及 maven 的预期行为。

【讨论】:

  • 嗨@Andrew - 谢谢你的回复。是的,我已经能够安装 Maven 并在我的系统中看到它们。我尝试运行 'mvn clean'、'mvn verify' 和 'mvn test -e' 并且没有错误。现在,如何运行我的测试脚本?我制作的 java 类。如何添加这些插件?我的 POM 看起来像上面:我已经更新了我的问题。请看。
  • 顺便说一句,正如您在我的代码中看到的那样,我没有任何“主要”。这是一个问题吗?因为查找时出错。
  • 更新您的 POM 就像在其中粘贴代码一样简单。格式见maven.apache.org/pom.html。 Maven 代码执行不需要 main 方法,它实际上是在文件名的开头或结尾查找以单词“Test”命名的类。在您的情况下,您将使用 TestNG 风格的测试套件。查看howtodoinjava.com/testng/… 了解如何编写通过 Maven 运行的 TestNG 套件的详细信息。
  • 我已经编辑了我的原始回复,以便更完整地回答您在此评论链中的问题,所以请阅读以上内容
  • 我能知道在没有main的情况下执行java类的代码吗?我无法执行这个,因为我没有任何主类,我只有一个 java 类 - mvn exec:java -Dexec.mainClass="com.example.Main"
猜你喜欢
  • 2017-07-21
  • 2013-05-09
  • 2023-03-18
  • 1970-01-01
  • 2017-04-03
  • 2016-06-19
  • 2014-09-27
  • 2012-10-12
  • 2014-12-10
相关资源
最近更新 更多