-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromeAbstract.cs
96 lines (83 loc) · 2.59 KB
/
ChromeAbstract.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
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace dSelenium
{
abstract class ChromeAbstract : IDisposable
{
internal ChromeAbstract(bool headLess)
{
Driver = GetChromeDriver(headLess);
}
protected RemoteWebDriver GetChromeDriver(bool headLess)
{
var options = new ChromeOptions();
if (headLess)
{
options.AddArguments(new List<string>() { "headless" });
}
return new ChromeDriver(ChromeDriverService.CreateDefaultService(), options);
}
protected void GoToUrl(string url)
{
Driver.Navigate().GoToUrl(url);
}
protected string GetElementTextById(string id)
{
var item = Driver.FindElementById(id);
if (item == null)
{
Console.WriteLine($"Element with ID: {id} - not found");
return null;
}
return item.Text;
}
protected IWebElement GetElementByClass(string className)
{
return Driver.FindElementByClassName(className);
}
protected string GetElementTextByClass(string className)
{
var item = Driver.FindElementsByClassName(className).FirstOrDefault();
if (item == null)
{
Console.WriteLine($"Element with Class: {className} - not found");
return null;
}
return item.Text;
}
protected void ClickElementByClass(string className, Func<IWebElement, bool> whereFunc)
{
var item = Driver.FindElementsByClassName(className).Where(whereFunc).FirstOrDefault();
if (item == null)
{
Console.WriteLine($"Element with Class: {className} - not found");
return;
}
item.Click();
}
protected void Write(string targetFile, string content)
{
var fileInfo = new FileInfo(targetFile);
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
File.WriteAllText(fileInfo.FullName, content);
}
protected string Source() { return Driver.PageSource; }
public void Dispose()
{
Driver.Quit();
}
~ChromeAbstract()
{
Dispose();
}
protected readonly RemoteWebDriver Driver;
}
}