gpt4 book ai didi

java - maven项目中"Parameter '浏览器' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined"错误

转载 作者:行者123 更新时间:2023-12-02 09:29:18 25 4
gpt4 key购买 nike

我有一个 Maven 项目,其中我正在从 pom.xml 文件运行 testng.xml。

我的 testng.xml 文件如下所示:

<?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 -->

下面是我的类(class):

    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

当我从 pom.xml 文件运行 testng.xml 时出现问题。

下面是我定义了 testng.xml 的 POM.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>

我在互联网上浏览了很多有关此问题的帖子,但没有找到任何解决方案。请有人帮我解决这个问题。

下面是我运行 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

最佳答案

请尝试执行以下操作之一(应该可以解决您的问题)

  1. 确保注释@Parameters是从包org.testng.annotations导入的
  2. 运行测试时,请确保遵守以下其中一项规定:
    1. 如果您从 IDE 运行,请右键单击该类并选择作为 TestNG 测试运行,然后确保编辑运行配置并提供 JVM 参数-Dbrowser=火狐
    2. 如果您想运行整个套件,则需要确保右键单击该套件,然后运行它。
    3. 如果您使用 maven 从命令行运行,请确保您使用 mvn clean test -P Regression (这将确保配置文件 Regression 被激活通过此,测试执行通过套件文件进行)

关于java - maven项目中"Parameter '浏览器' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58109338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com