Skip to content

Commit

Permalink
Adding support for .NET legacy log API for drivers that support them
Browse files Browse the repository at this point in the history
The logging APIs (`driver.Manage().Logs`) are not part of the W3C
WebDriver Specification, and users should only use them for drivers
that support the extension end points for the methods. Drivers that
support these methods will be marked with the new ISupportsLogs
interface, and the user can use a cast (using a non-throwing operator
like the `is` or the `as` operator) to check whether the API is
supported before attempting to call the logging methods. If the
driver does not implement the interface, the logging methods will
not throw (for now), but will return empty lists instead of valid
data.
  • Loading branch information
jimevans committed Jun 24, 2019
1 parent 6bd78a5 commit 02203c8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OpenQA.Selenium.Chromium
{
public abstract class ChromiumDriver : RemoteWebDriver
public abstract class ChromiumDriver : RemoteWebDriver, ISupportsLogs
{
/// <summary>
/// Accept untrusted SSL Certificates
Expand Down
34 changes: 34 additions & 0 deletions dotnet/src/webdriver/ISupportsLogs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="ISupportsLogs.cs" company="WebDriver 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenQA.Selenium
{
/// <summary>
/// Marker interface indicating that an object implementing
/// the interface supports the legacy log retrieval methods.
/// </summary>
public interface ISupportsLogs
{
}
}
35 changes: 35 additions & 0 deletions dotnet/src/webdriver/Remote/RemoteLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace OpenQA.Selenium.Remote
public class RemoteLogs : ILogs
{
private RemoteWebDriver driver;
private bool isLogSupported = false;

/// <summary>
/// Initializes a new instance of the <see cref="RemoteLogs"/> class.
Expand All @@ -35,6 +36,7 @@ public class RemoteLogs : ILogs
public RemoteLogs(RemoteWebDriver driver)
{
this.driver = driver;
this.isLogSupported = (this.driver as ISupportsLogs) != null;
}

/// <summary>
Expand All @@ -45,6 +47,19 @@ public ReadOnlyCollection<string> AvailableLogTypes
get
{
List<string> availableLogTypes = new List<string>();
if (this.isLogSupported)
{
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAvailableLogTypes, null);
object[] responseValue = commandResponse.Value as object[];
if (responseValue != null)
{
foreach (object logKind in responseValue)
{
availableLogTypes.Add(logKind.ToString());
}
}
}

return availableLogTypes.AsReadOnly();
}
}
Expand All @@ -58,6 +73,26 @@ public ReadOnlyCollection<string> AvailableLogTypes
public ReadOnlyCollection<LogEntry> GetLog(string logKind)
{
List<LogEntry> entries = new List<LogEntry>();
if (this.isLogSupported)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("type", logKind);
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetLog, parameters);

object[] responseValue = commandResponse.Value as object[];
if (responseValue != null)
{
foreach (object rawEntry in responseValue)
{
Dictionary<string, object> entryDictionary = rawEntry as Dictionary<string, object>;
if (entryDictionary != null)
{
entries.Add(LogEntry.FromDictionary(entryDictionary));
}
}
}
}

return entries.AsReadOnly();
}
}
Expand Down
9 changes: 9 additions & 0 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCa
this.storage = new RemoteWebStorage(this);
}
}

if ((this as ISupportsLogs) != null)
{
// Only add the legacy log commands if the driver supports
// retrieving the logs via the extension end points.
this.CommandExecutor.CommandInfoRepository.TryAddCommand(DriverCommand.GetAvailableLogTypes, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/se/log/types"));
this.CommandExecutor.CommandInfoRepository.TryAddCommand(DriverCommand.GetLog, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/se/logs"));
}

}

/// <summary>
Expand Down

0 comments on commit 02203c8

Please sign in to comment.