1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . IO ;
3
4
using System . Linq ;
4
5
using Microsoft . VisualStudio . TestTools . UnitTesting ;
5
6
using OpenQA . Selenium ;
6
7
using OpenQA . Selenium . Firefox ;
8
+ using OpenQA . Selenium . Internal . Logging ;
7
9
8
10
namespace SeleniumDocs . Browsers
9
11
{
@@ -17,7 +19,7 @@ public class FirefoxTest
17
19
[ TestCleanup ]
18
20
public void Cleanup ( )
19
21
{
20
- if ( _logLocation != null && File . Exists ( _logLocation ) )
22
+ if ( ! String . IsNullOrEmpty ( _logLocation ) && File . Exists ( _logLocation ) )
21
23
{
22
24
File . Delete ( _logLocation ) ;
23
25
}
@@ -56,41 +58,44 @@ public void SetBinary()
56
58
}
57
59
58
60
[ TestMethod ]
59
- [ Ignore ( "Not implemented" ) ]
60
61
public void LogsToFile ( )
61
62
{
62
63
var service = FirefoxDriverService . CreateDefaultService ( ) ;
63
- // service.LogFile = _logLocation
64
+ service . LogPath = GetLogLocation ( ) ;
64
65
65
66
driver = new FirefoxDriver ( service ) ;
66
67
var lines = File . ReadLines ( GetLogLocation ( ) ) ;
67
68
Assert . IsNotNull ( lines . FirstOrDefault ( line => line . Contains ( "geckodriver INFO Listening on" ) ) ) ;
68
69
}
69
70
70
71
[ TestMethod ]
71
- [ Ignore ( "Not implemented" ) ]
72
72
public void LogsToConsole ( )
73
73
{
74
- var stringWriter = new StringWriter ( ) ;
75
- var originalOutput = Console . Out ;
76
- Console . SetOut ( stringWriter ) ;
77
-
78
- var service = FirefoxDriverService . CreateDefaultService ( ) ;
79
- //service.LogToConsole = true;
80
-
81
- driver = new FirefoxDriver ( service ) ;
82
- Assert . IsTrue ( stringWriter . ToString ( ) . Contains ( "geckodriver INFO Listening on" ) ) ;
83
- Console . SetOut ( originalOutput ) ;
84
- stringWriter . Dispose ( ) ;
74
+ TestLogHandler testLogHandler = new TestLogHandler ( ) ;
75
+ ResetGlobalLog ( ) ;
76
+ try
77
+ {
78
+ Log . SetLevel ( LogEventLevel . Trace ) . Handlers . Add ( testLogHandler ) ;
79
+ var service = FirefoxDriverService . CreateDefaultService ( ) ;
80
+ driver = new FirefoxDriver ( service ) ;
81
+ Assert . IsTrue ( testLogHandler . Events . Count >= 1 ) ;
82
+ Assert . IsTrue ( testLogHandler . Events . Any ( e => e . Message . Contains ( "geckodriver INFO" ) ) ) ;
83
+ }
84
+ catch ( Exception e )
85
+ {
86
+ // If the test fails, we still want to reset the global log
87
+ }
88
+ finally
89
+ {
90
+ ResetGlobalLog ( ) ;
91
+ }
85
92
}
86
93
87
94
[ TestMethod ]
88
- [ Ignore ( "You can set it, just can't see it" ) ]
89
95
public void LogsLevel ( )
90
96
{
91
97
var service = FirefoxDriverService . CreateDefaultService ( ) ;
92
- //service.LogFile = _logLocation
93
-
98
+ service . LogPath = GetLogLocation ( ) ;
94
99
service . LogLevel = FirefoxDriverLogLevel . Debug ;
95
100
96
101
driver = new FirefoxDriver ( service ) ;
@@ -99,11 +104,10 @@ public void LogsLevel()
99
104
}
100
105
101
106
[ TestMethod ]
102
- [ Ignore ( "Not implemented" ) ]
103
107
public void StopsTruncatingLogs ( )
104
108
{
105
109
var service = FirefoxDriverService . CreateDefaultService ( ) ;
106
- // service.TruncateLogs = false;
110
+ service . LogTruncate = false ;
107
111
108
112
service . LogLevel = FirefoxDriverLogLevel . Debug ;
109
113
@@ -113,18 +117,17 @@ public void StopsTruncatingLogs()
113
117
}
114
118
115
119
[ TestMethod ]
116
- [ Ignore ( "Not implemented" ) ]
117
120
public void SetProfileLocation ( )
118
121
{
119
122
var service = FirefoxDriverService . CreateDefaultService ( ) ;
120
- // service.ProfileRoot = GetTempDirectory();
123
+ service . ProfileRoot = GetTempDirectory ( ) ;
121
124
122
125
driver = new FirefoxDriver ( service ) ;
123
126
124
127
string profile = ( string ) driver . Capabilities . GetCapability ( "moz:profile" ) ;
125
128
string [ ] directories = profile . Split ( "/" ) ;
126
129
var dirName = directories . Last ( ) ;
127
- Assert . AreEqual ( GetTempDirectory ( ) + "/" + dirName , profile ) ;
130
+ Assert . AreEqual ( GetTempDirectory ( ) + dirName , profile ) ;
128
131
}
129
132
130
133
[ TestMethod ]
@@ -171,7 +174,7 @@ public void InstallUnsignedAddon()
171
174
172
175
private string GetLogLocation ( )
173
176
{
174
- if ( _logLocation != null && ! File . Exists ( _logLocation ) )
177
+ if ( String . IsNullOrEmpty ( _logLocation ) && ! File . Exists ( _logLocation ) )
175
178
{
176
179
_logLocation = Path . GetTempFileName ( ) ;
177
180
}
@@ -181,7 +184,7 @@ private string GetLogLocation()
181
184
182
185
private string GetTempDirectory ( )
183
186
{
184
- if ( _tempPath != null && ! File . Exists ( _tempPath ) )
187
+ if ( String . IsNullOrEmpty ( _tempPath ) && ! File . Exists ( _tempPath ) )
185
188
{
186
189
_tempPath = Path . GetTempPath ( ) ;
187
190
}
@@ -203,5 +206,26 @@ private static string GetFirefoxLocation()
203
206
} ;
204
207
return new DriverFinder ( options ) . GetBrowserPath ( ) ;
205
208
}
209
+
210
+ private void ResetGlobalLog ( )
211
+ {
212
+ Log . SetLevel ( LogEventLevel . Info ) ;
213
+ Log . Handlers . Clear ( ) . Handlers . Add ( new TextWriterHandler ( Console . Error ) ) ;
214
+ }
206
215
}
216
+ }
217
+
218
+ class TestLogHandler : ILogHandler
219
+ {
220
+ public ILogHandler Clone ( )
221
+ {
222
+ return this ;
223
+ }
224
+
225
+ public void Handle ( LogEvent logEvent )
226
+ {
227
+ Events . Add ( logEvent ) ;
228
+ }
229
+
230
+ public IList < LogEvent > Events { get ; internal set ; } = new List < LogEvent > ( ) ;
207
231
}
0 commit comments