错误-> cucumber.runtime.CucumberException:无法实例化类<class>-此类没有空或启用页面的构造函数”

时间:2019-10-24 10:04:20

标签: java selenium-webdriver firefox compiler-errors runtime-error

我使用FirefoxDriverInstance作为呼叫驱动程序的单例。另外,使用firefox配置文件绕过2要素身份验证。

public class FirefoxDriverInstance {

public static WebDriver Instance = null;
public static void Initialize() {

    if (Instance == null) {
        System.out.println("Initializing Firefox Driver!!!");
        System.setProperty("webdriver.gecko.driver","./src/test/resources/driver/geckodriver2.exe");

        ProfilesIni profile = new ProfilesIni();
        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile.getProfile("SeleniumTester"));
        Instance = new FirefoxDriver(options);
    }

    Instance.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    Instance.manage().window().maximize();
}


public static void close() {
    System.out.println("Closing Browser!!!");
    Instance.close();
    Instance = null;
}


public static void quit() {
    System.out.println("Quitting Browser!!!");
    Instance.quit();
    Instance = null;
}

}

下面是我的使用cucumber-selenium-webdriver的登录代码。我使用了相同的驱动程序实例。

public class TC01_Login {

@BeforeMethod
public void openbrowser() throws IOException { 
    FirefoxDriverInstance.Initialize();
 }

    public Properties propertyFileReader() throws Exception {
    Properties obj = new Properties();                  
    FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\resources\\testdata\\application.properties");                                   
    obj.load(objfile);
    return obj;
 }

WebDriverWait wait = new WebDriverWait(FirefoxDriverInstance.Instance, 150);


@Given("^user has entered the required URL$")
public void user_has_entered_the_required_URL() throws Throwable {
    Properties obj = propertyFileReader();

    System.out.println("In the First Loop!!");

    FirefoxDriverInstance.Instance.get(obj.getProperty("baseUrl"));
    String actualTitle = FirefoxDriverInstance.Instance.getTitle();
    System.out.println("Actual Title :" + actualTitle);

    if (actualTitle.contains("test")){
        System.out.println("INFO : Title is being displayed as expected.");
     }
     else {
         System.out.println("ERROR : Title is NOT being displayed as expected."); }
}
}

无法理解如何绕过此错误。

1 个答案:

答案 0 :(得分:0)

尝试将以下代码放入构造函数中:

   FirefoxDriverInstance.Initialize();

示例:

public class TC01_Login {

   public TC01_Login (){
       FirefoxDriverInstance.Initialize();
    }
}

BeforeMethod

删除它