- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselenium_java_generator.rb
More file actions
Latest commit
176 lines (136 loc) · 5.47 KB
/
Copy pathselenium_java_generator.rb
File metadata and controls
176 lines (136 loc) · 5.47 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
require'open-uri'
# use it for give vars and functions name
$counter =1000;
$array_rules_not_include =["https://","about:blank","_blank","rel=","target="]
$special_char_to_match =/[!@#$%^&*().?":{}\[\]|<>;]/
$java_class_name ="SeleniumJavaGenerator"
classSeleniumJavaGenerator
# check if string has latters
defself.letters?(string)
string.chars.any?{ |char| ('a'..'z').include?char.downcase}
end
# Create java body with selenium import libs
defself.create_java_body()
java_body="import org.openqa.selenium.JavascriptExecutor;\n"
java_body += "import org.openqa.selenium.WebDriver;\n"
java_body += "import org.openqa.selenium.WebElement;\n"
java_body += "import org.openqa.selenium.support.FindBy;\n\n\n"
java_body += "public class #{$java_class_name} {\n"
returnjava_body
end
# Read web page through url
defself.read_web_page_url(url)
open(url)do |file|
read_html_by_lines(file)
end
end
# Read web page through file
defself.read_web_page_file(file_name)
File.open(file_name,"a+")do |file|
allText=file.readlines()
read_html_by_lines(allText)
end
end
# take html line by line to read
defself.read_html_by_lines(html_source)
print_html=create_java_body()
html_source.each{ |line|
ifline.match(/(class=|id=)/)
new_java_code=create_web_element_object(line)
ifnew_java_code.length > 0
print_html += new_java_code + "\n"
end
end
}
write_java_class(print_html + "}\n")
end
# Read html line and search for id or class to create object
defself.create_web_element_object(line)
newLine=line.scan(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/)
tag_name=line.scan(/<(\w+)\s+\w+.*?>/)
java_code=""
index=0
whileindex < newLine.size
current_item=newLine[index]
index2=0
whileindex2 < current_item.size
$counter += 1
current_item2=newLine[index][index2]
ifcurrent_item2 === ("id")
java_code="//\t" + line + "\n"
java_code += check_and_create(newLine[index][index2 + 1],"id",nil)
elsifcurrent_item2 === ("class")
java_code="//\t" + line + "\n"
iftag_name[0] != nil
java_code += check_and_create(newLine[index][index2 + 1],"css",tag_name[0][0])
end
end
index2 += 1
end
index += 1
end
returnjava_code
end
defself.check_conditions(tag_check)
$array_rules_not_include.each{ |condition|
iftag_check.include?(condition)
returnfalse
end
}
returntrue
end
defself.check_and_create(item_class_name,find_type,tag_name)
collector=""
ifitem_class_name != nil
ifitem_class_name.length > 3 &&
check_conditions(item_class_name) &&
letters?(item_class_name) &&
!item_class_name.match($special_char_to_match)
# check if first char is number if return do not continue !
ifitem_class_name[0].match(/[0-9]/)
return"";
end
rename_item=item_class_name;
# for css will rename to add tag name in the first name
if(find_type == "css")
rename_item=css_element(item_class_name,tag_name)
end
### <Create on the following format>
# First: @FindBy([type]="[id | class]")
collector += "\t\t@FindBy(#{find_type}=\"#{rename_item}\")" + "\n"
# Second: Create the object and name it in camel case
camel_case_item=camel_case(item_class_name,true)
collector += "\t\tWebElement #{camel_case_item}_#{$counter};" + "\n\n"
# Third: Create function method to start click
collector += create_java_click_method(camel_case_item)
end
end
returncollector
end
defself.create_java_click_method(method_name)
method="\t\tpublic void clickOn#{camel_case(method_name,false)}_#{$counter}(){\n"
method += "\t\t\t#{method_name}_#{$counter}.click();\n"
method += "\t\t}\n\n"
returnmethod
end
# create camel case method
defself.camel_case(item_name,downcase)
# will take the string and convert to camel case
new_item_name=item_name.gsub(/\s|\-|\__|\_/,' ').split.map(&:capitalize)*''
# make first latter down case
ifdowncase
new_item_name[0]=new_item_name[0].downcase
end
returnnew_item_name
end
defself.css_element(item_name,tag_name)
returntag_name + "." + item_name.rstrip.lstrip.gsub(/(\s)+/,'.')
end
defself.write_java_class(html)
puts"... Creating Java Class ...."
File.open("./#{$java_class_name}.java","a+")do |file|
file.truncate(0)
file << html
end
end
end