From 772e3a1a4419cc88b17dc566a46fd104388fbb85 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Mon, 29 Jan 2018 11:03:40 +0000 Subject: [PATCH] Clean up E2E tests now elements are retained via diffing --- .../Tests/ComponentRenderingTest.cs | 59 +++++++++---------- .../BasicTestApp/MessageComponent.cshtml | 2 +- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs index bdf82ceb4b..563f86f010 100644 --- a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using BasicTestApp; using Microsoft.AspNetCore.Blazor.Components; @@ -48,32 +49,34 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests [Fact] public void CanTriggerEvents() { + // Initial count is zero var appElement = MountTestComponent(); + var countDisplayElement = appElement.FindElement(By.TagName("p")); + Assert.Equal("Current count: 0", countDisplayElement.Text); - Assert.Equal( - "Current count: 0", - appElement.FindElement(By.TagName("p")).Text); - + // Clicking button increments count appElement.FindElement(By.TagName("button")).Click(); - - Assert.Equal( - "Current count: 1", - appElement.FindElement(By.TagName("p")).Text); + Assert.Equal("Current count: 1", countDisplayElement.Text); } [Fact] public void CanTriggerKeyPressEvents() { + // List is initially empty var appElement = MountTestComponent(); + var inputElement = appElement.FindElement(By.TagName("input")); + Func> liElements = + () => appElement.FindElements(By.TagName("li")); + Assert.Empty(liElements()); - Assert.Empty(appElement.FindElements(By.TagName("li"))); - - appElement.FindElement(By.TagName("input")).SendKeys("a"); - Assert.Collection(appElement.FindElements(By.TagName("li")), + // Typing adds element + inputElement.SendKeys("a"); + Assert.Collection(liElements(), li => Assert.Equal("a", li.Text)); - appElement.FindElement(By.TagName("input")).SendKeys("b"); - Assert.Collection(appElement.FindElements(By.TagName("li")), + // Typing again adds another element + inputElement.SendKeys("b"); + Assert.Collection(liElements(), li => Assert.Equal("a", li.Text), li => Assert.Equal("b", li.Text)); } @@ -99,34 +102,30 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests [Fact] public void CanTriggerEventsOnChildComponents() { + // Counter is displayed as child component. Initial count is zero. var childComponentWrapper = MountTestComponent() .FindElements(By.CssSelector("blazor-component")).Single(); + var counterDisplay = childComponentWrapper.FindElement(By.TagName("p")); + Assert.Equal("Current count: 0", counterDisplay.Text); - Assert.Equal( - "Current count: 0", - childComponentWrapper.FindElement(By.TagName("p")).Text); - + // Clicking increments count in child component childComponentWrapper.FindElement(By.TagName("button")).Click(); - - Assert.Equal( - "Current count: 1", - childComponentWrapper.FindElement(By.TagName("p")).Text); + Assert.Equal("Current count: 1", counterDisplay.Text); } [Fact] public void ChildComponentsRerenderWhenPropertiesChanged() { + // Count value is displayed in child component with initial value zero var appElement = MountTestComponent(); + 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( - "Current count: 0", - appElement.FindElement(By.TagName("p")).Text); - + // Clicking increments count in child element appElement.FindElement(By.TagName("button")).Click(); - - Assert.Equal( - "Current count: 1", - appElement.FindElement(By.TagName("p")).Text); + Assert.Equal("1", messageElementInChild.Text); } private IWebElement MountTestComponent() where TComponent: IComponent diff --git a/test/testapps/BasicTestApp/MessageComponent.cshtml b/test/testapps/BasicTestApp/MessageComponent.cshtml index 611f596f05..51ce2cc47d 100644 --- a/test/testapps/BasicTestApp/MessageComponent.cshtml +++ b/test/testapps/BasicTestApp/MessageComponent.cshtml @@ -1,4 +1,4 @@ -@Message +@Message @functions { public string Message { get; set; } }