Skip to content

Commit

Permalink
[dotnet] Align WebDriver errors with specification (SeleniumHQ#14936)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael authored Dec 26, 2024
1 parent 983a43e commit 59f090b
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 63 deletions.
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private Response CreateResponse(HttpResponseInfo responseInfo)
}
else
{
response.Status = WebDriverResult.UnhandledError;
response.Status = WebDriverResult.UnknownError;
response.Value = body;
}
}
Expand Down
51 changes: 51 additions & 0 deletions dotnet/src/webdriver/UnknownErrorException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <copyright file="UnknownErrorException.cs" company="Selenium 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;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// An unknown error occurred in the remote end while processing the command.
/// </summary>
[Serializable]
public class UnknownErrorException : WebDriverException
{
/// <summary>
/// Initializes a new instance of the <see cref="UnknownErrorException"/> class with the specified message.
/// </summary>
/// <param name="message">The message of the exception.</param>
public UnknownErrorException(string? message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnknownErrorException"/> class with the specified message and inner exception.
/// </summary>
/// <param name="message">The message of the exception.</param>
/// <param name="innerException">The inner exception for this exception.</param>
public UnknownErrorException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}
}
49 changes: 49 additions & 0 deletions dotnet/src/webdriver/UnknownMethodException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="UnknownMethodException.cs" company="Selenium 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;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Exception that is thrown when the requested command matched a known URL but did not match any method for that URL.
/// </summary>
[Serializable]
public class UnknownMethodException : WebDriverException
{
/// <summary>
/// Initializes a new instance of the <see cref="UnknownMethodException"/> class with the specified message.
/// </summary>
/// <param name="message">The message of the exception.</param>
public UnknownMethodException(string? message) : base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnknownMethodException"/> class with the specified message and inner exception.
/// </summary>
/// <param name="message">The message of the exception.</param>
/// <param name="innerException">The inner exception for this exception.</param>
public UnknownMethodException(string? message, Exception? innerException) : base(message, innerException)
{
}
}
}
50 changes: 50 additions & 0 deletions dotnet/src/webdriver/UnsupportedOperationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// <copyright file="UnsupportedOperationException.cs" company="Selenium 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;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Indicates that a command that should have executed properly cannot be supported for some reason.
/// </summary>
public class UnsupportedOperationException : WebDriverException
{
/// <summary>
/// Initializes a new instance of the <see cref="UnsupportedOperationException"/> class with the specified message.
/// </summary>
/// <param name="message">The message of the exception.</param>
public UnsupportedOperationException(string? message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnsupportedOperationException"/> class with the specified message and inner exception.
/// </summary>
/// <param name="message">The message of the exception.</param>
/// <param name="innerException">The inner exception for this exception.</param>
public UnsupportedOperationException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}
}
14 changes: 10 additions & 4 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecut
{
commandResponse = new Response
{
Status = WebDriverResult.UnhandledError,
Status = WebDriverResult.UnknownError,
Value = e
};
}
Expand Down Expand Up @@ -782,9 +782,6 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command
case WebDriverResult.ElementNotSelectable:
throw new InvalidElementStateException(errorMessage);

case WebDriverResult.UnhandledError:
throw new WebDriverException(errorMessage);

case WebDriverResult.NoSuchDocument:
throw new NoSuchElementException(errorMessage);

Expand Down Expand Up @@ -854,6 +851,15 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command
case WebDriverResult.InsecureCertificate:
throw new InsecureCertificateException(errorMessage);

case WebDriverResult.UnknownError:
throw new UnknownErrorException(errorMessage);

case WebDriverResult.UnknownMethod:
throw new UnknownMethodException(errorMessage);

case WebDriverResult.UnsupportedOperation:
throw new UnsupportedOperationException(errorMessage);

default:
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
}
Expand Down
44 changes: 9 additions & 35 deletions dotnet/src/webdriver/WebDriverError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,16 @@ namespace OpenQA.Selenium
/// </summary>
internal static class WebDriverError
{
/// <summary>
/// Represents the detached shadow root error.
/// </summary>
public const string DetachedShadowRoot = "detached shadow root";

/// <summary>
/// Represents the element click intercepted error.
/// </summary>
public const string ElementClickIntercepted = "element click intercepted";

/// <summary>
/// Represents the element not selectable error.
/// </summary>
public const string ElementNotSelectable = "element not selectable";

/// <summary>
/// Represents the element not interactable error.
/// </summary>
public const string ElementNotInteractable = "element not interactable";

/// <summary>
/// Represents the element not visible error.
/// </summary>
/// TODO: Remove this string; it is no longer valid in the specification.
public const string ElementNotVisible = "element not visible";

/// <summary>
/// Represents the insecure certificate error.
/// </summary>
Expand All @@ -68,17 +52,6 @@ internal static class WebDriverError
/// </summary>
public const string InvalidCookieDomain = "invalid cookie domain";

/// <summary>
/// Represents the invalid coordinates error.
/// </summary>
public const string InvalidCoordinates = "invalid coordinates";

/// <summary>
/// Represents the invalid element coordinates error.
/// </summary>
/// TODO: Remove this string; it is no longer valid in the specification.
public const string InvalidElementCoordinates = "invalid element coordinates";

/// <summary>
/// Represents the invalid element state error.
/// </summary>
Expand Down Expand Up @@ -149,6 +122,11 @@ internal static class WebDriverError
/// </summary>
public const string StaleElementReference = "stale element reference";

/// <summary>
/// Represents the detached shadow root error.
/// </summary>
public const string DetachedShadowRoot = "detached shadow root";

/// <summary>
/// Represents the timeout error.
/// </summary>
Expand Down Expand Up @@ -194,16 +172,11 @@ internal static class WebDriverError
static WebDriverError()
{
resultMap = new Dictionary<string, WebDriverResult>();
resultMap[DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot;
resultMap[ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted;
resultMap[ElementNotSelectable] = WebDriverResult.ElementNotSelectable;
resultMap[ElementNotVisible] = WebDriverResult.ElementNotDisplayed;
resultMap[ElementNotInteractable] = WebDriverResult.ElementNotInteractable;
resultMap[InsecureCertificate] = WebDriverResult.InsecureCertificate;
resultMap[InvalidArgument] = WebDriverResult.InvalidArgument;
resultMap[InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain;
resultMap[InvalidCoordinates] = WebDriverResult.InvalidElementCoordinates;
resultMap[InvalidElementCoordinates] = WebDriverResult.InvalidElementCoordinates;
resultMap[InvalidElementState] = WebDriverResult.InvalidElementState;
resultMap[InvalidSelector] = WebDriverResult.InvalidSelector;
resultMap[InvalidSessionId] = WebDriverResult.NoSuchDriver;
Expand All @@ -218,14 +191,15 @@ static WebDriverError()
resultMap[ScriptTimeout] = WebDriverResult.AsyncScriptTimeout;
resultMap[SessionNotCreated] = WebDriverResult.SessionNotCreated;
resultMap[StaleElementReference] = WebDriverResult.ObsoleteElement;
resultMap[DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot;
resultMap[Timeout] = WebDriverResult.Timeout;
resultMap[UnableToSetCookie] = WebDriverResult.UnableToSetCookie;
resultMap[UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen;
resultMap[UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen;
resultMap[UnknownCommand] = WebDriverResult.UnknownCommand;
resultMap[UnknownError] = WebDriverResult.UnhandledError;
resultMap[UnknownMethod] = WebDriverResult.UnknownCommand;
resultMap[UnsupportedOperation] = WebDriverResult.UnhandledError;
resultMap[UnknownError] = WebDriverResult.UnknownError;
resultMap[UnknownMethod] = WebDriverResult.UnknownMethod;
resultMap[UnsupportedOperation] = WebDriverResult.UnsupportedOperation;
}

/// <summary>
Expand Down
Loading

0 comments on commit 59f090b

Please sign in to comment.