-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyExcelFrameworkSelenium.cs
More file actions
663 lines (645 loc) · 26.1 KB
/
EasyExcelFrameworkSelenium.cs
File metadata and controls
663 lines (645 loc) · 26.1 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
using EasyExcelFramework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Data;
namespace EasyExcelFrameworkSelenium
{
public class EasyExcelFrameworkSelenium
{
public EasyExcelF EasyExcel;
public IWebDriver? driver;
private Wait? waitclass;
public enum Elewaitconditions { CLICKABLE }
public enum Bywaitconditions { VISIBLE, EXISTS }
public EasyExcelFrameworkSelenium(string filename)
{
//Implement Constructor for Framework Extension
this.EasyExcel = new EasyExcelF(filename);
Init();
}
private void Init()
{
EasyExcel.RegisterScreenShot(Screenshot);
EasyExcel.RegisterMethod("LAUNCH", Launchbrowser);
EasyExcel.RegisterMethod("URL", Gotourl);
EasyExcel.RegisterMethod("GET BROWSER CAPABILITIES", Getbrowsercapabilities);
EasyExcel.RegisterMethod("GET URL", Geturl);
EasyExcel.RegisterMethod("GET ATTRIBUTE", Getattribute);
EasyExcel.RegisterMethod("IS SELECTED", Isselected);
EasyExcel.RegisterMethod("GET SELECTION", Getselected);
EasyExcel.RegisterMethod("ACCEPT ALERT", Acceptalert);
EasyExcel.RegisterMethod("DISMISS ALERT", Dismisalert);
EasyExcel.RegisterMethod("CLICK", Click);
EasyExcel.RegisterMethod("TYPE", Type);
EasyExcel.RegisterMethod("CLEAR", Clear);
EasyExcel.RegisterMethod("SUBMIT", Submit);
EasyExcel.RegisterMethod("CHECK", Check);
EasyExcel.RegisterMethod("UNCHECK", Uncheck);
EasyExcel.RegisterMethod("SELECT BY TEXT", Selectbytext);
EasyExcel.RegisterMethod("SELECT BY VALUE", Selectbyvalue);
EasyExcel.RegisterMethod("SELECT BY INDEX", Selectbyindex);
EasyExcel.RegisterMethod("DESELECT BY TEXT", Deselectbytext);
EasyExcel.RegisterMethod("DESELECT BY VALUE", Deselectbyvalue);
EasyExcel.RegisterMethod("DESELECT BY INDEX", Deselectbyindex);
EasyExcel.RegisterMethod("SELECT ALL", Selectall);
EasyExcel.RegisterMethod("DESELECT ALL", Deselectall);
EasyExcel.RegisterMethod("WAIT UNTIL ALERT IS PRESENT", Waituntilalertpresent);
EasyExcel.RegisterMethod("WAIT UNTIL ELEMENT EXISTS", Waituntilcontrolexists);
EasyExcel.RegisterMethod("WAIT UNTIL ELEMENT VISIBLE", Waituntilcontrolvisible);
EasyExcel.RegisterMethod("WAIT UNTIL ELEMENT CLICKABLE", Waituntilcontrolclickable);
EasyExcel.RegisterMethod("SWITCH FRAME DEFAULT", Switchframedefault);
EasyExcel.RegisterMethod("SWITCH FRAME BY INDEX", Switchframebyname_id_index);
EasyExcel.RegisterMethod("SWITCH FRAME BY NAME", Switchframebyname_id_index);
EasyExcel.RegisterMethod("SWITCH FRAME BY ID", Switchframebyname_id_index);
EasyExcel.RegisterMethod("SWITCH FRAME BY ELEMENT", Switchframebyelement);
EasyExcel.RegisterMethod("SWITCH FRAME PARENT", Switchframeparent);
}
private bool Launchbrowser(EasyExcelF ee, dynamic[] parms)
{
string targetbrowser;
int timeout=60;
if (ee.Globals.ContainsKey("__EEF__TIMEOUT"))
{
timeout = (int)ee.Globals["__EEF__TIMEOUT"];
}
try
{
targetbrowser = ee.Interpreter.EvalToString(ee, parms[0], parms);
}
catch
{
targetbrowser = parms[0];
}
switch (targetbrowser.ToUpper())
{
case "CHROME":
goto default;
case "CHROME HEADLESS":
ChromeOptions headlesschromeopts = new();
headlesschromeopts.AddArgument("--headless");
headlesschromeopts.AddArgument("--disable-gpu");
foreach (string opt in parms[1..])
{
if (!string.IsNullOrEmpty(opt))
headlesschromeopts.AddArgument(opt);
}
driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), headlesschromeopts, TimeSpan.FromMinutes(3));
break;
case "EDGE":
driver = new EdgeDriver();
break;
case "FIREFOX":
driver = new FirefoxDriver();
break;
default:
ChromeOptions chromeopts = new();
foreach (string opt in parms[1..])
{
if (!string.IsNullOrEmpty(opt))
chromeopts.AddArgument(opt);
}
driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), chromeopts, TimeSpan.FromMinutes(3));
break;
}
_ = driver.Manage().Timeouts().PageLoad.Add(TimeSpan.FromSeconds(timeout));
waitclass = new Wait(driver);
return true;
}
private bool Getbrowsercapabilities(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
ICapabilities cap = ((ChromeDriver)driver).Capabilities;
ee.Locals[parms[1]] = cap.GetCapability(parms[0]);
return true;
}
private bool Gotourl(EasyExcelF ee, dynamic[] parms)
{
if (driver==null || waitclass==null)
{
throw new InvalidOperationException("Browser is in open");
}
string targ;
try
{
targ = ee.Interpreter.EvalToString(ee, parms[0], parms);
}
catch
{
targ = parms[0];
}
driver.Navigate().GoToUrl(targ);
return true;
}
private bool Geturl(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
ee.Locals[parms[0]] = driver.Url.ToString();
return true;
}
private bool Click(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (ele != null)
{
ele.Click();
}
else
{
throw new Exception("Unable to find element: " + parms[0]);
}
return true;
}
private bool Submit(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (ele != null)
{
ele.Submit();
}
else
{
throw new Exception("Unable to find element: " + parms[0]);
}
return true;
}
private bool Type(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (ele != null)
{
string text;
try
{
text = ee.Interpreter.EvalToString(ee, parms[1], parms);
}
catch
{
text = parms[1];
}
ele.SendKeys(text);
}
else
{
throw new Exception("Unable to find element: " + parms[0]);
}
return true;
}
private bool Clear(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (ele != null)
{
ele.Clear();
}
else
{
throw new Exception("Unable to find element: " + parms[0]);
}
return true;
}
private bool Acceptalert(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
waitclass.Waitalert(Wait.Timeout(parms[0]));
driver.SwitchTo().Alert().Accept();
return true;
}
private bool Dismisalert(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
waitclass.Waitalert(Wait.Timeout(parms[0]));
driver.SwitchTo().Alert().Dismiss();
return true;
}
private bool Check(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (ele.Selected)
return true;
else
ele.Click();
return true;
}
private bool Uncheck(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Elewait(lc, Elewaitconditions.CLICKABLE);
if (!ele.Selected)
return true;
else
ele.Click();
return true;
}
private bool Getattribute(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
ee.Locals[parms[2]] = ele.GetAttribute(parms[1]);
return true;
}
private bool Isselected(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
ee.Locals[parms[1]] = ele.Selected.ToString().ToLower();
return true;
}
private bool Selectbytext(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.SelectByText(parms[1]);
return true;
}
private bool Selectbyvalue(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.SelectByValue(parms[1]);
return true;
}
private bool Selectbyindex(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.SelectByIndex(int.Parse(parms[1]));
return true;
}
private bool Selectall(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
for (int i = 0; i < selectele.Options.Count; i++)
{
selectele.SelectByIndex(i);
}
return true;
}
private bool Getselected(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
string result = "";
for (int i = 0; i < selectele.Options.Count; i++)
{
if (selectele.Options[i].Selected)
{
if (string.IsNullOrEmpty(result))
{
result = selectele.Options[i].Text.ToString();
}
else
{
result += "|" + selectele.Options[i].Text.ToString();
}
}
}
ee.Locals[parms[1]] = result;
return true;
}
private bool Deselectall(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.DeselectAll();
return true;
}
private bool Deselectbytext(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.DeselectByText(parms[1]);
return true;
}
private bool Deselectbyvalue(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.DeselectByValue(parms[1]);
return true;
}
private bool Deselectbyindex(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
IWebElement ele = waitclass.Bywait(lc, Bywaitconditions.EXISTS);
var selectele = new SelectElement(ele);
selectele.DeselectByIndex(int.Parse(parms[1]));
return true;
}
private bool Waituntilalertpresent(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
waitclass.Waitalert(Wait.Timeout(parms[0]));
return true;
}
private bool Waituntilcontrolexists(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
waitclass.Bywait(lc, Bywaitconditions.EXISTS, Wait.Timeout(parms[0]));
return true;
}
private bool Waituntilcontrolvisible(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
waitclass.Bywait(lc, Bywaitconditions.VISIBLE, Wait.Timeout(parms[0]));
return true;
}
private bool Waituntilcontrolclickable(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
waitclass.Elewait(lc, Elewaitconditions.CLICKABLE, Wait.Timeout(parms[0]));
return true;
}
private bool Switchframedefault(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
driver.SwitchTo().DefaultContent();
return true;
}
private bool Switchframebyname_id_index(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
if (int.TryParse((string)parms[0], out _))
driver.SwitchTo().Frame(int.Parse(parms[0]));
else
driver.SwitchTo().Frame(parms[0]);
return true;
}
private bool Switchframebyelement(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
Locator lc = new(ee, this.driver, parms[0]);
driver.SwitchTo().Frame(waitclass.Bywait(lc, Bywaitconditions.EXISTS));
return true;
}
private bool Switchframeparent(EasyExcelF ee, dynamic[] parms)
{
if (driver == null || waitclass == null)
{
throw new InvalidOperationException("Browser is in open");
}
driver.SwitchTo().ParentFrame();
return true;
}
public class Locator
{
public string original;
public List<IWebElement> targets;
public List<By> bys;
public Locator(EasyExcelF ee, IWebDriver driver, string loc)
{
original = loc;
targets = new List<IWebElement>();
List<string> targstrs = new();
bys = new List<By>();
if (ee.Worksheets.ContainsKey("@Locators"))
{
string col1 = ee.Worksheets["@Locators"].Columns[0].ColumnName;
DataRow[] rows = ee.Worksheets["@Locators"].Select(col1 + " Like'" + loc + "'");
foreach (DataRow row in rows)
{
foreach (string col in row.ItemArray.Cast<string>())
{
targstrs.Add(col.ToString());
}
}
}
else
{
foreach (var str in loc.Split("|"))
{
targstrs.Add(str);
}
}
if (targstrs.Count == 0)
throw new IndexOutOfRangeException("Cannot have blank target");
foreach (string targstr in targstrs)
{
char[] c = { ':' };
string[] locs = targstr.Split(c, 2);
switch (locs[0].ToUpper())
{
case "ID":
bys.Add(By.Id(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.Id(locs[1])))
{
targets.Add(ele);
}
break;
case "NAME":
bys.Add(By.Name(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.Name(locs[1])))
{
targets.Add(ele);
}
break;
case "CLASSNAME":
bys.Add(By.ClassName(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.ClassName(locs[1])))
{
targets.Add(ele);
}
break;
case "CSS":
bys.Add(By.CssSelector(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.CssSelector(locs[1])))
{
targets.Add(ele);
}
break;
case "XPATH":
bys.Add(By.XPath(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.XPath(locs[1])))
{
targets.Add(ele);
}
break;
case "LINKTEXT":
case "LINK":
bys.Add(By.LinkText(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.LinkText(locs[1])))
{
targets.Add(ele);
}
break;
case "PARTIALLINKTEXT":
case "PARTIALLINK":
bys.Add(By.PartialLinkText(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.PartialLinkText(locs[1])))
{
targets.Add(ele);
}
break;
case "LABEL":
bys.Add(By.XPath("//label[text()='" + locs[1] + "']/following-sibling::input"));
foreach (IWebElement ele in driver.FindElements(By.XPath("//label[text()='" + locs[1] + "']/following-sibling::input")))
{
targets.Add(ele);
}
break;
case "TEXT":
bys.Add(By.XPath("//*[text()='" + locs[1] + "']"));
foreach (IWebElement ele in driver.FindElements(By.XPath("//*[text()='" + locs[1] + "']")))
{
targets.Add(ele);
}
break;
case "TAGNAME":
bys.Add(By.TagName(locs[1]));
foreach (IWebElement ele in driver.FindElements(By.TagName(locs[1])))
{
targets.Add(ele);
}
break;
default:
if (locs[0].Substring(0, 1) == "/" | locs[0].Substring(0, 1) == "(")
{
bys.Add(By.XPath(locs[0]));
foreach (IWebElement ele in driver.FindElements(By.XPath(locs[0])))
{
targets.Add(ele);
}
}
else
{
bys.Add(By.CssSelector(locs[0]));
foreach (IWebElement ele in driver.FindElements(By.CssSelector(locs[0])))
{
targets.Add(ele);
}
}
break;
}
}
}
}
private string Screenshot(string defaultpath)
{
Screenshot TakeScreenshot = ((ITakesScreenshot)driver).GetScreenshot();
Random randNo = new();
string fname = "\\"+randNo.Next(0, 10000).ToString() + ".png";
TakeScreenshot.SaveAsFile(defaultpath+fname);
return defaultpath + fname;
}
}
}