aspnetcore/test/Microsoft.Blazor.E2ETest/Tests/ComponentRenderingTest.cs

69 lines
2.3 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using BasicTestApp;
using Microsoft.Blazor.E2ETest.Infrastructure;
using Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using Xunit;
namespace Microsoft.Blazor.E2ETest.Tests
{
public class ComponentRenderingTest
: ServerTestBase<DevHostServerFixture<BasicTestApp.Program>>
{
public ComponentRenderingTest(BrowserFixture browserFixture, DevHostServerFixture<Program> serverFixture)
: base(browserFixture, serverFixture)
{
}
[Fact]
public void BasicTestAppCanBeServed()
{
Navigate("/", noReload: true);
Assert.Equal("Basic test app", Browser.Title);
}
[Fact]
public void CanRenderTextOnlyComponent()
{
Navigate("/", noReload: true);
MountTestComponent("BasicTestApp.TextOnlyComponent");
var appElement = Browser.FindElement(By.TagName("app"));
Assert.Equal("Hello from TextOnlyComponent", appElement.Text);
}
[Fact]
public void CanRenderComponentWithAttributes()
{
Navigate("/", noReload: true);
MountTestComponent("BasicTestApp.RedTextComponent");
var appElement = Browser.FindElement(By.TagName("app"));
var styledElement = appElement.FindElement(By.TagName("h1"));
Assert.Equal("Hello, world!", styledElement.Text);
Assert.Equal("color: red;", styledElement.GetAttribute("style"));
Assert.Equal("somevalue", styledElement.GetAttribute("customattribute"));
}
private void MountTestComponent(string componentTypeName)
{
WaitUntilDotNetRunningInBrowser();
((IJavaScriptExecutor)Browser).ExecuteScript(
$"mountTestComponent('{componentTypeName}')");
}
private void WaitUntilDotNetRunningInBrowser()
{
new WebDriverWait(Browser, TimeSpan.FromSeconds(30)).Until(driver =>
{
return ((IJavaScriptExecutor)driver)
.ExecuteScript("return window.isTestReady;");
});
}
}
}