Reliability improvement for forms input date tests (#14096)

This commit is contained in:
Steve Sanderson 2019-09-19 16:56:11 +01:00 committed by GitHub
parent a6f1917c01
commit 854c052e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -200,7 +200,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
Browser.Equal("modified valid", () => renewalDateInput.GetAttribute("class"));
// Can become invalid
renewalDateInput.ReplaceText("0/0/0");
ApplyInvalidInputDateValue(".renewal-date input", "11111-11-11");
Browser.Equal("modified invalid", () => renewalDateInput.GetAttribute("class"));
Browser.Equal(new[] { "The RenewalDate field must be a date." }, messagesAccessor);
@ -228,7 +228,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
Browser.Equal("modified valid", () => expiryDateInput.GetAttribute("class"));
// Can become invalid
expiryDateInput.ReplaceText("111111111");
ApplyInvalidInputDateValue(".expiry-date input", "11111-11-11");
Browser.Equal("modified invalid", () => expiryDateInput.GetAttribute("class"));
Browser.Equal(new[] { "The OptionalExpiryDate field must be a date." }, messagesAccessor);
@ -383,5 +383,20 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
.OrderBy(x => x)
.ToArray();
}
private void ApplyInvalidInputDateValue(string cssSelector, string invalidValue)
{
// It's very difficult to enter an invalid value into an <input type=date>, because
// most combinations of keystrokes get normalized to something valid. Additionally,
// using Selenium's SendKeys interacts unpredictably with this normalization logic,
// most likely based on timings. As a workaround, use JS to apply the values. This
// should only be used when strictly necessary, as it doesn't represent actual user
// interaction as authentically as SendKeys in other cases.
var javascript = (IJavaScriptExecutor)Browser;
javascript.ExecuteScript(
$"var elem = document.querySelector('{cssSelector}');"
+ $"elem.value = {JsonSerializer.Serialize(invalidValue, TestJsonSerializerOptionsProvider.Options)};"
+ "elem.dispatchEvent(new KeyboardEvent('change'));");
}
}
}