1

I have a class : Function Library where I am instantiating the webdriver instance in constructor as below

public class FunctionLibrary {
    public WebDriver driver;

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

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

I am accessing the webdriver instance in child class extending super class: function library

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");        
    }
}

when I execute the above piece of junit code I am getting error as

java.lang.Exception: Test class should have exactly one public zero-argument constructor

Anybody can let me where I am going wrong

resueman
  • 10,572
  • 10
  • 31
  • 45
Shilpa
  • 75
  • 2
  • 5
  • 12
  • You don't have exactly one public zero-argument constructor. You have exactly one public one-argument constructor. Where do you think the constructor parameter `driver` will come from? – Andy Turner Dec 21 '15 at 14:15
  • BTW: there's no need to use inheritance for this. Use composition. – Andy Turner Dec 21 '15 at 14:16
  • Also: you override the value of `driver` passed into the constructor of `FunctionLibrary`. You currently don't need the parameter; your problem will go away if you remove the parameter (and the corresponding parameter to Outlook's constructor too). – Andy Turner Dec 21 '15 at 14:18
  • Thanks Andy, It helped me – Shilpa Dec 21 '15 at 18:16
  • Why is there no answer marked correct? – Val Mar 19 '18 at 05:15

3 Answers3

3

There is no need to pass a parameter into the ctor of FunctionLibrary, since you simply overwrite its value:

public class FunctionLibrary {
    public WebDriver driver;

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

    // etc.
}

Making this change means you don't need to pass a parameter in from the test class: just remove its constructor.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

You need the @RunWith(Parameterized.class) on top of the class.

If you use the @Parameters correct it will run just fine. :)

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
1

You do not have a public zero-arg constructor. The testing environment doesn't know what to pass to it's constructor, since you require a WebDriver object to be passed.

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

There, what do you want the testing environment to pass? Instead do something like keep a zero arg constructor, and pass in a WebDriver instance yourself. Something like this should work.

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

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91