如何强制继承类在其构造函数中实现方法
本文关键字:构造函数 实现 方法 何强制 继承 | 更新日期: 2023-09-27 18:08:11
我遇到的问题是,我有一堆继承自父类(BasePage)的类(页面)。每当初始化子类时,还需要运行一个名为WaitForLoadingToStop()的方法。我如何继承这个方法和它的实现到我的子类的构造函数?
这是我需要改变的,因为它在所有地方都是重复的,因此,我希望在创建类时自动继承并执行它。
var assPage = new StudentAssesmentPage(Driver);
assPage.WaitForLoadingToStop();
这是一个样例子类,我有另外10个像这样的
public class StudentAssesmentPage : BasePage<StudentAssesmentPageObjectRepository>
{
public StudentAssesmentPage(IWebDriver driver) : base(driver, new StudentAssesmentPageObjectRepository(driver))
{}
}
下面是我的BasePage类
public abstract class BasePage<TObjectRepository> where TObjectRepository : BasePageObjectRepository
{
protected WebDriverWait Wait { get; }
protected IWebDriver Driver { get; }
protected ApplicationUrls ApplicationUrls { get; }
public IJavaScriptExecutor JavascriptExecutor { get; }
protected Actions UserInteractions { get; private set; }
protected By _loadingSpinnerLocator = By.Id("spinner");
private readonly double LOADING_SPINNER_TIMEOUT = 60;
private TObjectRepository _objectRepository;
public BasePage(IWebDriver driver, TObjectRepository repository)
{
Driver = driver;
_objectRepository = repository;
UserInteractions = new Actions(Driver);
ApplicationUrls = new ApplicationUrls();
Wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(GetPageTimeout()));
JavascriptExecutor = Driver as IJavaScriptExecutor;
}
internal TObjectRepository ObjectRepository
{
get { return _objectRepository; }
}
protected bool WaitForLoadingToStop(int secondsToSleep = 5)
{
var sleepTime = TimeSpan.FromSeconds(secondsToSleep);
Reporter.Debug($"{Helpers.GetCurrentMethodName()}. Going to sleep for:{sleepTime.Seconds}");
Thread.Sleep(sleepTime);
Reporter.Debug($"Now, going to wait for loading spinner for:{LOADING_SPINNER_TIMEOUT} seconds.");
var basePageWait = new WebDriverWait(Driver, TimeSpan.FromSeconds(LOADING_SPINNER_TIMEOUT));
basePageWait.Message =
$"Waited for {LOADING_SPINNER_TIMEOUT}sec for the spinner. However, it took longer than that for the application to load.";
return basePageWait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//img[@src='/Content/images/spinner.gif']")));
}
}
从基类实现BasePageThatWaitsForLoadingToStop
继承,它在构造函数中为您完成工作。唯一剩下的样板代码是传递特定实现的构造函数参数(例如StudentAssesmentPage
到这个基类)
public class StudentAssesmentPage : BasePageThatWaitsForLoadingToStop
{
public StudentAssesmentPage(IWebDriver driver) : base(driver, new StudentAssesmentPageObjectRepository(driver)) // this is the only boilerplate code that is left
{
}
}
public class BasePageThatWaitsForLoadingToStop : BasePage<StudentAssesmentPageObjectRepository>
{
public BasePageThatWaitsForLoadingToStop(IWebDriver driver) : base(driver, new StudentAssesmentPageObjectRepository(driver))
{
base.WaitForLoadingToStop();
}
}
你可以像下面这样简单地调用它,它会自动为你工作:
var assPage = new StudentAssesmentPage(Driver);
但是,请注意,从测试的角度来看,在构造函数中工作并不理想。
public class StudentAssesmentPage : BasePage<StudentAssesmentPageObjectRepository>
{
public StudentAssesmentPage(IWebDriver driver) : base(driver, new StudentAssesmentPageObjectRepository(driver))
{
base.WaitForLoadingToStop();
}
}