如何使用Maven在IntelliJ Idea上的终端上运行?

时间:2019-05-06 01:50:20

标签: maven intellij-idea

我是IntelliJ IDEA的新手,我想通过端子线进行测试。

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

我将Appium用作移动自动化工具。

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

Tests location

我已经看过Google,但是所有人都处于高水平,有人可以向我逐步介绍如何做吗? 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

错误:[ERROR]未知的生命周期阶段“ appium.java”。您必须以:或:[:]:格式指定有效的生命周期阶段或目标。可用的生命周期阶段包括:验证,初始化,生成电子源,流程源,生成资源,流程资源,编译,流程类,生成测试源,流程测试源,生成测试资源,过程测试资源,测试编译,过程测试类,测试,准备打包,打包,集成前测试,集成测试,集成后测试,验证,安装,部署,预清理,清理,清理后,站点前,站点,站点后,站点部署。 -> [帮助1] [错误] [错误]要查看错误的完整堆栈跟踪,请使用-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;

1 个答案:

答案 0 :(得分:0)

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

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

  • 干净
  • 验证
  • 编译
  • 测试
  • 包裹
  • 验证
  • 安装
  • 站点
  • 部署

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

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

您将需要Maven-Compiler-Plugin,以及Maven-Surefire-Plugin或Maven-Failsafe插件。对于我自己的个人项目,这是我使用的包含这些插件的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方法。我的Test类如下:

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的副本,以方便您查看格式。我强烈建议您保留注释,包括存储库信息,以备需要快速更新时使用。

<?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期望发生的行为。