Skip to content

Commit 9f1e37d

Browse files
authored
Merge pull request #56 from mjohanss/master
Fixes for UI tests on build server + some UI navigation tests
2 parents 8ad36a1 + 3924aa3 commit 9f1e37d

File tree

7 files changed

+189
-1
lines changed

7 files changed

+189
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using OpenQA.Selenium;
3+
using OpenQA.Selenium.Support.UI;
4+
using Xunit.Sdk;
5+
6+
namespace codeRR.Server.Web.Tests.Helpers.Extensions
7+
{
8+
public static class WebDriverExtensions
9+
{
10+
public static bool ElementIsPresent(this IWebDriver driver, By by)
11+
{
12+
var present = false;
13+
try
14+
{
15+
present = driver.FindElement(by).Displayed;
16+
}
17+
catch (NoSuchElementException)
18+
{
19+
}
20+
return present;
21+
}
22+
23+
public static bool WaitUntilElementIsPresent(this IWebDriver driver, By by, int timeout = 5)
24+
{
25+
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
26+
return wait.Until(d => d.ElementIsPresent(by));
27+
}
28+
29+
public static string WaitUntilTitleEquals(this IWebDriver driver, string title, int timeout = 5)
30+
{
31+
try
32+
{
33+
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
34+
wait.Until(ExpectedConditions.TitleIs(title));
35+
}
36+
catch
37+
{
38+
// ignored
39+
}
40+
41+
return driver.Title;
42+
}
43+
}
44+
}

src/Server/Coderr.Server.Web.Tests/Helpers/IisExpressHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,14 @@ private void StartIisExpress(Action<Process> action, string site)
128128
};
129129

130130
if (EnvironmentVariables != null)
131+
{
131132
foreach (var environmentVariable in EnvironmentVariables)
133+
{
134+
Console.WriteLine($"Setting environment variable '{environmentVariable.Key}' to '{environmentVariable.Value}'");
135+
132136
process.StartInfo.EnvironmentVariables[environmentVariable.Key] = environmentVariable.Value;
137+
}
138+
}
133139

134140
process.OutputDataReceived += (sender, args) =>
135141
{

src/Server/Coderr.Server.Web.Tests/Pages/BasePage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public void NavigateToPage(bool wait = true)
3535
Wait.Until(ExpectedConditions.UrlContains(Url));
3636
}
3737

38+
public void WaitForTitle(string title)
39+
{
40+
Wait.Until(ExpectedConditions.TitleIs(title));
41+
}
42+
3843
public void DeleteCookies()
3944
{
4045
WebDriver.Manage().Cookies.DeleteAllCookies();

src/Server/Coderr.Server.Web.Tests/Pages/HomePage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace codeRR.Server.Web.Tests.Pages
66
{
77
public class HomePage : BasePage
88
{
9-
public HomePage(IWebDriver webDriver) : base(webDriver, "", "MyTestApp")
9+
public HomePage(IWebDriver webDriver) : base(webDriver, "", "Overview")
1010
{
1111
}
1212

@@ -16,6 +16,18 @@ public HomePage(IWebDriver webDriver) : base(webDriver, "", "MyTestApp")
1616
[FindsBy(How = How.Id, Using = "pageTitle")]
1717
public IWebElement PageTitle { get; set; }
1818

19+
[FindsBy(How = How.XPath, Using = "//a/span[.=' Dashboard ']")]
20+
public IWebElement NavigationDashboard { get; set; }
21+
22+
[FindsBy(How = How.XPath, Using = "//a[.='Overview']")]
23+
public IWebElement NavigationDashboardOverview { get; set; }
24+
25+
[FindsBy(How = How.XPath, Using = "//a[.='Incidents']")]
26+
public IWebElement NavigationDashboardIncidents { get; set; }
27+
28+
[FindsBy(How = How.XPath, Using = "//a[.='Feedback']")]
29+
public IWebElement NavigationDashboardFeedback { get; set; }
30+
1931
public bool HasApplicationInNavigation(string applicationName)
2032
{
2133
var by = By.XPath($"//a/span[.=' {applicationName} ']");
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using codeRR.Server.Web.Tests.Helpers.Extensions;
3+
using codeRR.Server.Web.Tests.Pages;
4+
using Xunit;
5+
6+
namespace codeRR.Server.Web.Tests.Tests
7+
{
8+
[Trait("Category", "Integration")]
9+
public class NavigationTests : LoggedInTest, IDisposable
10+
{
11+
public NavigationTests()
12+
{
13+
Login();
14+
}
15+
16+
[Fact]
17+
public void Should_be_able_to_navigate_to_dashboard()
18+
{
19+
UITest(() =>
20+
{
21+
var sut = new HomePage(WebDriver);
22+
sut.NavigateToPage();
23+
sut.NavigationDashboard.Click();
24+
25+
Assert.Equal("Overview", WebDriver.WaitUntilTitleEquals("Overview"));
26+
});
27+
}
28+
29+
[Fact]
30+
public void Should_be_able_to_navigate_to_dashboard_overview()
31+
{
32+
UITest(() =>
33+
{
34+
var sut = new HomePage(WebDriver);
35+
sut.NavigateToPage();
36+
sut.NavigationDashboardOverview.Click();
37+
38+
Assert.Equal("Overview", WebDriver.WaitUntilTitleEquals("Overview"));
39+
});
40+
}
41+
42+
[Fact]
43+
public void Should_be_able_to_navigate_to_dashboard_incidents()
44+
{
45+
UITest(() =>
46+
{
47+
var sut = new HomePage(WebDriver);
48+
sut.NavigateToPage();
49+
sut.NavigationDashboardIncidents.Click();
50+
51+
Assert.Equal("Incidents", WebDriver.WaitUntilTitleEquals("Incidents"));
52+
});
53+
}
54+
55+
[Fact]
56+
public void Should_be_able_to_navigate_to_dashboard_feedback()
57+
{
58+
UITest(() =>
59+
{
60+
var sut = new HomePage(WebDriver);
61+
sut.NavigateToPage();
62+
sut.NavigationDashboardFeedback.Click();
63+
64+
Assert.Equal("All feedback", WebDriver.WaitUntilTitleEquals("All feedback"));
65+
});
66+
}
67+
68+
public void Dispose()
69+
{
70+
Logout();
71+
}
72+
}
73+
}

src/Server/Coderr.Server.Web.Tests/WebTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Configuration;
34
using System.IO;
5+
using System.Net;
6+
using System.Web.Configuration;
47
using codeRR.Server.SqlServer.Core.Accounts;
58
using codeRR.Server.SqlServer.Tests.Helpers;
69
using codeRR.Server.Web.Tests.Helpers;
@@ -35,6 +38,14 @@ static WebTest()
3538
_databaseManager.Dispose();
3639
};
3740

41+
// Disables database migration in codeRR.Server.Web project, should be up-to-date already
42+
// SchemaUpdateModule does not handle coderr_ConnectionString environment variable
43+
// This should only be run on build server due to changes in web.config
44+
if (Environment.GetEnvironmentVariable("BUILD_SERVER") != null)
45+
{
46+
DisableDatabaseMigrations();
47+
}
48+
3849
var configPath =
3950
Path.Combine(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\")),
4051
"applicationhost.config");
@@ -50,11 +61,44 @@ static WebTest()
5061
};
5162
_iisExpress.Start("codeRR.Server.Web");
5263

64+
// Warmup request only on build server
65+
if (Environment.GetEnvironmentVariable("BUILD_SERVER") != null)
66+
{
67+
var webClient = new WebClient();
68+
webClient.DownloadString(_iisExpress.BaseUrl);
69+
}
70+
5371
TestData = new TestDataManager(_databaseManager.OpenConnection);
5472
WebDriver = DriverFactory.Create(BrowserType.Chrome);
5573
AppDomain.CurrentDomain.DomainUnload += (o, e) => { DisposeWebDriver(); };
5674
}
5775

76+
/// <summary>
77+
/// Disables database migration in codeRR.Server.Web project
78+
/// </summary>
79+
private static void DisableDatabaseMigrations()
80+
{
81+
var webConfigFilename =
82+
Path.Combine(
83+
Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\codeRR.Server.Web\")),
84+
"web.config");
85+
86+
Console.WriteLine($"Setting DisableMigrations=true in '{webConfigFilename}'");
87+
88+
// Prevent SchemaUpdateModule from running
89+
var configFile = new FileInfo(webConfigFilename);
90+
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
91+
var wcfm = new WebConfigurationFileMap();
92+
wcfm.VirtualDirectories.Add("/", vdm);
93+
var configuration = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
94+
var appSettings = configuration.AppSettings.Settings;
95+
if (appSettings["DisableMigrations"] == null)
96+
appSettings.Add("DisableMigrations", "true");
97+
else
98+
appSettings["DisableMigrations"].Value = "true";
99+
configuration.Save();
100+
}
101+
58102
protected WebTest()
59103
{
60104
TestData.ResetDatabase(_iisExpress.BaseUrl);

src/Server/Coderr.Server.Web.Tests/codeRR.Server.Web.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<ProjectReference Include="..\Coderr.Server.SqlServer.Tests\codeRR.Server.SqlServer.Tests.csproj" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<Reference Include="System.Web" />
25+
</ItemGroup>
26+
2327
<ItemGroup>
2428
<None Update="applicationhost.config">
2529
<DesignTime>True</DesignTime>

0 commit comments

Comments
 (0)