-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathFirefoxTest.cs
More file actions
231 lines (192 loc) · 7.41 KB
/
FirefoxTest.cs
File metadata and controls
231 lines (192 loc) · 7.41 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Internal.Logging;
namespace SeleniumDocs.Browsers
{
[TestClass]
public class FirefoxTest
{
private FirefoxDriver driver;
private string _logLocation;
private string _tempPath;
[TestCleanup]
public void Cleanup()
{
if (!String.IsNullOrEmpty(_logLocation) && File.Exists(_logLocation))
{
File.Delete(_logLocation);
}
if (_tempPath != null && File.Exists(_tempPath))
{
File.Delete(_tempPath);
}
driver.Quit();
}
[TestMethod]
public void BasicOptions()
{
var options = new FirefoxOptions();
driver = new FirefoxDriver(options);
}
[TestMethod]
public void Arguments()
{
var options = new FirefoxOptions();
options.AddArgument("-headless");
driver = new FirefoxDriver(options);
}
[TestMethod]
public async Task SetBinary()
{
var options = new FirefoxOptions();
options.BinaryLocation = await GetFirefoxLocationAsync();
driver = new FirefoxDriver(options);
}
[TestMethod]
public void LogsToFile()
{
var service = FirefoxDriverService.CreateDefaultService();
service.LogPath = GetLogLocation();
driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("geckodriver INFO Listening on")));
}
[TestMethod]
public void LogsToConsole()
{
TestLogHandler testLogHandler = new TestLogHandler();
ResetGlobalLog();
try
{
Log.SetLevel(LogEventLevel.Trace).Handlers.Add(testLogHandler);
var service = FirefoxDriverService.CreateDefaultService();
driver = new FirefoxDriver(service);
Assert.IsTrue(testLogHandler.Events.Count >= 1);
Assert.IsTrue(testLogHandler.Events.Any(e => e.Message.Contains("geckodriver")));
}
finally
{
ResetGlobalLog();
}
}
[TestMethod]
public void LogsLevel()
{
var service = FirefoxDriverService.CreateDefaultService();
service.LogPath = GetLogLocation();
service.LogLevel = FirefoxDriverLogLevel.Debug;
driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("Marionette\tDEBUG")));
}
[TestMethod]
public void StopsTruncatingLogs()
{
var service = FirefoxDriverService.CreateDefaultService();
service.LogTruncate = false;
service.LogLevel = FirefoxDriverLogLevel.Debug;
driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
Assert.IsNull(lines.FirstOrDefault(line => line.Contains(" ... ")));
}
[TestMethod]
public void SetProfileLocation()
{
var service = FirefoxDriverService.CreateDefaultService();
service.ProfileRoot = GetTempDirectory();
driver = new FirefoxDriver(service);
string profile = (string)driver.Capabilities.GetCapability("moz:profile");
string[] directories = profile.Split("/");
var dirName = directories.Last();
Assert.AreEqual(GetTempDirectory() + dirName, profile);
}
[TestMethod]
public void InstallAddon()
{
SetWaitingDriver();
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string extensionFilePath = Path.Combine(baseDir, "../../../Extensions/webextensions-selenium-example.xpi");
driver.InstallAddOnFromFile(Path.GetFullPath(extensionFilePath));
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
IWebElement injected = driver.FindElement(By.Id("webextensions-selenium-example"));
Assert.AreEqual("Content injected by webextensions-selenium-example", injected.Text);
}
[TestMethod]
public void UnInstallAddon()
{
driver = new FirefoxDriver();
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string extensionFilePath = Path.Combine(baseDir, "../../../Extensions/webextensions-selenium-example.xpi");
string extensionId = driver.InstallAddOnFromFile(Path.GetFullPath(extensionFilePath));
driver.UninstallAddOn(extensionId);
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
Assert.AreEqual(driver.FindElements(By.Id("webextensions-selenium-example")).Count, 0);
}
[TestMethod]
public void InstallUnsignedAddon()
{
SetWaitingDriver();
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string extensionDirPath = Path.Combine(baseDir, "../../../Extensions/webextensions-selenium-example/");
driver.InstallAddOnFromDirectory(Path.GetFullPath(extensionDirPath), true);
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
IWebElement injected = driver.FindElement(By.Id("webextensions-selenium-example"));
Assert.AreEqual("Content injected by webextensions-selenium-example", injected.Text);
}
private string GetLogLocation()
{
if (string.IsNullOrEmpty(_logLocation) && !File.Exists(_logLocation))
{
_logLocation = Path.GetTempFileName();
}
return _logLocation;
}
private string GetTempDirectory()
{
if (string.IsNullOrEmpty(_tempPath) && !File.Exists(_tempPath))
{
_tempPath = Path.GetTempPath();
}
return _tempPath;
}
private void SetWaitingDriver()
{
driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
}
private static async Task<string> GetFirefoxLocationAsync()
{
var options = new FirefoxOptions()
{
BrowserVersion = "stable"
};
return await new DriverFinder(options).GetBrowserPathAsync();
}
private void ResetGlobalLog()
{
Log.SetLevel(LogEventLevel.Info);
Log.Handlers.Clear().Handlers.Add(new TextWriterHandler(Console.Error));
}
}
}
class TestLogHandler : ILogHandler
{
public ILogHandler Clone()
{
return this;
}
public void Handle(LogEvent logEvent)
{
Events.Add(logEvent);
}
public IList<LogEvent> Events { get; internal set; } = new List<LogEvent>();
}