Skip to content

Instantly share code, notes, and snippets.

@krishna-acondy
Created June 26, 2019 10:01
Show Gist options
  • Save krishna-acondy/f43e3e5dbe5424f471e524bcb919f8c2 to your computer and use it in GitHub Desktop.
Save krishna-acondy/f43e3e5dbe5424f471e524bcb919f8c2 to your computer and use it in GitHub Desktop.
Base Page
namespace AutomatedUiTests.Pages
{
public abstract class BasePage
{
public string Url => WebDriver.Url;
public void Close()
{
WebDriver.Close();
}
public void Refresh()
{
WebDriver.Navigate().Refresh();
}
public virtual bool HasLoaded()
{
var result = DocumentReady((IJavaScriptExecutor) driver);
return result.Equals("complete");
}
public virtual void WaitFor(TimeSpan timeSpan)
{
Thread.Sleep(timeSpan);
}
public virtual void WaitUntil(Func<bool> condition, int timeoutValue = 30)
{
var wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(timeoutValue));
wait.Until(driver => condition.Invoke().Equals(true));
}
public virtual void WaitUntilLoaded()
{
var wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
wait.Until(driver => DocumentReady((IJavaScriptExecutor) driver));
}
public string Title => Header.Title;
protected BasePage(IWebElement element)
{
_webElement = element;
}
protected IWebElement FindElement(By locator)
{
var wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(30));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(locator));
WebDriver.ExecuteJavaScript("arguments[0].scrollIntoView();", _webElement.FindElement(locator));
return _webElement.FindElement(locator);
}
protected IEnumerable<IWebElement> FindElements(By locator)
{
return _webElement.FindElements(locator);
}
protected Header Header =>
new Header(_webElement.FindElement(By.TagName("xpl-header")));
private readonly IWebElement _webElement;
private IWebDriver WebDriver => ((IWrapsDriver) _webElement).WrappedDriver;
private static bool DocumentReady(IJavaScriptExecutor executor) =>
executor.ExecuteScript("return document.readyState").Equals("complete");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment