forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompoundMutator.cs
67 lines (56 loc) · 2.89 KB
/
CompoundMutator.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
using System;
using System.Collections.Generic;
using System.Text;
namespace Selenium.Internal.SeleniumEmulation
{
/// <summary>
/// A class that collects together a group of other mutators and applies
/// them in the order they've been added to any script that needs modification.
/// Any JS to be executed will be wrapped in an "eval" block so that a
/// meaningful return value can be created.
/// </summary>
internal class CompoundMutator : IScriptMutator
{
private List<IScriptMutator> mutators = new List<IScriptMutator>();
public CompoundMutator(string baseUrl)
{
this.AddMutator(new VariableDeclaration("selenium", "var selenium = {};"));
this.AddMutator(new VariableDeclaration("selenium.browserbot", "selenium.browserbot = {};"));
this.AddMutator(new VariableDeclaration("selenium.browserbot.baseUrl", "selenium.browserbot.baseUrl = '" + baseUrl + "';"));
this.AddMutator(new FunctionDeclaration("selenium.page", "if (!selenium.browserbot) { selenium.browserbot = {} }; return selenium.browserbot;"));
this.AddMutator(new FunctionDeclaration("selenium.browserbot.getCurrentWindow", "return window;"));
this.AddMutator(new FunctionDeclaration("selenium.page().getCurrentWindow", "return window;"));
this.AddMutator(new FunctionDeclaration("selenium.browserbot.getDocument", "return document;"));
this.AddMutator(new FunctionDeclaration("selenium.page().getDocument", "return document;"));
this.AddMutator(new SeleniumMutator("selenium.isElementPresent", JavaScriptLibrary.GetSeleniumScript("isElementPresent.js")));
this.AddMutator(new SeleniumMutator("selenium.isTextPresent", JavaScriptLibrary.GetSeleniumScript("isTextPresent.js")));
this.AddMutator(new SeleniumMutator("selenium.isVisible", JavaScriptLibrary.GetSeleniumScript("isVisible.js")));
this.AddMutator(new SeleniumMutator("selenium.browserbot.findElement", JavaScriptLibrary.GetSeleniumScript("findElement.js")));
}
#region IScriptMutator Members
public void Mutate(string script, StringBuilder outputTo)
{
StringBuilder nested = new StringBuilder();
foreach (IScriptMutator mutator in mutators)
{
mutator.Mutate(script, nested);
}
nested.Append("").Append(script);
outputTo.Append("return eval('");
outputTo.Append(escape(nested.ToString()));
outputTo.Append("');");
}
#endregion
internal void AddMutator(IScriptMutator mutator)
{
this.mutators.Add(mutator);
}
private string escape(String escapee)
{
return escapee
.Replace("\\", "\\\\")
.Replace("\n", "\\n")
.Replace("'", "\\'");
}
}
}