Skip to content

Commit

Permalink
Update CI workflow and UI tests: rename job, add UI test execution, a…
Browse files Browse the repository at this point in the history
…nd improve URL handling
  • Loading branch information
tsviz committed Nov 20, 2024
1 parent 6be3de1 commit 7b0044a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
48 changes: 11 additions & 37 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ env:
CONTAINER_REGISTRY: "ghcr.io/octodemo/dotnet-razor-pages-movie"

jobs:
provision-environment-for-qa:
qa-functional-UI-tests:
permissions:
actions: read
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
issues: write


runs-on: ubuntu-latest
runs-on: ubuntu-20.04-16core
environment: STAGE
steps:
- name: Checkout code
Expand Down Expand Up @@ -87,6 +86,13 @@ jobs:
echo "ARTIFACT_ID=$ARTIFACT_ID" >> $GITHUB_ENV
echo "ARTIFACT_URL=https://github.com/octodemo/dotnet-razor-pages-movie/actions/runs/${{ github.run_id }}/artifacts/${ARTIFACT_ID}" >> $GITHUB_ENV
- name: Run UI Automated Selenium Tests
run: |
dotnet test RazorPagesMovie.UITests/RazorPagesMovie.UITests.csproj --logger "console;verbosity=detailed"
env:
BASE_URL: ${{ steps.capture_outputs.outputs.CONTAINER_APP_URL }}


# Open an issue and notify QA that the staging environment is ready for testing
- name: Create Issue
uses: actions/[email protected]
Expand Down Expand Up @@ -123,39 +129,7 @@ jobs:
IMAGE_TAG: ${{ inputs.image_tag }}
CONTAINER_APP_URL: ${{ env.CONTAINER_APP_URL }}
ARTIFACT_ID: ${{ env.ARTIFACT_ID }}

# automated-ui-tests:
# permissions:
# actions: read
# id-token: write # This is required for requesting the JWT
# contents: read # This is required for actions/checkout
# issues: write
# runs-on: ubuntu-latest
# environment: STAGE
# steps:
# - name: Checkout code
# uses: actions/[email protected]

# - name: Set environment variables
# run: |
# export SA_PASSWORD='YourStrong!Passw0rd'
# export ConnectionStrings__RazorPagesMovieContext='Server=sqlserver,1433;Database=RazorPagesMovieContext;User ID=sa;Password=YourStrong!Passw0rd;'
# export ASPNETCORE_URLS="http://0.0.0.0:5000"
# export TAG="132"

# - name: Run Docker Compose
# run: docker compose up -d --build webapp

# - name: Run UI acceptance tests
# run: |
# echo -e "\033[0;32mdotnet clean RazorPagesMovie.UITests/RazorPagesMovie.UITests.csproj\033[0m"
# dotnet restore RazorPagesMovie.UITests/RazorPagesMovie.UITests.csproj

# echo -e "\033[0;32mWaiting for the web application to start...\033[0m"
# sleep 20

# echo -e "\033[0;32mdotnet test RazorPagesMovie.UITests/RazorPagesMovie.UITests.csproj --logger \"console;verbosity=detailed\"\033[0m"
# dotnet test RazorPagesMovie.UITests/RazorPagesMovie.UITests.csproj --logger "console;verbosity=detailed"


production:
permissions:
Expand All @@ -167,7 +141,7 @@ jobs:
environment:
name: PROD
url: https://tsvi-demo-movie.salmontree-a9d9695c.eastus.azurecontainerapps.io
needs: [provision-environment-for-qa]
needs: [qa-functional-UI-tests]
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
Expand Down
27 changes: 19 additions & 8 deletions RazorPagesMovie.UITests/LoginUITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,44 @@ namespace RazorPagesMovie.Tests.UITests
public class LoginUITests : IClassFixture<WebDriverFixture>
{
private readonly IWebDriver _driver;
private readonly string _url = "http://localhost/Account/Login";
private readonly string _url;
private readonly string _baseUrl;
private const string DEFAULT_HOST = "https://localhost";
private const int DEFAULT_PORT = 5001;
private const string LOGIN_PATH = "/Account/Login";
private static bool _hasLoggedBaseUrl = false;

public LoginUITests(WebDriverFixture fixture)
{
_driver = fixture.Driver;
_baseUrl = Environment.GetEnvironmentVariable("BASE_URL") ?? $"{DEFAULT_HOST}:{DEFAULT_PORT}";
_url = $"{_baseUrl.TrimEnd('/')}{LOGIN_PATH}";

if (!_hasLoggedBaseUrl)
{
Console.WriteLine($"Using base URL: {_baseUrl}");
_hasLoggedBaseUrl = true;
}
}

[Fact]
public async Task Login_WithValidCredentials_ShouldRedirectToHomePage()
{
await _driver.Navigate().GoToUrlAsync(_url); // Added await
_driver.Navigate().GoToUrl(_url);
await _driver.Navigate().GoToUrlAsync(_url);

var usernameField = _driver.FindElement(By.Name("LoginInput.Username"));
var passwordField = _driver.FindElement(By.Name("LoginInput.Password"));
var loginButton = _driver.FindElement(By.CssSelector("button[type='submit']"));

// Use actual valid credentials
usernameField.SendKeys("user");
passwordField.SendKeys("password");
loginButton.Click();

// Wait for the URL to change to the expected page
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(4));
wait.Until(d => d.Url == "http://localhost/Movies");

Assert.Equal("http://localhost/Movies", _driver.Url);
var expectedUrl = $"{_baseUrl}/Movies";
wait.Until(d => d.Url == expectedUrl);

Assert.Equal(expectedUrl, _driver.Url);
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions RazorPagesMovie.UITests/WebDriverFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public WebDriverFixture()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AcceptInsecureCertificates = true; // Accept self-signed certificates
chromeOptions.AddArgument("--headless"); // Optional: Run Chrome headlessly for CI
chromeOptions.AddArgument("--headless"); // Optional: Run Chrome headlessly for CI/CD
chromeOptions.AddArgument("--no-sandbox"); // Optional: Bypass any OS security checks
chromeOptions.AddArgument("--disable-dev-shm-usage"); // Optional: Overcome limited resource problems
chromeOptions.AddArgument("--disable-gpu"); // Optional: Disable GPU
chromeOptions.AddArgument("--window-size=1920,1080"); // Optional: Set window size
// chromeOptions.AddArgument("--disable-dev-shm-usage"); // Optional: Overcome limited resource problems
// chromeOptions.AddArgument("--disable-gpu"); // Optional: Disable GPU
// chromeOptions.AddArgument("--window-size=1920,1080"); // Optional: Set window size
Driver = new ChromeDriver(chromeOptions);
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(4); // Set implicit wait
}
Expand Down

0 comments on commit 7b0044a

Please sign in to comment.