我有一个Parent类,该类使用Selenium和c#初始化我自动化的网站的打开。因此,所有子方法(单独的类文件)都继承父方法以启动网站并配置应用程序。在父类中,我想从子类中调用方法 MultipleAddVehicle(); 。我该怎么做呢。这是我要调用的父类和子方法的代码:
[TestInitialize]
public void BaseTestInit()
{
// create chrome driver
driver = new ChromeDriver(".");
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(45);
driver.Manage().Window.Maximize();
// create class for commonly used components / functions
objCommon = new clsCommon(driver);
_userSetRepo = new UserSetttingsRep(driver);
LoginAndSelectAutomationFleet();
}
public void LoginAndSelectAutomationFleet()
{
// login the user
objCommon.loginVT(driver);
System.Threading.Thread.Sleep(3000);
Actions builder = new Actions(driver);
builder.MoveToElement(_userSetRepo.userIcon).Click().Build().Perform(); //Hover on System
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(UserSetttingsRep.InputOrganisationSelector)));
objCommon.SendKeysAndClickTab(_userSetRepo.inputOrgansiation, "M-Powered Automation", driver);
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(By.XPath(UserSetttingsRep.InputOrganisationSelector)));
}
public void GenerationTestData()
{
//First data that is needed is vehicle management data
//Add multiple vehicles and add unassigned devices and unassigned drivers
**MultipleAddVehicle();**
}
[TestCleanup]
public void TestCleanUp()
{
driver.Quit();
objCommon.ValidateResult();
}
}
}
我想通过在父类中创建方法来从父类中的其他类调用子方法:
public void MultipleAddVehicle()
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Actions builder = new Actions(driver);
builder.MoveToElement(_regRep.SystemIcon).Click().Build().Perform(); //Hower on System }
System.Threading.Thread.Sleep(8000);
_vechRep.VehicleMgmtLink.Click();
string[] array = { "AK12 OLX", "FN53 KTO", "OU03 TGX", "BF14 YDS" };
// Loop with foreach and write colors with string interpolation.
foreach (string VRN in array)
{
//_vechRep.SearchVehicle.Click();
System.Threading.Thread.Sleep(5000);
_vechRep.AddNewVehicle.Click();
_vechRep.VRNSearch.SendKeys(VRN);
_vechRep.FindVehicle.Click();
System.Threading.Thread.Sleep(2000);
_vechRep.VehicleTypeOption.SendKeys("Car");
_vechRep.VehicleTypeOption.SendKeys(Keys.Tab);
System.Threading.Thread.Sleep(1000);
_vechRep.VehicleColor.SendKeys("Red");
_vechRep.btnAddVehicle.Click();
System.Threading.Thread.Sleep(2000);
try
{
if (_vechRep.VehicleAddSuccessMsg.Displayed)
{
Console.WriteLine("Vehicle is successfuly added");
}
}
catch
{
Console.WriteLine("Vehicle is NOT added");
Assert.Fail();
}
}
}
这是我尝试在父类中创建和使用方法的方法
public void GenerationTestData()
{
//First data that is needed is vehicle management data
//Add multiple vehicles and add unassigned devices and unassigned drivers
**MultipleAddVehicle();**
我得到这个在当前上下文中不存在
答案 0 :(得分:0)
在父类中将MultipleAddVehicle
定义为抽象方法,并在子类中将其覆盖。这将需要您将父类定义为抽象类:
public abstract class Parent
{
public abstract void MultipleAddVehicle();
[TestInitialize]
public void BaseTestInit()
{
// ...
}
public void LoginAndSelectAutomationFleet()
{
// ...
}
public void GenerationTestData()
{
//First data that is needed is vehicle management data
//Add multiple vehicles and add unassigned devices and unassigned drivers
MultipleAddVehicle();
}
}
public class Child : Parent
{
public override void MultipleAddVehicle()
{
// ...
}
}
这只是老式的面向对象编程。这里没有魔术。
这与Template Method Pattern非常相似。
答案 1 :(得分:-1)
子类看到父类的所有成员,但是父类没有看到子类的所有成员,除非它有权访问子类对象。如果子类不是静态类,则必须创建子类的实例并将其上的引用放到父类中。如果所有类都在同一个命名空间中,则它们将彼此可见。
public class ParentClass
{
.................................
public ChildClass childclassinstance;
public void GenerationTestData()
{
//First data that is needed is vehicle management data
//Add multiple vehicles and add unassigned devices and unassigned drivers
childclassinstance.MultipleAddVehicle();
}
}
public class ChildClass : ParentClass
{
public void MultipleAddVehicle()
{
}
}
当然,您需要在调用子类实例的方法之前对其进行初始化。如果您不关心此特定对象的值,而只想调用一个void方法,则可以通过默认构造函数从父类中完成此操作。
public ChildClass childclassinstance = new ChildClass();
或者如果它是在测试期间创建的,则可以从子类中初始化它
childclassinstance = this;
或者您可以制作MultipleAddVehicle();一个静态方法,因此您无需创建对象,只需从类类型调用它即可。
ChildClass.MultipleAddVehicle();