【问题标题】:Error -> cucumber.runtime.CucumberException: Failed to instantiate class <class> - this class doesn't have an empty or a page enabled constructor"错误 -> cucumber.runtime.CucumberException:无法实例化类 <class> - 此类没有空的或启用页面的构造函数”
【发布时间】:2019-10-24 10:04:20
【问题描述】:

我使用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."); }
}
}

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

【问题讨论】:

  • 你不要问问题。

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


【解决方案1】:

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

   FirefoxDriverInstance.Initialize();

示例:

public class TC01_Login {

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

BeforeMethod

中删除它

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多