-
Notifications
You must be signed in to change notification settings - Fork 1
/
PageBase.cs
255 lines (217 loc) · 8.48 KB
/
PageBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using AutomacaoBDD.Functions;
using AutomacaoBDD.Helpers;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutomacaoBDD.Helpers
{
public class PageBase
{
protected WebDriverWait wait { get; private set; }
protected IWebDriver driver { get; private set; }
protected IJavaScriptExecutor javaScript { get; private set; }
public PageBase()
{
PageFactory.InitElements(DriverFactory.INSTANCE, this);
wait = new WebDriverWait(DriverFactory.INSTANCE, TimeSpan.FromSeconds(Convert.ToDouble(ConfigurationManager.AppSettings["DEFAULT_TIMEOUT"])));
driver = DriverFactory.INSTANCE;
javaScript = (IJavaScriptExecutor)driver;
}
public void Refresh()
{
DriverFactory.INSTANCE.Navigate().Refresh();
}
public void NavigateTo(string url)
{
DriverFactory.INSTANCE.Navigate().GoToUrl(url);
}
public void SendKeysJavaScript(IWebElement element, string value)
{
javaScript.ExecuteScript("arguments[0].setAttribute=('value', arguments[1])", element, value);
}
public void SelectByTextJavaScript(IWebElement element, string value)
{
javaScript.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, value);
}
public static bool Exists(By locator)
{
return WaitUntil.ElementExists(locator);
}
public static void Click(IWebElement element)
{
//IWebElement e = WaitUntil.ElementToBeClickable(element);
//GeneralHelpers.ScrollTo(GetElement(element));
//new Actions(DriverFactory.INSTANCE).MoveToElement(e).Click().Perform();
GetElement(element).Click();
}
public static void Click(By locator)
{
//IWebElement e = WaitUntil.ElementToBeClickable(locator);
//GeneralHelpers.ScrollTo(GetElement(locator));
//new Actions(DriverFactory.INSTANCE).MoveToElement(e).Click().Perform();
GetElement(locator).Click();
}
public static void SendKey(IWebElement element, string value)
{
//WaitUntil.ElementToBeClickable(element).SendKeys(value + OpenQA.Selenium.Keys.Tab);
element.SendKeys(value + OpenQA.Selenium.Keys.Tab);
}
public static void SendKeyInt(IWebElement element, int value)
{
//WaitUntil.ElementToBeClickable(element).SendKeys(value + OpenQA.Selenium.Keys.Tab);
element.SendKeys(value + OpenQA.Selenium.Keys.Tab);
}
public static void SendKeyDouble(IWebElement element, double value)
{
//WaitUntil.ElementToBeClickable(element).SendKeys(value + OpenQA.Selenium.Keys.Tab);
element.SendKeys(value + OpenQA.Selenium.Keys.Tab);
}
public static void Type(By locator, string value)
{
//WaitUntil.ElementToBeClickable(locator).SendKeys(value + OpenQA.Selenium.Keys.Tab);
GetElement(locator).SendKeys(value + OpenQA.Selenium.Keys.Tab);
}
public static void Switch()
{
DriverFactory.INSTANCE.SwitchTo().Window(DriverFactory.INSTANCE.WindowHandles.Last());
}
public static void RetornaPrimeiraPagina()
{
DriverFactory.INSTANCE.SwitchTo().Window(DriverFactory.INSTANCE.WindowHandles.First());
}
public static IWebElement GetElement(IWebElement element)
{
//return WaitUntil.ElementToBeClickable(element);
return element;
}
public static IWebElement GetElement(By locator)
{
return WaitUntil.ElementToBeClickable(locator);
}
public static IWebElement GetElementOculto(By locator)
{
return WaitUntil.ElementToBeClickable(locator);
}
public static string GetElementText(IWebElement element)
{
//return WaitUntil.ElementToBeClickable(element).GetAttribute("Text");
return element.GetAttribute("Text");
}
public static string GetElementTextValue(IWebElement element)
{
//return WaitUntil.ElementToBeClickable(element).GetAttribute("Value");
return element.GetAttribute("Value");
}
public static string RetornaTextLabel(By element)
{
return DriverFactory.INSTANCE.FindElement(element).Text;
}
public static bool ValidaElementTextLabel(By element, string value)
{
//return WaitUntil.ElementToBeVisible(element).Text.Contains(value);
return GetElement(element).Text.Contains(value);
}
public static void Wait(IWebElement element)
{
WaitUntil.ElementToBeClickable(element);
}
public static void Wait(By locator)
{
WaitUntil.ElementToBeClickable(locator);
}
public static void Clear(IWebElement element)
{
//WaitUntil.ElementToBeClickable(element).Clear();
element.Clear();
}
public static void Clear(By locator)
{
//WaitUntil.ElementToBeClickable(locator).Clear();
GetElement(locator).Clear();
}
public static void Select(IWebElement element, string value)
{
new SelectElement(GetElement(element)).SelectByText(value);
}
public static void SelectValue(IWebElement element, string value)
{
new SelectElement(GetElement(element)).SelectByValue(value);
}
public static void SelectIndex(IWebElement element, int value)
{
new SelectElement(GetElement(element)).SelectByIndex(value);
}
public static void Select(By locator, string value)
{
new SelectElement(GetElement(locator)).SelectByText(value);
}
public static string GetSelectedText(IWebElement element)
{
return new SelectElement(GetElement(element)).SelectedOption.Text;
}
public static string GetSelectedText(By locator)
{
return new SelectElement(GetElement(locator)).SelectedOption.Text;
}
public static List<string> GetAllOptionsCombo(By locator)
{
SelectElement oSelect = new SelectElement(GetElement(locator));
IList<IWebElement> elementCount = oSelect.Options;
List<string> allOptions = new List<string>();
int iSize = elementCount.Count;
for (int i = 0; i < iSize; i++)
{
allOptions.Add(elementCount.ElementAt(i).Text);
}
return allOptions;
}
public static List<string> GetAllOptionsComboValue(By locator)
{
IWebElement drop_down = DriverFactory.INSTANCE.FindElement(locator);
SelectElement se = new SelectElement(drop_down);
List<string> allOptions = new List<string>();
for (int i = 0; i < se.Options.Count; i++)
allOptions.Add(se.Options.ElementAt(i).GetAttribute("value"));
return allOptions;
}
public static bool IsEnabled(By element)
{
//return WaitUntil.ElementToBeVisible(element).Enabled;
return GetElement(element).Enabled;
}
public static bool IsVisible(By element)
{
return WaitUntil.ElementExists(element);
}
public static bool IsInvisibilit(By element)
{
return WaitUntil.ElementisInvisibility(element);
}
public static bool CampoOculto(By element)
{
var style = DriverFactory.INSTANCE.FindElement(element).GetAttribute("style");
if (style.Contains("display: none;"))
{
return true;
}
else
{
return false;
}
}
public static int Random(int de, int ate)
{
return new Random().Next(de, ate);
}
}
}