Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Add method for returning types which inherit from MvcWebForm, WebForm… #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Src/MvcPages/BrowserAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,28 @@ public MvcWebForm<TModel> GetForm<TModel>(string formId)
return new MvcWebForm<TModel>(formElement, Driver, supportedInputsAdapters, this.NumberOfInputSetRetries, this.AfterFieldValueSetAction);
}

public TCustomForm GetCustomMvcForm<TCustomForm, TModel>(string formId) where TCustomForm : MvcWebForm<TModel>, new()
{
var form = new TCustomForm();
var formElement = this.Driver.GetStableAccessibleElementById(formId);
form.Init(formElement, Driver, supportedInputsAdapters, this.NumberOfInputSetRetries, this.AfterFieldValueSetAction);
return form;
}

public WebForm GetForm(string formId)
{
var formElement = this.Driver.GetStableAccessibleElementById(formId);
return new WebForm(formElement, Driver, supportedInputsAdapters, this.NumberOfInputSetRetries, this.AfterFieldValueSetAction);
}

public TCustomForm GetCustomForm<TCustomForm>(string formId) where TCustomForm : WebForm, new()
{
var formElement = this.Driver.GetStableAccessibleElementByInScope(By.TagName("form"), Driver);
var form = new TCustomForm();
form.Init(formElement, Driver, supportedInputsAdapters, this.NumberOfInputSetRetries, this.AfterFieldValueSetAction);
return form;
}

public WebForm GetForm()
{
var formElement = this.Driver.GetStableAccessibleElementByInScope(By.TagName("form"), Driver);
Expand All @@ -213,6 +229,15 @@ public IPageFragment GetPageFragmentById(string elementId)
return new PageFragment(Driver, pageFragment);
}

public TCustomPageFragment GetCustomPageFragmentById<TCustomPageFragment>(string elementId)
where TCustomPageFragment : PageFragment, new()
{
var element = Driver.GetStableAccessibleElementById(elementId);
var pageFragment = new TCustomPageFragment();
pageFragment.Init(Driver, element);
return pageFragment;
}

public void RefreshPage()
{
this.navigator.RefreshPage();
Expand Down Expand Up @@ -404,12 +429,34 @@ public interface IBrowserAdapter : IPageFragment, IBrowserCamera, IDisposable,
MvcWebForm<TModel> GetForm<TModel>(string formId);


/// <summary>
/// Return object that inherits from strongly typed adapter for web form with given id
/// </summary>
/// <typeparam name="TCustomForm">Inherited object type</typeparam>
/// <typeparam name="TModel">Model connected with form</typeparam>
/// <param name="formId">Id of expected form</param>
///<remarks>
/// This method allows to create custom objects that inherit from <see cref="MvcWebForm{T}"/>
/// </remarks>
TCustomForm GetCustomMvcForm<TCustomForm, TModel>(string formId) where TCustomForm : MvcWebForm<TModel>, new();

/// <summary>
/// Return weakly typed adapter for web form with given id
/// </summary>
/// <param name="formId">Id of expected form</param>
WebForm GetForm(string formId);


/// <summary>
/// Return object that inherits from weakly typed adapter for web form with given id
/// </summary>
/// <typeparam name="TCustomForm">Class that derive from WebForm</typeparam>
/// <param name="formId">Id of expected form</param>
/// <remarks>
/// This method allows to create custom objects that inherit from <see cref="WebForm"/>
/// </remarks>
TCustomForm GetCustomForm<TCustomForm>(string formId) where TCustomForm : WebForm, new();

/// <summary>
/// Refresh page
/// </summary>
Expand All @@ -434,6 +481,13 @@ public interface IBrowserAdapter : IPageFragment, IBrowserCamera, IDisposable,
/// <param name="elementId">Id of expected element</param>
IPageFragment GetPageFragmentById(string elementId);


/// <summary>
/// Return page fragment with given id
/// </summary>
/// <param name="elementId">Id of expected element</param>
TCustomPageFragment GetCustomPageFragmentById<TCustomPageFragment>(string elementId) where TCustomPageFragment:PageFragment, new();

/// <summary>
/// Stop execution until element with given id appear
/// </summary>
Expand Down Expand Up @@ -515,5 +569,7 @@ public interface IBrowserAdapter : IPageFragment, IBrowserCamera, IDisposable,
/// <param name="downloadCallback">Action to invoke when finish downloading. Action parameter is a path to downloaded file,</param>
/// <param name="downloadTimeoutInSeconds"></param>
void DownloadFileWith(Action action, Action<string> downloadCallback = null, int downloadTimeoutInSeconds = 60);

WebForm GetForm();
}
}
19 changes: 14 additions & 5 deletions Src/MvcPages/WebPages/PageFragment.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Drawing;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;
Expand All @@ -12,15 +11,25 @@ namespace Tellurium.MvcPages.WebPages
{
public class PageFragment : IPageFragment
{
protected readonly RemoteWebDriver Driver;
protected readonly IWebElement WebElement;
protected RemoteWebDriver Driver;
protected IWebElement WebElement;

public PageFragment(RemoteWebDriver driver, IWebElement webElement)
{
this.Driver = driver;
this.WebElement = webElement;
}

protected PageFragment()
{
}

public void Init(RemoteWebDriver driver, IWebElement webElement)
{
this.Driver = driver;
this.WebElement = webElement;
}

public void Click()
{
this.Driver.ClickOn(this.WebElement);
Expand Down Expand Up @@ -141,11 +150,11 @@ private byte[] TakeSceenshotManually()
{
var wholePageScreenshot = Driver.GetScreenshot();
var imageScreen = wholePageScreenshot.AsByteArray.ToBitmap();
var webElementArea = GetWebElementAreaContrainedTo(imageScreen);
var webElementArea = GetWebElementAreaConstrainedTo(imageScreen);
return imageScreen.Clone(webElementArea, imageScreen.PixelFormat).ToBytes();
}

private Rectangle GetWebElementAreaContrainedTo(Bitmap imageScreen)
private Rectangle GetWebElementAreaConstrainedTo(Bitmap imageScreen)
{
var originalLocation = this.WebElement.Location;
var originalSize = this.WebElement.Size;
Expand Down
5 changes: 5 additions & 0 deletions Src/MvcPages/WebPages/WebForms/MvcWebForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public MvcWebForm(IWebElement webElement, RemoteWebDriver driver, List<IFormInpu
{
}

public MvcWebForm()
{
}


/// <summary>
/// Set value for field indicated by expression
/// </summary>
Expand Down
16 changes: 14 additions & 2 deletions Src/MvcPages/WebPages/WebForms/WebForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Tellurium.MvcPages.WebPages.WebForms
/// </summary>
public class WebForm : PageFragment
{
private readonly int numberOfSetRetries;
private readonly AfterFieldValueSet afterFieldValueSet;
private int numberOfSetRetries;
private AfterFieldValueSet afterFieldValueSet;
private List<IFormInputAdapter> SupportedInputs { get; set; }

public WebForm(IWebElement webElement, RemoteWebDriver driver, List<IFormInputAdapter> supportedInputs, int numberOfSetRetries, AfterFieldValueSet afterFieldValueSet = AfterFieldValueSet.Nothing) : base(driver, webElement)
Expand All @@ -24,6 +24,18 @@ public WebForm(IWebElement webElement, RemoteWebDriver driver, List<IFormInputAd
SupportedInputs = supportedInputs;
}

public WebForm()
{
}

public void Init(IWebElement webElement, RemoteWebDriver driver, List<IFormInputAdapter> supportedInputs, int numberOfSetRetries, AfterFieldValueSet afterFieldValueSet = AfterFieldValueSet.Nothing)
{
this.numberOfSetRetries = numberOfSetRetries;
this.afterFieldValueSet = afterFieldValueSet;
SupportedInputs = supportedInputs;
base.Init(driver, webElement);
}

/// <summary>
/// Set value for field indicated by field name
/// </summary>
Expand Down