Clean up E2E tests now elements are retained via diffing

This commit is contained in:
Steve Sanderson 2018-01-29 11:03:40 +00:00
parent 4f496f649a
commit 772e3a1a44
2 changed files with 30 additions and 31 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using BasicTestApp; using BasicTestApp;
using Microsoft.AspNetCore.Blazor.Components; using Microsoft.AspNetCore.Blazor.Components;
@ -48,32 +49,34 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
[Fact] [Fact]
public void CanTriggerEvents() public void CanTriggerEvents()
{ {
// Initial count is zero
var appElement = MountTestComponent<CounterComponent>(); var appElement = MountTestComponent<CounterComponent>();
var countDisplayElement = appElement.FindElement(By.TagName("p"));
Assert.Equal("Current count: 0", countDisplayElement.Text);
Assert.Equal( // Clicking button increments count
"Current count: 0",
appElement.FindElement(By.TagName("p")).Text);
appElement.FindElement(By.TagName("button")).Click(); appElement.FindElement(By.TagName("button")).Click();
Assert.Equal("Current count: 1", countDisplayElement.Text);
Assert.Equal(
"Current count: 1",
appElement.FindElement(By.TagName("p")).Text);
} }
[Fact] [Fact]
public void CanTriggerKeyPressEvents() public void CanTriggerKeyPressEvents()
{ {
// List is initially empty
var appElement = MountTestComponent<KeyPressEventComponent>(); var appElement = MountTestComponent<KeyPressEventComponent>();
var inputElement = appElement.FindElement(By.TagName("input"));
Func<IEnumerable<IWebElement>> liElements =
() => appElement.FindElements(By.TagName("li"));
Assert.Empty(liElements());
Assert.Empty(appElement.FindElements(By.TagName("li"))); // Typing adds element
inputElement.SendKeys("a");
appElement.FindElement(By.TagName("input")).SendKeys("a"); Assert.Collection(liElements(),
Assert.Collection(appElement.FindElements(By.TagName("li")),
li => Assert.Equal("a", li.Text)); li => Assert.Equal("a", li.Text));
appElement.FindElement(By.TagName("input")).SendKeys("b"); // Typing again adds another element
Assert.Collection(appElement.FindElements(By.TagName("li")), inputElement.SendKeys("b");
Assert.Collection(liElements(),
li => Assert.Equal("a", li.Text), li => Assert.Equal("a", li.Text),
li => Assert.Equal("b", li.Text)); li => Assert.Equal("b", li.Text));
} }
@ -99,34 +102,30 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
[Fact] [Fact]
public void CanTriggerEventsOnChildComponents() public void CanTriggerEventsOnChildComponents()
{ {
// Counter is displayed as child component. Initial count is zero.
var childComponentWrapper = MountTestComponent<CounterComponentWrapper>() var childComponentWrapper = MountTestComponent<CounterComponentWrapper>()
.FindElements(By.CssSelector("blazor-component")).Single(); .FindElements(By.CssSelector("blazor-component")).Single();
var counterDisplay = childComponentWrapper.FindElement(By.TagName("p"));
Assert.Equal("Current count: 0", counterDisplay.Text);
Assert.Equal( // Clicking increments count in child component
"Current count: 0",
childComponentWrapper.FindElement(By.TagName("p")).Text);
childComponentWrapper.FindElement(By.TagName("button")).Click(); childComponentWrapper.FindElement(By.TagName("button")).Click();
Assert.Equal("Current count: 1", counterDisplay.Text);
Assert.Equal(
"Current count: 1",
childComponentWrapper.FindElement(By.TagName("p")).Text);
} }
[Fact] [Fact]
public void ChildComponentsRerenderWhenPropertiesChanged() public void ChildComponentsRerenderWhenPropertiesChanged()
{ {
// Count value is displayed in child component with initial value zero
var appElement = MountTestComponent<CounterComponentUsingChild>(); var appElement = MountTestComponent<CounterComponentUsingChild>();
var wholeCounterElement = appElement.FindElement(By.TagName("p"));
var messageElementInChild = wholeCounterElement.FindElement(By.ClassName("message"));
Assert.Equal("Current count: 0", wholeCounterElement.Text);
Assert.Equal("0", messageElementInChild.Text);
Assert.Equal( // Clicking increments count in child element
"Current count: 0",
appElement.FindElement(By.TagName("p")).Text);
appElement.FindElement(By.TagName("button")).Click(); appElement.FindElement(By.TagName("button")).Click();
Assert.Equal("1", messageElementInChild.Text);
Assert.Equal(
"Current count: 1",
appElement.FindElement(By.TagName("p")).Text);
} }
private IWebElement MountTestComponent<TComponent>() where TComponent: IComponent private IWebElement MountTestComponent<TComponent>() where TComponent: IComponent

View File

@ -1,4 +1,4 @@
<span style="color: red">@Message</span> <span style="color: red" class="message">@Message</span>
@functions { @functions {
public string Message { get; set; } public string Message { get; set; }
} }