Tengo un proyecto de maven en el que estoy ejecutando testng.xml desde el archivo pom.xml.

Mi archivo testng.xml se ve a continuación:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test name="FirefoxTest">
  <parameter name="browser" value="firefox"/>
    <classes>
      <class name="tests.PM_User_Test"/>
    </classes>
  </test> <!-- Test -->

  <test name="ChromeTest">
    <parameter name="browser" value="chrome"/>
    <classes>
      <class name="tests.PM_User_Test"/>
      <class name="tests.PM_Extension_Test"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Y abajo está mi clase:

    package base;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import utilities.ExcelReadAndWrite;
import utilities.Take_Screenshot;

public class ConfigClass
{
    public WebDriver driver = null;
    public static String excelPath = null;
    public static ExcelReadAndWrite loginData;
    public static ExcelReadAndWrite pmTests;
    public static ExcelReadAndWrite snmTests;
    public static ExcelReadAndWrite ipData;


    @BeforeSuite
    public void beforeSuite()
    {
        excelPath = "C://Users//mallikar//git//PM//PM//src//main//java//myTestData//TestData.xlsx";
        loginData = new ExcelReadAndWrite("logindata", excelPath);
        pmTests = new ExcelReadAndWrite("PMTestData", excelPath);
        snmTests = new ExcelReadAndWrite("SNMTestData", excelPath);
        ipData = new ExcelReadAndWrite("IP", excelPath);
    }

    @AfterSuite
    public void afterSuite() 
    {

    }

    @Parameters({"browser"})
    @BeforeClass
    public void beforeClass(String browser)
    {
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
        if(browser.equalsIgnoreCase("firefox"))
        {
            driver = new FirefoxDriver(dc);
        }
        else if(browser.equalsIgnoreCase("chrome"))
        {
            driver = new ChromeDriver();
        }

    }

    /*@Parameters("browser")
    @BeforeClass
    public void beforeClass()
    {
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
        driver = new FirefoxDriver(dc);
    }*/

    @AfterClass
    public void afterClass()
    {
        driver.close();
    }

    @BeforeMethod
    public void beforeMethod()
    {
//      driver.get("https://sqa.stackexchange.com/questions/36253/taking-screenshot-on-test-failure-selenium-webdriver-testng");
//      System.out.println(driver);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void afterMethod(ITestResult result) throws IOException
    {
        if(result.getStatus() == 2)
        {
            String methodName = result.getMethod().getMethodName();
            new Take_Screenshot().get_Screenshot(driver, methodName);
        }
    }
}


But I am getting the "Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined" error in maven project.

However when I run only testng.xml file it is absolutely working fine. The pro

Blem viene cuando ejecuto testng.xml desde el archivo pom.xml.

A continuación se muestra el contenido del archivo POM.xml donde he definido testng.xml:

<profiles>
    <profile>
        <id>Regression</id>
        <build>
    <plugins>
    <plugin>
        <inherited>true</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
           <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.2</version>
        <configuration>                        
          <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>              
          </suiteXmlFiles>         
        </configuration>       
      </plugin>   
    </plugins>
  </build>
    </profile>
  </profiles>

He pasado por muchos hilos sobre esto en Internet pero no he encontrado ninguna solución. Por favor, que alguien me ayude con esto.

Y debajo están los registros de la consola cuando ejecuto el comando mvn clean install

Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@ba4d54
Tests run: 10, Failures: 2, Errors: 0, Skipped: 8, Time elapsed: 2.368 sec <<< FAILURE!
beforeClass(tests.PM_Extension_Test)  Time elapsed: 2.098 sec  <<< FAILURE!
org.testng.TestNGException:
Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined

        at org.testng.internal.Parameters.createParams(Parameters.java:289)
        at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:377)
        at org.testng.internal.Parameters.createParameters(Parameters.java:721)
        at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:164)
        at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:275)
        at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.testng.TestRunner.privateRun(TestRunner.java:770)
        at org.testng.TestRunner.run(TestRunner.java:591)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
        at org.testng.SuiteRunner.run(SuiteRunner.java:304)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
        at org.testng.TestNG.runSuites(TestNG.java:1032)
        at org.testng.TestNG.run(TestNG.java:1000)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:159)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

beforeClass(tests.PM_User_Test)  Time elapsed: 0.002 sec  <<< FAILURE!
org.testng.TestNGException:
Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined

        at org.testng.internal.Parameters.createParams(Parameters.java:289)
        at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:377)
        at org.testng.internal.Parameters.createParameters(Parameters.java:721)
        at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:164)
        at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:275)
        at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.testng.TestRunner.privateRun(TestRunner.java:770)
        at org.testng.TestRunner.run(TestRunner.java:591)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
        at org.testng.SuiteRunner.run(SuiteRunner.java:304)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
        at org.testng.TestNG.runSuites(TestNG.java:1032)
        at org.testng.TestNG.run(TestNG.java:1000)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:159)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests:   beforeClass(tests.PM_Extension_Test): (..)
  beforeClass(tests.PM_User_Test): (..)

Tests run: 10, Failures: 2, Errors: 0, Skipped: 8

[WARNING] Could not delete temp direcotry C:\Users\mallikar\git\PM\PM\target\surefire because Directory C:\Users\mallikar\git\PM\PM\target\surefire unable to be deleted.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.374 s
[INFO] Finished at: 2019-09-26T16:39:22+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project PM: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\mallikar\git\PM\PM\target\surefire-reports for the individual test results.
[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/MojoFailureException
0
Mallikarjuna B 26 sep. 2019 a las 06:54

1 respuesta

La mejor respuesta

Intente realizar una de las siguientes acciones (que debería solucionar su problema)

  1. Asegúrese de que la anotación @Parameters se importe del paquete org.testng.annotations
  2. Asegúrese de cumplir con uno de los siguientes al ejecutar la prueba:
    1. Si está ejecutando desde el IDE, haga clic con el botón derecho en la clase y seleccione run as TestNG test y luego asegúrese de editar la configuración de ejecución y proporcionar el argumento JVM -Dbrowser=firefox
    2. Si desea ejecutar la suite completa, deberá asegurarse de hacer clic con el botón derecho en la suite y luego ejecutarla.
    3. Si está ejecutando desde la línea de comandos usando maven, asegúrese de que está usando mvn clean test -P Regression (esto aseguraría que el perfil Regression se activa y, a través de eso, la ejecución de la prueba ocurre a través del paquete archivo)
1
Krishnan Mahadevan 26 sep. 2019 a las 11:55