-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributeBuilder.cs
135 lines (121 loc) · 4.43 KB
/
AttributeBuilder.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
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Golem.Framework.Specter;
using OpenQA.Selenium;
using OpenQA.Selenium.Internal;
namespace ProtoTest.Specter
{
internal class AttributeBuilder
{
public List<Attribute> attributes;
public IWebElement element;
public WebDriverExecutor executor;
public AttributeBuilder(IWebElement element)
{
this.element = element;
attributes = GetAllAttributes();
}
public string textString
{
get { return Specter.useText ? "text()" : "."; }
}
public IWebDriver driver
{
get { return ((IWrapsDriver) element).WrappedDriver; }
}
public List<Attribute> SplitAttribute(Attribute att)
{
var atts = new List<Attribute>();
string attValue = att.value.Trim(' ');
foreach (string val in attValue.Split(' '))
{
if ((val != "") && (val != " "))
{
atts.Add(new Attribute(att.name, val));
}
}
return atts;
}
public Attribute GetUniqueAttribute()
{
var results = new List<Attribute>();
foreach (Attribute att in attributes)
{
string xpath = string.Format("//{0}[{1}=\"{2}\"]", element.TagName, att.name, att.value);
if (driver.FindElements(By.XPath(xpath)).Count == 1)
return new Attribute(att.name, att.value);
}
return new Attribute("", "");
}
public string TrimText(string text)
{
if (text != "")
{
string finalText = text.Replace("\r\n", " ");
if (finalText.Length > Specter.maxAttLength)
{
finalText = finalText.Substring(0, Specter.maxAttLength);
}
return finalText;
}
return "";
}
public List<Attribute> GetAllAttributes()
{
attributes = new List<Attribute>();
if (!Specter.skipAttributeString.Contains("text"))
{
string text = TrimText(element.Text);
if (text != "" && text != " ")
{
attributes.Add(new Attribute(textString, text));
}
}
string html = element.GetHtml();
string nodeHtml = html.Split('>')[0];
string[] elementParts = nodeHtml.Split(' ');
string tag = elementParts[0];
nodeHtml = nodeHtml.Replace(tag, "");
string[] atts = Regex.Split(nodeHtml, "\" ");
if (atts[0].Contains("=\""))
{
for (int i = 0; i < atts.Length; i++)
{
string key = Regex.Split(atts[i], "=\"")[0].Replace("\"", "").Trim();
string value = Regex.Split(atts[i], "=\"")[1].Replace("\"", "");
if (!key.Contains("style") && !Specter.skipAttributeString.Contains(key))
{
if (key != "" && value != "")
{
if (Specter.splitAttributes)
{
List<Attribute> splitatts = SplitAttribute(new Attribute("@" + key, TrimText(value)));
attributes.AddRange(splitatts);
}
else
{
attributes.Add(new Attribute("@" + key, TrimText(value)));
}
}
}
}
}
IOrderedEnumerable<Attribute> items = from att in attributes
orderby att.name.Length ascending
select att;
return items.ToList();
}
public List<Attribute> GetUniqueAttributes()
{
var results = new List<Attribute>();
foreach (Attribute att in attributes)
{
string xpath = string.Format("//{0}[{1}=\"{2}\"]", element.TagName, att.name, att.value);
if (driver.FindElements(By.XPath(xpath)).Count == 1)
results.Add(new Attribute(att.name, att.value));
}
return results;
}
}
}