Skip to content

Commit f79b743

Browse files
authored
Implement elements (#41)
* #12 Implement elements * #12 Rename not necessary method from ITextBox * #12 Remove clear element from Type * #12 fix comments * #12 Remove clear from Type in TextBox
1 parent 26c1389 commit f79b743

File tree

12 files changed

+174
-52
lines changed

12 files changed

+174
-52
lines changed

Aquality.Selenium/src/Aquality.Selenium/Elements/Button.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Elements.Interfaces;
2+
using Aquality.Selenium.Localization;
23
using OpenQA.Selenium;
3-
using System;
44

55
namespace Aquality.Selenium.Elements
66
{
@@ -13,6 +13,6 @@ protected internal Button(By locator, string name, ElementState state) : base(lo
1313
{
1414
}
1515

16-
protected override string ElementType => throw new NotImplementedException();
16+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.button");
1717
}
1818
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Aquality.Selenium.Elements.Actions;
22
using Aquality.Selenium.Elements.Interfaces;
3+
using Aquality.Selenium.Localization;
4+
using Aquality.Selenium.Logging;
35
using OpenQA.Selenium;
4-
using System;
56

67
namespace Aquality.Selenium.Elements
78
{
@@ -14,25 +15,41 @@ protected internal CheckBox(By locator, string name, ElementState state) : base(
1415
{
1516
}
1617

17-
protected override string ElementType => throw new NotImplementedException();
18+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.checkbox");
1819

19-
public bool IsChecked => throw new NotImplementedException();
20-
21-
CheckBoxJsActions ICheckBox.JsActions => new CheckBoxJsActions(this, ElementType);
20+
public bool IsChecked
21+
{
22+
get
23+
{
24+
Logger.InfoLoc("loc.checkbox.get.state");
25+
return GetElement().Selected;
26+
}
27+
}
2228

29+
public new CheckBoxJsActions JsActions => new CheckBoxJsActions(this, ElementType);
30+
2331
public void Check()
2432
{
25-
throw new NotImplementedException();
33+
SetState(true);
34+
}
35+
36+
public void Uncheck()
37+
{
38+
SetState(false);
2639
}
2740

2841
public void Toggle()
2942
{
30-
throw new NotImplementedException();
43+
SetState(!IsChecked);
3144
}
3245

33-
public void Uncheck()
46+
private void SetState(bool state)
3447
{
35-
throw new NotImplementedException();
48+
Logger.InfoLoc("loc.setting.value", state.ToString());
49+
if (state != IsChecked)
50+
{
51+
Click();
52+
}
3653
}
3754
}
3855
}
Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using Aquality.Selenium.Elements.Actions;
22
using Aquality.Selenium.Elements.Interfaces;
3+
using Aquality.Selenium.Localization;
4+
using Aquality.Selenium.Logging;
5+
using Aquality.Selenium.Waitings;
36
using OpenQA.Selenium;
4-
using System;
7+
using OpenQA.Selenium.Support.UI;
58
using System.Collections.Generic;
9+
using System.Linq;
610

711
namespace Aquality.Selenium.Elements
812
{
@@ -15,39 +19,93 @@ protected internal ComboBox(By locator, string name, ElementState state) : base(
1519
{
1620
}
1721

18-
protected override string ElementType => throw new NotImplementedException();
22+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.combobox");
1923

20-
public string SelectedText => throw new NotImplementedException();
24+
public string SelectedText => ConditionalWait.WaitFor(driver => new SelectElement(GetElement()).SelectedOption.Text);
2125

22-
public string SelectedTextByJs => throw new NotImplementedException();
26+
public string SelectedTextByJs => JsActions.GetSelectedText();
2327

24-
public IList<string> Values => throw new NotImplementedException();
28+
public IList<string> Values
29+
{
30+
get
31+
{
32+
Logger.InfoLoc("loc.combobox.get.values");
33+
return ConditionalWait.WaitFor(driver =>
34+
{
35+
var options = new SelectElement(GetElement()).Options;
36+
return options.Select(option => string.IsNullOrEmpty(option.Text) ? option.GetAttribute(Attributes.Value) : option.Text).ToList();
37+
});
38+
}
39+
}
2540

26-
ComboBoxJsActions IComboBox.JsActions => new ComboBoxJsActions(this, ElementType);
41+
public new ComboBoxJsActions JsActions => new ComboBoxJsActions(this, ElementType);
2742

28-
public void SelectByContainingText(string partialText)
43+
public void SelectOptionThatContainsText(string text)
2944
{
30-
throw new NotImplementedException();
45+
Logger.InfoLoc("loc.selecting.value");
46+
ConditionalWait.WaitFor(driver =>
47+
{
48+
var select = new SelectElement(GetElement());
49+
foreach (var element in select.Options)
50+
{
51+
var elementText = element.Text;
52+
if (elementText.ToLower().Contains(text.ToLower()))
53+
{
54+
select.SelectByText(elementText);
55+
return true;
56+
}
57+
}
58+
return false;
59+
});
3160
}
3261

33-
public void SelectByContainingValue(string partialValue)
62+
public void SelectOptionThatContainsValue(string value)
3463
{
35-
throw new NotImplementedException();
64+
Logger.InfoLoc("loc.selecting.value");
65+
ConditionalWait.WaitFor(driver =>
66+
{
67+
var select = new SelectElement(GetElement());
68+
foreach (var element in select.Options)
69+
{
70+
var elementValue = element.GetAttribute(Attributes.Value);
71+
if (elementValue.ToLower().Contains(value.ToLower()))
72+
{
73+
select.SelectByValue(elementValue);
74+
return true;
75+
}
76+
}
77+
return false;
78+
});
3679
}
3780

3881
public void SelectByIndex(int index)
3982
{
40-
throw new NotImplementedException();
83+
Logger.InfoLoc("loc.selecting.value");
84+
ConditionalWait.WaitFor(driver =>
85+
{
86+
new SelectElement(GetElement()).SelectByIndex(index);
87+
return true;
88+
});
4189
}
4290

4391
public void SelectByText(string text)
4492
{
45-
throw new NotImplementedException();
93+
Logger.InfoLoc("loc.selecting.value");
94+
ConditionalWait.WaitFor(driver =>
95+
{
96+
new SelectElement(GetElement()).SelectByText(text);
97+
return true;
98+
});
4699
}
47100

48101
public void SelectByValue(string value)
49102
{
50-
throw new NotImplementedException();
103+
Logger.InfoLoc("loc.selecting.value");
104+
ConditionalWait.WaitFor(driver =>
105+
{
106+
new SelectElement(GetElement()).SelectByValue(value);
107+
return true;
108+
});
51109
}
52110
}
53111
}

Aquality.Selenium/src/Aquality.Selenium/Elements/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ private Attributes()
1616
}
1717

1818
public const string Class = "class";
19+
public const string Value = "value";
1920
}
2021
}

Aquality.Selenium/src/Aquality.Selenium/Elements/Element.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected Element(By locator, string name, ElementState state)
3535

3636
public IElementStateProvider State => new ElementStateProvider(Locator);
3737

38-
private Logger Logger => Logger.Instance;
38+
protected Logger Logger => Logger.Instance;
3939

4040
private Browser Browser => BrowserManager.Browser;
4141

Aquality.Selenium/src/Aquality.Selenium/Elements/Interfaces/IComboBox.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public interface IComboBox : IElement
5353
/// <summary>
5454
/// Selects option by partial text.
5555
/// </summary>
56-
/// <param name="partialText">Partial text of option.</param>
57-
void SelectByContainingText(string partialText);
56+
/// <param name="text">Partial text of option.</param>
57+
void SelectOptionThatContainsText(string text);
5858

5959
/// <summary>
6060
/// Selects option by partial value.
6161
/// </summary>
62-
/// <param name="partialValue">Partial value of option.</param>
63-
void SelectByContainingValue(string partialValue);
62+
/// <param name="value">Partial value of option.</param>
63+
void SelectOptionThatContainsValue(string value);
6464
}
6565
}

Aquality.Selenium/src/Aquality.Selenium/Elements/Label.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Elements.Interfaces;
2+
using Aquality.Selenium.Localization;
23
using OpenQA.Selenium;
3-
using System;
44

55
namespace Aquality.Selenium.Elements
66
{
@@ -13,6 +13,6 @@ protected internal Label(By locator, string name, ElementState state) : base(loc
1313
{
1414
}
1515

16-
protected override string ElementType => throw new NotImplementedException();
16+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.label");
1717
}
1818
}

Aquality.Selenium/src/Aquality.Selenium/Elements/Link.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Elements.Interfaces;
2+
using Aquality.Selenium.Localization;
23
using OpenQA.Selenium;
3-
using System;
44

55
namespace Aquality.Selenium.Elements
66
{
@@ -13,8 +13,8 @@ protected internal Link(By locator, string name, ElementState state) : base(loca
1313
{
1414
}
1515

16-
protected override string ElementType => throw new NotImplementedException();
16+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.link");
1717

18-
public string Href => throw new NotImplementedException();
18+
public string Href => GetAttribute("href");
1919
}
2020
}

Aquality.Selenium/src/Aquality.Selenium/Elements/RadioButton.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Elements.Interfaces;
2+
using Aquality.Selenium.Localization;
23
using OpenQA.Selenium;
3-
using System;
44

55
namespace Aquality.Selenium.Elements
66
{
@@ -13,8 +13,8 @@ protected internal RadioButton(By locator, string name, ElementState state) : ba
1313
{
1414
}
1515

16-
public bool IsChecked => throw new NotImplementedException();
16+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.radio");
1717

18-
protected override string ElementType => throw new NotImplementedException();
18+
public bool IsChecked => GetElement().Selected;
1919
}
2020
}
Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Aquality.Selenium.Elements.Interfaces;
2+
using Aquality.Selenium.Localization;
3+
using Aquality.Selenium.Logging;
4+
using Aquality.Selenium.Waitings;
25
using OpenQA.Selenium;
3-
using System;
46

57
namespace Aquality.Selenium.Elements
68
{
@@ -9,27 +11,77 @@ namespace Aquality.Selenium.Elements
911
/// </summary>
1012
public class TextBox : Element, ITextBox
1113
{
14+
private const string SecretMask = "*********";
15+
1216
protected internal TextBox(By locator, string name, ElementState state) : base(locator, name, state)
1317
{
1418
}
1519

16-
protected override string ElementType => throw new NotImplementedException();
20+
protected override string ElementType => LocalizationManager.Instance.GetLocalizedMessage("loc.text.field");
1721

18-
public string Value => throw new NotImplementedException();
22+
public string Value => GetAttribute(Attributes.Value);
1923

2024
public void Type(string value, bool secret = false)
2125
{
22-
throw new NotImplementedException();
26+
Logger.InfoLoc("loc.text.typing", secret ? SecretMask : value);
27+
JsActions.HighlightElement();
28+
ConditionalWait.WaitFor(driver =>
29+
{
30+
try
31+
{
32+
GetElement().SendKeys(value);
33+
return true;
34+
} catch (WebDriverException ex)
35+
{
36+
Logger.Debug(ex.Message);
37+
return false;
38+
}
39+
});
2340
}
2441

2542
public void ClearAndType(string value, bool secret = false)
2643
{
27-
throw new NotImplementedException();
44+
Logger.InfoLoc("loc.text.clearing");
45+
Logger.InfoLoc("loc.text.typing", secret ? SecretMask : value);
46+
JsActions.HighlightElement();
47+
ConditionalWait.WaitFor(driver =>
48+
{
49+
try
50+
{
51+
GetElement().Clear();
52+
GetElement().SendKeys(value);
53+
return true;
54+
} catch (WebDriverException ex)
55+
{
56+
Logger.Debug(ex.Message);
57+
return false;
58+
}
59+
});
2860
}
2961

3062
public void Submit()
3163
{
32-
throw new NotImplementedException();
64+
ConditionalWait.WaitFor(driver =>
65+
{
66+
try
67+
{
68+
GetElement().Submit();
69+
return true;
70+
} catch (WebDriverException ex)
71+
{
72+
Logger.Debug(ex.Message);
73+
return false;
74+
}
75+
});
76+
}
77+
78+
public new void Focus()
79+
{
80+
ConditionalWait.WaitFor(driver =>
81+
{
82+
GetElement().SendKeys(string.Empty);
83+
return true;
84+
});
3385
}
3486
}
3587
}

0 commit comments

Comments
 (0)