当前位置: 首页 > 知识库问答 >
问题:

我无法从命令提示符执行testNG测试套件

丁鸿信
2023-03-14

我已经在maven中创建了以下项目。

package com.crm.qa.testBase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.crm.qa.utility.TestUtility;
import com.crm.qa.utility.WebEventListener;

public class TestBase {

    public static WebDriver driver;
    public static Properties prop;
    public static EventFiringWebDriver e_driver;
    public static WebEventListener eventListener;

    public TestBase() {
        try {

            prop = new Properties();
            FileInputStream fp = new FileInputStream(
                    "C:\\Users\\Shilpa Khandge\\eclipse-workspace\\FreeCRM\\src\\main\\java\\com\\crm\\qa\\config\\conf.properties");
            prop.load(fp);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void initialization() {
        String browsername = prop.getProperty("browser");
        String urlnm = prop.getProperty("url");
        if (browsername.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver",
                    "C:\\Users\\Shilpa Khandge\\Downloads\\chromedriver_win32 (3)\\chromedriver.exe");
            driver = new ChromeDriver();
        }

        else if (browsername.equals("FF")) {
            System.setProperty("webdriver.gecko.driver",
                    "C:\\Users\\Shilpa Khandge\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");

            driver = new FirefoxDriver();

        }

        e_driver = new EventFiringWebDriver(driver);
        eventListener = new WebEventListener();
        e_driver.register(eventListener);
        driver=e_driver;

        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(TestUtility.pageloadTime, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(TestUtility.implicitTime, TimeUnit.SECONDS);
        driver.get(urlnm);
    }

}

我使用如下页面对象模型:

package com.crm.qa.pages;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.crm.qa.testBase.TestBase;

import io.qameta.allure.Step;

public class LoginPage extends TestBase{

    //pageFactory or Object repository
    @FindBy(name="username") WebElement emailaddress;
    @FindBy(name="password")WebElement password;
    @FindBy(xpath="//input[@type='submit']")WebElement loginbutton;
    @FindBy(linkText="Sign Up")WebElement signuplink;


    //initializing OR:
    public LoginPage()
    {
        PageFactory.initElements(driver, this);
    }

    //Actions or methods:
    @Step("Verifying  login title on login page step.....")
        public String validatelogintitle() {
        return driver.getTitle();
    }
        @Step("Verifying  signup link on login page step......")
    public boolean validatesignuplink() {
        return signuplink.isDisplayed();
    }


    @Step("Verifying  login functionality with username: {0} and password: {1} step......")
    public HomePage validateloginfunctionality(String email,String pwd) throws InterruptedException
    {


        emailaddress.sendKeys(email);
        password.sendKeys(pwd);
        //loginbutton.click();
        JavascriptExecutor js=(JavascriptExecutor)driver;
        js.executeScript("arguments[0].click()", loginbutton);
        Thread.sleep(3000);


        return new HomePage();

    }
}

我的测试是用 testNG 编写的,如下所示:

package com.crm.qa.testcases;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.crm.qa.pages.HomePage;
import com.crm.qa.pages.LoginPage;
import com.crm.qa.testBase.TestBase;

import io.qameta.allure.Description;
import io.qameta.allure.Severity;
import io.qameta.allure.SeverityLevel;
import io.qameta.allure.Story;

public class TestLoginpage extends TestBase {


    LoginPage loginpage;
    HomePage homepage;
    public TestLoginpage() {
        super();
    }

    @BeforeMethod
    public void setUp() {
        initialization();
        loginpage=new LoginPage();
    }


    @Test(priority=3,description="Verify login page title test")
    @Severity(SeverityLevel.NORMAL)
    @Description("Test Case Description : Verify login page title on login page")
    @Story("Story Name: To check login title on login page")
    public void loginpagetitleTest()
    {
        String title=loginpage.validatelogintitle();
        assertEquals(title, "CRMPRO - CRM software for customer relationship management, sales, and support.");
    }

    @Test(priority=2,description="Verifying signup link test")
    @Severity(SeverityLevel.CRITICAL)
    @Description("Test Case Description : Verify signup link on login page")
    @Story("Story Name: To check signup link on login page")
    public void validatesignuplinkTest() {
        boolean flag=loginpage.validatesignuplink();
        assertEquals(flag, true);
    }

    @Test(priority=1,description="Verifying login functionality test")
    @Severity(SeverityLevel.BLOCKER)
    @Description("Test Case Description : Verify login functionality on login page")
    @Story("Story Name: To check login functionality on login page")
    public void validateloginfunctionalityTest() throws InterruptedException {
        String uname=prop.getProperty("username");
        String password=prop.getProperty("password");
        homepage=loginpage.validateloginfunctionality(uname,password);
    }
    @AfterMethod
    public void tearDown()
    {
        driver.quit();
    }
}

我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>AllureReport</groupId>
  <artifactId>AllureReport</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>AllureReport</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <aspectj.version>1.8.10</aspectj.version>

  </properties>


  <build>
        <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-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testrunner/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                 <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>

            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>



    <dependencies>

 <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>openxml4j</artifactId>
            <version>1.0-beta</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.12.1</version>
        </dependency>



  </dependencies>
</project>

还有测试。xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="FreeCRM Applicatin automation testsuite">
  <test thread-count="5" name="FreeCRM app test cases">
    <classes>
<!--       <class name="com.crm.qa.testcases.TestHomepage"/> -->
<!--       <class name="com.crm.qa.testcases.TestContactspage"/> -->
      <class name="com.crm.qa.testcases.TestLoginpage"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

我尝试在命令行中通过maven使用命令“mvn clean test”执行测试

我在执行上述命令时收到以下错误:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.208 s
[INFO] Finished at: 2020-01-16T15:21:44+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project AllureReport: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\Users\Shilpa Khandge\eclipse-workspace\AllureReport\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] There was an error in the forked process
[ERROR] Suite file C:\Users\Shilpa Khandge\eclipse-workspace\AllureReport\src\test\resources\testrunner\testng.xml is not a valid file
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] Suite file C:\Users\Shilpa Khandge\eclipse-workspace\AllureReport\src\test\resources\testrunner\testng.xml is not a valid file
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
[ERROR]     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)
[ERROR]     at org.apache.maven.cli.MavenCli.main(MavenCli.java:194)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at java.lang.reflect.Method.invoke(Unknown Source)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

共有1个答案

方和宜
2023-03-14

将pom中TestNg的范围从compile更改为test。xml文件测试或提供

 类似资料:
  • 当我尝试从项目从命令提示符运行项目时,我有主菜单测试用例已经失败的异常。 java-cp c:\users\eclipse-workspac\ctv_web_auto_check\bin;c:\users\eclipse-workspace\ctv_web_auto_check\lib*org.testng.testng testng.xml

  • 但是,这将生成以下错误消息: 我查看了testNG文档(http://testNG.org/doc/documentation-main.html),它只是说testNG必须在您的类路径中,所以现在我有点困惑。任何帮助都将不胜感激。

  • 问题内容: 如何从命令行运行Junit 4.8.1测试套件?另外,我想使用JUnit 4.8引入的类别,有没有一种方法可以从命令行指定要运行的类别。 问题答案: 从4.8开始,无法从命令行指定类别。

  • POM依赖项 测试套件。xml

  • 好吧,我是新来的Java,并试图从命令提示符运行一个java程序(因为Sublime编译和运行它的小东西,但不允许用户输入和东西)。 我将命令提示符设置为我有我的文件的文件夹,称为Learner.java. 我在环境变量中为JDK bin设置了一个路径,并创建了一个JAVA_HOME变量,其中包含JDK bin的值(其他一些帖子建议)。 在我讨论这个问题之前,这是我的代码: 如你所见,这是一个以“

  • 我有一个使用testng API动态创建testng.xml文件的项目,因此使用java的main函数,我的项目按预期工作,但是如果我使用'run as->maven build'通过maven运行我的项目,看起来只有BeforeSuite和BeforeTest类在运行,而实际的测试并没有运行。以下是我的pom.xml文件: 最后,这是StackTrace: 我想知道我错过了什么。