forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEvaluateParametersTest.cs
128 lines (101 loc) · 4.74 KB
/
EvaluateParametersTest.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
// <copyright file="EvaluateParametersTest.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using NUnit.Framework;
using OpenQA.Selenium.BiDi.Modules.Script;
using System.Threading.Tasks;
namespace OpenQA.Selenium.BiDi.Script;
class EvaluateParametersTest : BiDiTestFixture
{
[Test]
public async Task CanEvaluateScript()
{
var res = await context.Script.EvaluateAsync("1 + 2", false);
Assert.That(res, Is.Not.Null);
Assert.That(res.Realm, Is.Not.Null);
Assert.That((res.Result as RemoteValue.Number).Value, Is.EqualTo(3));
}
[Test]
public async Task CanEvaluateScriptImplicitCast()
{
var res = await context.Script.EvaluateAsync<int>("1 + 2", false);
Assert.That(res, Is.EqualTo(3));
}
[Test]
public async Task СanEvaluateScriptWithUserActivationTrue()
{
await context.Script.EvaluateAsync("window.open();", true);
var res = await context.Script.EvaluateAsync<bool>("""
navigator.userActivation.isActive && navigator.userActivation.hasBeenActive
""", true, new() { UserActivation = true });
Assert.That(res, Is.True);
}
[Test]
public async Task СanEvaluateScriptWithUserActivationFalse()
{
await context.Script.EvaluateAsync("window.open();", true, new() { UserActivation = true });
var res = await context.Script.EvaluateAsync<bool>("""
navigator.userActivation.isActive && navigator.userActivation.hasBeenActive
""", true, new() { UserActivation = false });
Assert.That(res, Is.False);
}
[Test]
public void CanCallFunctionThatThrowsException()
{
var action = () => context.Script.EvaluateAsync("))) !!@@## some invalid JS script (((", false);
Assert.That(action, Throws.InstanceOf<ScriptEvaluateException>().And.Message.Contain("SyntaxError:"));
}
[Test]
public async Task CanEvaluateScriptWithResulWithOwnership()
{
var res = await context.Script.EvaluateAsync("Promise.resolve({a:1})", true, new()
{
ResultOwnership = ResultOwnership.Root
});
Assert.That(res, Is.Not.Null);
Assert.That((res.Result as RemoteValue.Object).Handle, Is.Not.Null);
Assert.That((string)(res.Result as RemoteValue.Object).Value[0][0], Is.EqualTo("a"));
Assert.That((int)(res.Result as RemoteValue.Object).Value[0][1], Is.EqualTo(1));
}
[Test]
public async Task CanEvaluateInASandBox()
{
// Make changes without sandbox
await context.Script.EvaluateAsync("window.foo = 1", true);
var res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" });
Assert.That(res.Result, Is.AssignableFrom<RemoteValue.Undefined>());
// Make changes in the sandbox
await context.Script.EvaluateAsync("window.foo = 2", true, targetOptions: new() { Sandbox = "sandbox" });
// Check if the changes are present in the sandbox
res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" });
Assert.That(res.Result, Is.AssignableFrom<RemoteValue.Number>());
Assert.That((res.Result as RemoteValue.Number).Value, Is.EqualTo(2));
}
[Test]
public async Task CanEvaluateInARealm()
{
await bidi.BrowsingContext.CreateAsync(Modules.BrowsingContext.ContextType.Tab);
var realms = await bidi.Script.GetRealmsAsync();
await bidi.Script.EvaluateAsync("window.foo = 3", true, new Target.Realm(realms[0].Realm));
await bidi.Script.EvaluateAsync("window.foo = 5", true, new Target.Realm(realms[1].Realm));
var res1 = await bidi.Script.EvaluateAsync<int>("window.foo", true, new Target.Realm(realms[0].Realm));
var res2 = await bidi.Script.EvaluateAsync<int>("window.foo", true, new Target.Realm(realms[1].Realm));
Assert.That(res1, Is.EqualTo(3));
Assert.That(res2, Is.EqualTo(5));
}
}