Reputation: 47
Ok guys firstly I am sorry I am not too great with c#. Basically I have 2 class files, lets call one TestBase and the other TestScripts1. Within class TestScripts1 it inherits the initializer TestBase so all my scripts can run. Now in reverse I want to use one of my methods called DragAssignDeviceAndDriver() in TestScripts1 and use it in TestBase . Does anyone know how to do that?
Here is my code for the initializer from class TestBase :
[TestClass]
public class TestBase
{
[TestInitialize]
public void BaseTestInit()
{
// create chrome driver
//driver = new ChromeDriver();
ChromeOptions opt = new ChromeOptions();
opt.AddArgument("disable-infobars");
opt.AddArguments("--start-maximized");
opt.AddArguments("--disable-extensions");
driver = new ChromeDriver(opt);
LoginAndSelectAutomationFleet(driver);
//GenerationTestData();
}
}
public void LoginAndGenerateTestData(IWebDriver driver)
{
DragAssignDeviceAndDriver();
}
Above how do I inherit the method DragAssignDeviceAndDriver();
And here is my code class TestScripts1 - as you can see TestScripts1 inherits TestBase already:
[TestClass]
public class TestScripts1: TestBase
{
[TestInitialize]
public void Setup()
{
(_regRep.organizationOption, "Amazing Power", driver);
}
[TestMethod]
[TestCategory("Cat1")]
public void DragAssignDeviceAndDriver()
{
//Do stuff
}
Upvotes: 0
Views: 530
Reputation: 22073
You "can't" call a method, defined in an derivative class, from the base class. The base class needs to know how it should be called. It should be declared in the base class and to be overridden in the derivative class.
In this case, when the base class doesn't have any default implementation, you could declare it as abstract. (since it is a base class, it would not constructed directly)
This way the base class can call the overridden method.
For example:
[TestClass]
public abstract class TestBase
{
protected abstract void DragAssignDeviceAndDriver();
[TestInitialize]
public void BaseTestInit()
{
// create chrome driver
//driver = new ChromeDriver();
ChromeOptions opt = new ChromeOptions();
opt.AddArgument("disable-infobars");
opt.AddArguments("--start-maximized");
opt.AddArguments("--disable-extensions");
driver = new ChromeDriver(opt);
LoginAndSelectAutomationFleet(driver);
//GenerationTestData();
}
public void LoginAndGenerateTestData(IWebDriver driver)
{
DragAssignDeviceAndDriver();
}
}
[TestClass]
public class TestScripts1: TestBase
{
[TestInitialize]
public void Setup()
{
(_regRep.organizationOption, "Amazing Power", driver); // <--- ??
}
[TestMethod]
[TestCategory("Cat1")]
protected override void DragAssignDeviceAndDriver()
{
//Do stuff
}
}
Upvotes: 1