1

我有一个类:函数库,我在构造函数中实例化 webdriver 实例,如下所示

public class FunctionLibrary {
    public WebDriver driver;

    public FunctionLibrary(WebDriver driver)
    {
        driver = new FirefoxDriver();
        this.driver=driver;
    }

    public WebDriver getDriver(){
        return this.driver;
    }
}

我正在访问扩展超类的子类中的 webdriver 实例:函数库

public class Outlook extends FunctionLibrary{
    public Outlook(WebDriver driver) {
        super(driver);      
    }

    @Before
    public void testSetUp()
    {
        getDriver().navigate().to("https://google.com");
    }

    @After
    public void closeTest()
    {
        getDriver().close();
    }

    @Test
    public void openOutlookAndCountTheNumberOfMails()
    {
        System.out.println("executed @Test Annotation");        
    }
}

当我执行上面的junit代码时,我收到错误

java.lang.Exception:测试类应该只有一个公共零参数构造函数

任何人都可以让我在哪里出错

4

3 回答 3

3

无需将参数传递给 ctor FunctionLibrary,因为您只需覆盖其值:

public class FunctionLibrary {
    public WebDriver driver;

    public FunctionLibrary()
    {
        this.driver=new FirefoxDriver();
    }

    // etc.
}

进行此更改意味着您不需要从测试类中传递参数:只需删除其构造函数即可。

于 2015-12-21T14:20:27.580 回答
2

你需要@RunWith(Parameterized.class)一流的。

如果你使用@Parameters正确的,它会运行得很好。:)

于 2017-02-22T09:11:28.063 回答
1

您没有公共的零参数构造函数。测试环境不知道将什么传递给它的构造函数,因为您需要WebDriver传递一个对象。

public Outlook(WebDriver driver) {
    super(driver);
}

在那里,您希望测试环境通过什么?而是做一些事情,比如保持一个零参数的构造函数,然后自己传入一个 WebDriver 实例。像这样的东西应该工作。

public Outlook() {
    super(new FirefoxDriver());      
}

希望这可以帮助。

于 2015-12-21T14:21:57.623 回答