-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPlaywrightActionValueFunction.cs
More file actions
136 lines (119 loc) · 5.74 KB
/
PlaywrightActionValueFunction.cs
File metadata and controls
136 lines (119 loc) · 5.74 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Microsoft.Extensions.Logging;
using Microsoft.Playwright;
using Microsoft.PowerApps.TestEngine.Config;
using Microsoft.PowerApps.TestEngine.System;
using Microsoft.PowerApps.TestEngine.TestInfra;
using Microsoft.PowerFx;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Types;
namespace testengine.module
{
/// <summary>
/// This will execute playwright actions for the current page
/// </summary>
public class PlaywrightActionValueFunction : ReflectionFunction
{
private readonly ITestInfraFunctions _testInfraFunctions;
private readonly ISingleTestInstanceState _singleTestInstanceState;
private readonly IFileSystem _fileSystem;
private readonly ITestState _testState;
private readonly ILogger _logger;
public PlaywrightActionValueFunction(ITestInfraFunctions testInfraFunctions, ISingleTestInstanceState singleTestInstanceState, IFileSystem fileSystem, ITestState testState, ILogger logger)
: base(DPath.Root.Append(new DName("Preview")), "PlaywrightActionValue", FormulaType.Blank, FormulaType.String, FormulaType.String, FormulaType.String)
{
_testInfraFunctions = testInfraFunctions;
_singleTestInstanceState = singleTestInstanceState;
_fileSystem = fileSystem;
_testState = testState;
_logger = logger;
}
public BooleanValue Execute(StringValue locator, StringValue action, StringValue value)
{
_logger.LogInformation("------------------------------\n\n" +
"Executing PlaywrightActionValue function.");
if (string.IsNullOrEmpty(locator.Value))
{
_logger.LogError("locator cannot be empty.");
throw new ArgumentException();
}
IPage page = _testInfraFunctions.GetContext().Pages.First();
switch (action.Value.ToLower())
{
case "click-in-iframe":
foreach (var frame in page.Frames)
{
if (frame.Locator(locator.Value).IsVisibleAsync().Result)
{
frame.Locator(locator.Value).ClickAsync(new LocatorClickOptions { Delay = 200 }).Wait();
}
}
break;
case "fill-in-iframe":
foreach (var frame in page.Frames)
{
if (frame.Locator(locator.Value).IsVisibleAsync().Result)
{
frame.Locator(locator.Value).PressSequentiallyAsync(value.Value, new LocatorPressSequentiallyOptions { Delay = 100 }).Wait();
}
}
break;
case "fill":
_testInfraFunctions.FillAsync(locator.Value, value.Value).Wait();
break;
case "press-in-iframe":
foreach (var frame in page.Frames)
{
if (frame.Locator(locator.Value).IsVisibleAsync().Result)
{
var key = string.IsNullOrEmpty(value.Value) ? "Enter" : value.Value;
frame.Locator(locator.Value).PressAsync(key).Wait();
}
}
break;
case "press":
{
var key = string.IsNullOrEmpty(value.Value) ? "Enter" : value.Value;
_testInfraFunctions.PressAsync(locator.Value, key).Wait();
}
break;
case "screenshot":
var testResultDirectory = _singleTestInstanceState.GetTestResultsDirectory();
if (!_fileSystem.Exists(testResultDirectory))
{
_logger.LogError("Test result directory needs to be set and accessible.");
throw new InvalidOperationException();
}
var fileName = value.Value;
if (string.IsNullOrEmpty(fileName))
{
_logger.LogTrace("File Name: " + nameof(fileName));
_logger.LogError("File must exist and cannot be empty.");
throw new ArgumentException();
}
if (Path.IsPathRooted(fileName))
{
_logger.LogError("Only support relative file paths");
throw new ArgumentException();
}
if (!fileName.EndsWith(".jpg") && !fileName.EndsWith(".jpeg") && !fileName.EndsWith("png"))
{
_logger.LogDebug("File extension: " + Path.GetExtension(fileName));
_logger.LogTrace("File name: " + fileName);
_logger.LogError("Only support jpeg and png files");
throw new ArgumentException();
}
var filePath = Path.Combine(testResultDirectory, fileName);
_logger.LogInformation("Screenshot item");
page.Locator(locator.Value).ScreenshotAsync(new LocatorScreenshotOptions() { Path = filePath }).Wait();
break;
default:
_logger.LogError("Action not found " + action.Value);
throw new ArgumentException();
}
_logger.LogInformation("Successfully finished executing PlaywrightActionValue function.");
return BooleanValue.New(true);
}
}
}