11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Linq ;
45using Microsoft . VisualStudio . TestTools . UnitTesting ;
56using OpenQA . Selenium ;
67using OpenQA . Selenium . Firefox ;
8+ using OpenQA . Selenium . Internal . Logging ;
79
810namespace SeleniumDocs . Browsers
911{
@@ -17,7 +19,7 @@ public class FirefoxTest
1719 [ TestCleanup ]
1820 public void Cleanup ( )
1921 {
20- if ( _logLocation != null && File . Exists ( _logLocation ) )
22+ if ( ! String . IsNullOrEmpty ( _logLocation ) && File . Exists ( _logLocation ) )
2123 {
2224 File . Delete ( _logLocation ) ;
2325 }
@@ -56,41 +58,44 @@ public void SetBinary()
5658 }
5759
5860 [ TestMethod ]
59- [ Ignore ( "Not implemented" ) ]
6061 public void LogsToFile ( )
6162 {
6263 var service = FirefoxDriverService . CreateDefaultService ( ) ;
63- // service.LogFile = _logLocation
64+ service . LogPath = GetLogLocation ( ) ;
6465
6566 driver = new FirefoxDriver ( service ) ;
6667 var lines = File . ReadLines ( GetLogLocation ( ) ) ;
6768 Assert . IsNotNull ( lines . FirstOrDefault ( line => line . Contains ( "geckodriver INFO Listening on" ) ) ) ;
6869 }
6970
7071 [ TestMethod ]
71- [ Ignore ( "Not implemented" ) ]
7272 public void LogsToConsole ( )
7373 {
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+ }
8592 }
8693
8794 [ TestMethod ]
88- [ Ignore ( "You can set it, just can't see it" ) ]
8995 public void LogsLevel ( )
9096 {
9197 var service = FirefoxDriverService . CreateDefaultService ( ) ;
92- //service.LogFile = _logLocation
93-
98+ service . LogPath = GetLogLocation ( ) ;
9499 service . LogLevel = FirefoxDriverLogLevel . Debug ;
95100
96101 driver = new FirefoxDriver ( service ) ;
@@ -99,11 +104,10 @@ public void LogsLevel()
99104 }
100105
101106 [ TestMethod ]
102- [ Ignore ( "Not implemented" ) ]
103107 public void StopsTruncatingLogs ( )
104108 {
105109 var service = FirefoxDriverService . CreateDefaultService ( ) ;
106- // service.TruncateLogs = false;
110+ service . LogTruncate = false ;
107111
108112 service . LogLevel = FirefoxDriverLogLevel . Debug ;
109113
@@ -113,18 +117,17 @@ public void StopsTruncatingLogs()
113117 }
114118
115119 [ TestMethod ]
116- [ Ignore ( "Not implemented" ) ]
117120 public void SetProfileLocation ( )
118121 {
119122 var service = FirefoxDriverService . CreateDefaultService ( ) ;
120- // service.ProfileRoot = GetTempDirectory();
123+ service . ProfileRoot = GetTempDirectory ( ) ;
121124
122125 driver = new FirefoxDriver ( service ) ;
123126
124127 string profile = ( string ) driver . Capabilities . GetCapability ( "moz:profile" ) ;
125128 string [ ] directories = profile . Split ( "/" ) ;
126129 var dirName = directories . Last ( ) ;
127- Assert . AreEqual ( GetTempDirectory ( ) + "/" + dirName , profile ) ;
130+ Assert . AreEqual ( GetTempDirectory ( ) + dirName , profile ) ;
128131 }
129132
130133 [ TestMethod ]
@@ -171,7 +174,7 @@ public void InstallUnsignedAddon()
171174
172175 private string GetLogLocation ( )
173176 {
174- if ( _logLocation != null && ! File . Exists ( _logLocation ) )
177+ if ( String . IsNullOrEmpty ( _logLocation ) && ! File . Exists ( _logLocation ) )
175178 {
176179 _logLocation = Path . GetTempFileName ( ) ;
177180 }
@@ -181,7 +184,7 @@ private string GetLogLocation()
181184
182185 private string GetTempDirectory ( )
183186 {
184- if ( _tempPath != null && ! File . Exists ( _tempPath ) )
187+ if ( String . IsNullOrEmpty ( _tempPath ) && ! File . Exists ( _tempPath ) )
185188 {
186189 _tempPath = Path . GetTempPath ( ) ;
187190 }
@@ -203,5 +206,26 @@ private static string GetFirefoxLocation()
203206 } ;
204207 return new DriverFinder ( options ) . GetBrowserPath ( ) ;
205208 }
209+
210+ private void ResetGlobalLog ( )
211+ {
212+ Log . SetLevel ( LogEventLevel . Info ) ;
213+ Log . Handlers . Clear ( ) . Handlers . Add ( new TextWriterHandler ( Console . Error ) ) ;
214+ }
206215 }
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 > ( ) ;
207231}
0 commit comments