[英]How to force an inheriting class to implement a method in its constructor?
我遇到的問題是我有一堆從父類 (BasePage) 繼承的類 (Pages)。 任何時候初始化子類時,我還需要運行一個名為 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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.