失败的配置:方法beforeTest需要1个参数,但@Configuration批注中提供了0个

时间:2018-08-20 08:44:58

标签: java selenium-webdriver testng

我试图创建一个用于Browserloading的函数,并从另一个类中调用了该函数,但遇到了错误。

   FAILED CONFIGURATION: @BeforeMethod beforeTest
    org.testng.TestNGException: 
    Method beforeTest requires 1 parameters but 0 were supplied in the @Configuration annotation.

我还在testNG xml文件中创建了testNG参数

下面是我为浏览器加载而创建的函数,以便可以从其他类中调用它

import com.seleniumdata.zmartano.LoanDetails;

public class Browser {

    public  static WebDriver driver;

     LoanDetails objLoan = new LoanDetails();

       @BeforeMethod
       @Parameters("Browser")
       public  void beforeTestUtility(String browser) throws Exception {
           LoanDetails.beforeTest(browser);
       }


   @Test
   public static void  GetBrowser(String Browser) 
   {

      if  (Browser.equalsIgnoreCase("Firefox")) {
              Log.info("Driver Initiated");
              System.setProperty("webdriver.firefox.driver", Constants.geckodriver);
              driver = new FirefoxDriver();
              driver.get(Constants.URL);
              Log.info("Application Opening");
              driver.manage().window().maximize();

        }

              else if (Browser.equalsIgnoreCase("Chrome")) {
                  Log.info("Driver Initiated");
                  System.setProperty("webdriver.chrome.driver", Constants.chromedriver);
                  driver = new ChromeDriver();
                  driver.get(Constants.URL);
                  Log.info("Application Opening");
                  driver.manage().window().maximize();


              }


   }   
}

我需要从另一个类调用浏览器功能

public class LoanDetails {



    public static  WebDriver driver ;





     public static  void beforeTest(String browser) throws Exception {
     Browser.GetBrowser(browser);

     }

tesng xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">



  <test name ="FirefoxTest">
   <parameter name="Browser" value ="Firefox"/>


    <classes>

      <class name="com.seleniumdata.zmartano.LoanDetails"/>
    </classes>



  </test> <!-- Test -->
</suite> <!-- Suite -->

1 个答案:

答案 0 :(得分:3)

您正在将浏览器对象传递给LoanDetails类中的void beforeTest()方法

因此您需要在@BeforeMethod注释上传递@Parameter注释:

public class LoanDetails {     

     WebDriver driver ;     

      public void commonMethod(String browser) throws Exception { 
              driver = Browser.GetBrowser(browser); 
      }
}

调用单独的类作为Test类,

public class Browser {

 private static WebDriver driver;
 LoanDetails objLoan = new LoanDetails();

   @BeforeMethod
   @Parameters("Browser")
   public  void beforeTestUtility(String browser) throws Exception {
        objLoan.commonMethod(browser);
   }

   @Test
   public static WebDriver  GetBrowser(String Browser) 
   {
    if (driver != null) 

        return driver;


        else if (Browser.equalsIgnoreCase("Firefox")) {
              Log.info("Driver Initiated");
              System.setProperty("webdriver.firefox.driver", Constants.geckodriver);
              driver = new FirefoxDriver();
              driver.get(Constants.URL);
              Log.info("Application Opening");
              driver.manage().window().maximize();
              return driver;
        }

              else if (Browser.equalsIgnoreCase("Chrome")) {
                  Log.info("Driver Initiated");
                  System.setProperty("webdriver.chrome.driver", Constants.chromedriver);
                  driver = new ChromeDriver();
                  driver.get(Constants.URL);
                  Log.info("Application Opening");
                  driver.manage().window().maximize();

                  return driver;
              }                    
   return driver;                    
   }   
}