“ Maven项目中,BeforeClass在方法beforeClass上需要参数'浏览器',但未标记为@Optional或defined”错误

问题描述 投票:0回答:1

我有一个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 -->

下面是我的课:

    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>

我在Internet上经历了很多讨论,但是没有找到任何解决方案。请有人帮助我。

java selenium testng
1个回答
0
投票

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

  1. 确保批注@Parameters从包org.testng.annotations导入
  2. [在运行测试时,请确保遵循以下条件之一:
    1. 如果从IDE运行,请右键单击该类并选择run as TestNG test,然后确保您编辑运行配置并提供JVM参数-Dbrowser=firefox
    2. 如果您要运行整个套件,则需要确保右键单击套件然后运行它。
    3. 如果使用maven从命令行运行,请确保使用的是mvn clean test -p Regression(这将确保配置文件Regression被激活,并通过套件文件进行测试执行)
© www.soinside.com 2019 - 2024. All rights reserved.