我遇到的问题是我有一堆继承自父类(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']")));
}
}
答案 0 :(得分:1)
只需从构造函数中为您工作的基类实现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);
但是,请注意,从测试的角度来看,在构造函数中进行工作并不理想。
答案 1 :(得分:-1)
public class StudentAssesmentPage : BasePage<StudentAssesmentPageObjectRepository>
{
public StudentAssesmentPage(IWebDriver driver) : base(driver, new StudentAssesmentPageObjectRepository(driver))
{
base.WaitForLoadingToStop();
}
}