-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllJSuntility
More file actions
340 lines (311 loc) · 12.7 KB
/
Copy pathAllJSuntility
File metadata and controls
340 lines (311 loc) · 12.7 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
import java.lang.Exception;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Keys;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
public class UtilityFunctions extends TestsigmaCustomFunctions{
@CustomTestStep
public TestStepResult jsExecutorDoubleClick(String locator){
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
WebElement element=driver.findElement(By.xpath(locator));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("var evt = document.createEvent('MouseEvents');"+ "evt.initMouseEvent('dblclick',true, true, window, 0,0, 0, 0, 0, false, false, false, false, 0,null);"+ "arguments[0].dispatchEvent(evt);", element);
result.setMessage("Javascript Executor Double click success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("Javascript Executor Double click failed");
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult jsExecutorMouseClick(String locator){
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
WebElement element=driver.findElement(By.xpath(locator));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].dispatchEvent(new MouseEvent('click'))", element);
result.setMessage("Javascript Executor Mouse click success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("Javascript Executor Mouse click failed");
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult jsExecutorRightClick(String locator){
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("var ele = document.evaluate(//div[@class='package']//div[@class='task-name' and text()='package1 '], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;var e = ele.ownerDocument.createEvent('MouseEvents');e.initMouseEvent('contextmenu', true, true, ele.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 2, null);!ele.dispatchEvent(e);");
result.setMessage("Javascript Executor Right click passed");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("Javascript Executor Right click failed"+e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult shadowElementClick(String JsQuery) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
WebElement shadowElement = (WebElement) js.executeScript(JsQuery);
shadowElement.click();
result.setMessage("shadowElementClick success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("shadowElementClick failed"+e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult shadowElementGetText(String JsQuery) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
WebElement shadowElement = (WebElement) js.executeScript(JsQuery);
String text= shadowElement.getText();
result.setMessage("shadowElement get text success" +text);
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("shadowElement get text failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult shadowElementValidateText(String JsQuery,String ExpectedResult) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
WebElement shadowElement = (WebElement) js.executeScript(JsQuery);
String actualText= shadowElement.getText();
if(ExpectedResult.equals(actualText)){
result.setMessage("Actual result is matched with expected result" +actualText);
result.setStatus(ResultConstants.SUCCESS);
}
else{
result.setMessage("Actual result is not matched with expected result"+actualText);
result.setStatus(ResultConstants.FAILURE);
}
}catch(Exception e){
result.setMessage("shadowElement get text failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult shadowElementAttachFile(String JsQuery,String FilePath) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
String parentWindow= driver.getWindowHandle();
/* driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(FilePath);
Thread.sleep(10000);*/
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(FilePath);
Thread.sleep(10000);
driver.switchTo().window(tabs.get(0));
//driver.switchTo().window(parentWindow);
JavascriptExecutor js=(JavascriptExecutor)driver;
WebElement shadowElement = (WebElement) js.executeScript(JsQuery);
shadowElement.sendKeys("C:\\Users\\Administrator\\Downloads\\mark-potterton-sNVkn3507Oo-unsplash.jpg");
result.setMessage("shadowElementAttachFile success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("shadowElementAttachFile failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult shadowElementAttachFileFromLocal(String JsQuery,String FilePath) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
WebElement shadowElement = (WebElement) js.executeScript(JsQuery);
shadowElement.sendKeys(FilePath);
result.setMessage("shadowElementAttachFileFromLocal success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("shadowElementAttachFileFromLocal failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult SelectDate(String Month, int Year, String Day) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("return document.querySelector('div.picker-columns.sc-ion-picker-md').scrollTop(500)");
WebElement month = (WebElement) js.executeScript("return document.querySelector('ion-picker-column.sc-ion-picker-md.md.picker-col.picker-opts-right.hydrated > div > button:nth-child(9)')");
WebElement date = (WebElement) js.executeScript("return document.querySelector('div.picker-columns.sc-ion-picker-md > ion-picker-column:nth-child(3) > div > button:nth-child(27)')");
int dif = 2021 - Year;
String yr = String.valueOf(dif);
WebElement year = (WebElement) js.executeScript("return document.querySelector('ion-picker-column.sc-ion-picker-md.md.picker-col.picker-opts-left.hydrated > div > button:nth-child(2)')");
month.click();
//date.click();
//year.click();
result.setMessage("SelectDate success");
result.setStatus(ResultConstants.SUCCESS);
}catch(Exception e){
result.setMessage("SelectDate failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult SelectDateFromDatePicker(int Month, int Year, int Date) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
WebElement month = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[1]"));
WebElement date = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[2]"));
WebElement year = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[3]"));
int actMonth = Integer.valueOf(month.getAttribute("opt-index"));
if(actMonth < Month-1){
while(actMonth != Month-1){
WebElement NextMonth = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[1]/following::button[1]"));
NextMonth.click();
actMonth = Integer.valueOf(NextMonth.getAttribute("opt-index"));
if(actMonth==Month-1){
result.setMessage("Month Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
if(actMonth > Month-1){
while(actMonth != Month-1){
WebElement NextMonth = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[1]/preceding::button[1]"));
NextMonth.click();
actMonth = Integer.valueOf(NextMonth.getAttribute("opt-index"));
if(actMonth==Month-1){
result.setMessage("Month Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
int actDay = Integer.valueOf(date.getAttribute("opt-index"));
if(actDay < Date-1){
while(actDay != Date-1){
WebElement Nextdate = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[2]/following::button[1]"));
Nextdate.click();
actDay = Integer.valueOf(Nextdate.getAttribute("opt-index"));
if(actDay==Date-1){
result.setMessage("Date Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
if(actDay > Date-1){
while(actDay != Date-1){
WebElement Nextdate = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[2]/preceding::button[1]"));
Nextdate.click();
actDay = Integer.valueOf(Nextdate.getAttribute("opt-index"));
if(actDay==Date-1){
result.setMessage("Date Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
int actyear = Integer.valueOf(year.getText());
if(actyear > Year){
while(actyear != Year){
WebElement NextYear = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[3]/following::button[1]"));
NextYear.click();
actyear = Integer.valueOf(NextYear.getText());
if(actyear==Year){
result.setMessage("Year Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
if(actyear < Year){
while(actyear != Year){
WebElement NextYear = driver.findElement(By.xpath("(//button[@class='picker-opt picker-opt-selected'])[3]/preceding::button[1]"));
NextYear.click();
actyear = Integer.valueOf(NextYear.getText());
if(actyear==Year){
result.setMessage("Year Selection success");
result.setStatus(ResultConstants.SUCCESS);
break;
}
}
}
}catch(Exception e){
result.setMessage("SelectDateFromDatePicker failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
finally{
return result;
}
}
@CustomTestStep
public TestStepResult SelectDateFromDatePicker2(int Month, int Year, int Date) {
//your custom code starts here
TestStepResult result = new TestStepResult();
try{
Path path=Paths.get("testsigma-storage:/livbim.io/uploads/11/21/dnd_for_testsigma.js");
String content = new String(Files.readAllBytes(path));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(content);
result.setMessage("Javascript Executor external script run success");
result.setStatus(ResultConstants.SUCCESS);
}
catch(Exception e){
result.setMessage("SelectDateFromDatePicker failed" +e);
result.setStatus(ResultConstants.FAILURE);
}
return result;
}
}