Skip to content

Commit

Permalink
E2E Test: Sign in page check update (#140)
Browse files Browse the repository at this point in the history
- Added test for Cody Free/Cody Enterprise sections
- Introduced ThreadHelper.JoinableTaskContext to use single setup step for every test in the constructor

CLOSE https://linear.app/sourcegraph/issue/CODY-4234/e2e-test-sign-in-page-check-update
  • Loading branch information
PiotrKarczmarz authored Nov 6, 2024
1 parent 61f6ca7 commit 0f62067
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
83 changes: 57 additions & 26 deletions src/Cody.VisualStudio.Tests/ChatNotLoggedStateTests.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,84 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
using Xunit;
using Xunit.Abstractions;
using Microsoft.VisualStudio.Shell;

namespace Cody.VisualStudio.Tests
{
public class ChatNotLoggedStateTests : PlaywrightTestsBase, IDisposable
{
private readonly JoinableTaskContext _context = ThreadHelper.JoinableTaskContext;

private string _accessToken;

public ChatNotLoggedStateTests(ITestOutputHelper output) : base(output)
{
_context.Factory.Run(async () =>
{
await WaitForPlaywrightAsync();
_accessToken = await GetAccessToken();

if (_accessToken != null)
await SetAccessToken("INVALID");
});

}

[VsFact(Version = VsVersion.VS2022)]
public async Task Loads_Properly_InNotLoggedState()
public async Task Cody_Free_Cody_Pro_Section_Is_Present()
{
// given
var sectionText = "Cody Free or Cody Pro";
var buttonText = "Sign In with GitHub";

var text = "Cody Free or Cody Pro";
IReadOnlyList<string> textContents;
string accessToken = null;
try
{
await WaitForPlaywrightAsync();
// then
await AssertTextIsPresent(sectionText);
await AssertTextIsPresent(buttonText);
}

accessToken = await GetAccessToken();
if (accessToken != null)
await SetAccessToken("INVALID");
[VsFact(Version = VsVersion.VS2022)]
public async Task Cody_Enterprise_Section_Is_Present()
{
// given
var sectionText = "Cody Enterprise";
var browserButtonText = " Sign In with Browser";
var tokenButtonText = " Sign In with Access Token";

// when
var getStarted = Page.GetByText(text);
textContents = await getStarted.AllTextContentsAsync();
}
finally
{
if (accessToken != null)
await SetAccessToken(accessToken); // make it valid
}
// then
await AssertTextIsPresent(sectionText);
await AssertTextIsPresent(browserButtonText);
await AssertTextIsPresent(tokenButtonText);
}

[VsFact(Version = VsVersion.VS2022)]
public async Task Logins_With_GitLab_Google_Are_Present()
{
// given
var gitlabButtonText = "Sign In with GitLab";
var googleButtonText = "Sign In with Google";

// then
Assert.Equal(text, textContents.First());
await AssertTextIsPresent(gitlabButtonText);
await AssertTextIsPresent(googleButtonText);
}

public void Dispose()

public async void Dispose()
{
var testName = GetTestName();
TakeScreenshot(testName);
try
{
var testName = GetTestName();
TakeScreenshot(testName);

if (_accessToken != null)
await SetAccessToken(_accessToken); // make it valid
}
catch (Exception ex)
{
WriteLog($"Dispose() for {GetTestName()} failed.");
}
}
}
}
13 changes: 13 additions & 0 deletions src/Cody.VisualStudio.Tests/PlaywrightTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using EnvDTE80;
using Microsoft.Playwright;
using Microsoft.VisualStudio.Shell;
using Xunit;
using Xunit.Abstractions;

namespace Cody.VisualStudio.Tests
Expand Down Expand Up @@ -125,6 +127,17 @@ protected async Task SetAccessToken(string accessToken)
await Task.Delay(TimeSpan.FromMilliseconds(2000)); // wait for the Chat to response
}

protected async Task AssertTextIsPresent(string text)
{
// given

var getStarted = Page.GetByText(text);
var textContents = await getStarted.AllTextContentsAsync();

// then
Assert.Equal(text, textContents.First());
}

protected async Task ShowChatTab() => await Page.GetByTestId("tab-chat").ClickAsync();

protected async Task ShowHistoryTab()
Expand Down

0 comments on commit 0f62067

Please sign in to comment.