Skip to content

Commit cc9394f

Browse files
committed
#10 added PageInfo attribute, constructor for Form
1 parent 01b8f66 commit cc9394f

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed
Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,80 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using Aquality.Selenium.Elements;
2+
using Aquality.Selenium.Elements.Interfaces;
3+
using Aquality.Selenium.Localization;
4+
using Aquality.Selenium.Logging;
5+
using OpenQA.Selenium;
6+
using System;
7+
using System.Linq;
48

59
namespace Aquality.Selenium.Forms
610
{
711
public abstract class Form
812
{
13+
private readonly Logger logger = Logger.Instance;
14+
15+
/// <summary>
16+
/// Locator for specified form
17+
/// </summary>
18+
protected readonly By Locator;
19+
20+
/// <summary>
21+
/// Name of specified form
22+
/// </summary>
23+
protected readonly string Name;
24+
25+
private readonly IElementFactory ElementFactory;
26+
27+
/// <summary>
28+
/// Constructor
29+
/// </summary>
30+
protected Form()
31+
{
32+
var type = typeof(Form).GetCustomAttributes(false).OfType<PageInfoAttribute>().FirstOrDefault();
33+
Name = type.PageName;
34+
Locator = GetLocatorFromPageInfo(type);
35+
ElementFactory = new ElementFactory();
36+
}
37+
38+
/// <summary>
39+
/// Constructor with parameters
40+
/// </summary>
41+
/// <param name="locator">Unique locator of the page.</param>
42+
/// <param name="name">Name of the page.</param>
43+
protected Form(By locator, string name)
44+
{
45+
Locator = locator;
46+
Name = name;
47+
ElementFactory = new ElementFactory();
48+
}
49+
50+
protected By GetLocatorFromPageInfo(PageInfoAttribute pageInfo)
51+
{
52+
if (!string.IsNullOrWhiteSpace(pageInfo.Xpath))
53+
{
54+
return By.XPath(pageInfo.Xpath);
55+
}
56+
else if (!string.IsNullOrWhiteSpace(pageInfo.Id))
57+
{
58+
return By.Id(pageInfo.Id);
59+
}
60+
else if (!string.IsNullOrWhiteSpace(pageInfo.Css))
61+
{
62+
return By.CssSelector(pageInfo.Css);
63+
}
64+
var message = string.Format(LocalizationManager.Instance.GetLocalizedMessage("loc.baseform.unknown.type"), Name);
65+
var exception = new ArgumentException(message);
66+
logger.FatalLoc(message, exception);
67+
throw exception;
68+
}
69+
70+
/// <summary>
71+
/// Return form state for form locator
72+
/// </summary>
73+
/// <returns>True - form is opened,
74+
/// False - form is not opened</returns>
75+
public bool IsFormDisplayed()
76+
{
77+
return ElementFactory.GetLabel(Locator, Name).State.WaitForDisplayed();
78+
}
979
}
1080
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace Aquality.Selenium.Forms
4+
{
5+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
6+
public class PageInfoAttribute : Attribute
7+
{
8+
/// <summary>
9+
/// Page name
10+
/// </summary>
11+
public string PageName { get; private set; }
12+
13+
/// <summary>
14+
/// Page xpath anchor
15+
/// </summary>
16+
public string Xpath { get; private set; }
17+
18+
/// <summary>
19+
/// Page id anchor getter
20+
/// </summary>
21+
public string Id { get; private set; }
22+
23+
/// <summary>
24+
/// Page css anchor
25+
/// </summary>
26+
public string Css { get; private set; }
27+
28+
public PageInfoAttribute(string pageName = default, string xpath = default, string id = default, string css = default)
29+
{
30+
PageName = pageName;
31+
Xpath = xpath;
32+
Id = id;
33+
Css = css;
34+
}
35+
}
36+
}

Aquality.Selenium/src/Aquality.Selenium/Resources/Localization/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"loc.baseform.unknown.type": "Unknown type of locator for page '{0}'",
23
"loc.browser.arguments.setting": "Setting browser start arguments from JSON file: {0}",
34
"loc.browser.back": "Return to previous page",
45
"loc.browser.forward": "Proceed to the next page",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"loc.baseform.unknown.type": "Неизвестный тип локатора для страницы '{0}'"
23
}

0 commit comments

Comments
 (0)