Skip to content

Commit 3ba51d9

Browse files
authored
test: refactor devsandbox homepage test (codeready-toolchain#1256)
* refactor homepage_test * improve * improvements * improvements * requested changes * requested changes * improving
1 parent 3c9d868 commit 3ba51d9

5 files changed

Lines changed: 132 additions & 35 deletions

File tree

make/devsandbox-dashboard.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ endif
3030

3131
.PHONY: e2e-run-devsandbox-dashboard
3232
e2e-run-devsandbox-dashboard: HOST_NS=$(shell oc get projects -l app=host-operator --output=name -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | sort | tail -n 1)
33+
e2e-run-devsandbox-dashboard: MEMBER_NS=$(shell oc get projects -l app=member-operator --output=name -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | sort | tail -n 2 | head -n 1)
34+
e2e-run-devsandbox-dashboard: MEMBER_NS_2=$(shell oc get projects -l app=member-operator --output=name -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | sort | tail -n 2 | tail -n 1)
35+
e2e-run-devsandbox-dashboard: SECOND_MEMBER=$(shell if [ "$(MEMBER_NS)" != "$(MEMBER_NS_2)" ] && [ -n "$(MEMBER_NS_2)" ]; then echo "true"; else echo "false"; fi)
3336
e2e-run-devsandbox-dashboard: RHDH=https://rhdh-${DEVSANDBOX_DASHBOARD_NS}.$(shell oc get ingress.config.openshift.io/cluster -o jsonpath='{.spec.domain}')
3437
e2e-run-devsandbox-dashboard:
3538
@echo "Installing Firefox browser for Playwright..."
@@ -40,8 +43,7 @@ e2e-run-devsandbox-dashboard:
4043

4144
@echo "Running Developer Sandbox Dashboard e2e tests in firefox..."
4245
@SSO_USERNAME=$(SSO_USERNAME_READ) SSO_PASSWORD=$(SSO_PASSWORD_READ) BASE_URL=${RHDH} BROWSER=firefox envsubst < deploy/devsandbox-dashboard/ui-e2e-tests/.env > testsupport/devsandbox-dashboard/.env
43-
go test "./test/e2e/devsandbox-dashboard" -v -timeout=10m -failfast
44-
@oc delete usersignup $(SSO_USERNAME_READ) -n $(HOST_NS)
46+
@HOST_NS=$(HOST_NS) MEMBER_NS=$(MEMBER_NS) MEMBER_NS_2=$(MEMBER_NS_2) REGISTRATION_SERVICE_NS=$(HOST_NS) SECOND_MEMBER_MODE=$(SECOND_MEMBER) go test "./test/e2e/devsandbox-dashboard" -v -timeout=10m -failfast
4547

4648
@echo "The Developer Sandbox Dashboard e2e tests successfully finished"
4749

test/e2e/devsandbox-dashboard/homepage_test.go renamed to test/e2e/devsandbox-dashboard/fresh_signup_test.go

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,62 @@
11
package sandboxui
22

33
import (
4+
"context"
5+
"errors"
46
"regexp"
57
"testing"
68

9+
"github.com/codeready-toolchain/toolchain-e2e/testsupport"
10+
"github.com/codeready-toolchain/toolchain-e2e/testsupport/cleanup"
711
sandboxui "github.com/codeready-toolchain/toolchain-e2e/testsupport/devsandbox-dashboard"
812
"github.com/playwright-community/playwright-go"
913
"github.com/spf13/viper"
1014
"github.com/stretchr/testify/assert"
1115
"github.com/stretchr/testify/require"
1216
)
1317

14-
// TestHomepage tests the homepage layout and welcome text
15-
// when the user accesses the Developer Sandbox Dashboard for the first time
16-
func TestHomepage(t *testing.T) {
17-
page := sandboxui.Setup(t, "test-homepage")
18+
// TestFreshSignup tests the complete fresh signup flow:
19+
// 1. Homepage layout when first accessing the dashboard
20+
// 2. Clicking "Try it" to signup
21+
// 3. Accessing OpenShift after signup
22+
func TestFreshSignup(t *testing.T) {
23+
// Step 1: Setup the browser and login (LoadConfig called inside Setup)
24+
page := sandboxui.Setup(t, "test-fresh-signup")
25+
26+
// Ensure the user signup is not present in the system
27+
env := viper.GetString("ENVIRONMENT")
28+
username := viper.GetString("SSO_USERNAME")
29+
ensureNoUserSignup(t, env, username)
30+
31+
// Step 2: Verify homepage layout on first access
32+
verifyHomepage(t, page)
33+
34+
// Step 3: Perform signup by clicking "Try it"
35+
performSignup(t, page, env, username)
36+
37+
// Step 4: Verify OpenShift access
38+
verifyDevSandboxAccess(t, page, env)
39+
}
40+
41+
func ensureNoUserSignup(t *testing.T, env, username string) {
42+
if env == sandboxui.TestEnv {
43+
awaitilities := testsupport.WaitForDeployments(t)
44+
hostAwait := awaitilities.Host()
45+
userSignup, err := sandboxui.WaitForUserSignup(t, hostAwait, username)
46+
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
47+
require.NoError(t, err) // fail on unexpected errors
48+
}
49+
if userSignup != nil {
50+
// delete user signup
51+
err := sandboxui.DeleteUserSignup(t, hostAwait, userSignup)
52+
require.NoError(t, err)
53+
}
54+
}
55+
}
1856

57+
// verifyHomepage checks the homepage layout and welcome text
58+
// when the user accesses the Developer Sandbox Dashboard for the first time
59+
func verifyHomepage(t *testing.T, page playwright.Page) {
1960
homeLink := page.Locator("a").Filter(playwright.LocatorFilterOptions{
2061
HasText: "Home",
2162
})
@@ -43,11 +84,9 @@ func TestHomepage(t *testing.T) {
4384
require.NoError(t, err)
4485
}
4586

46-
// TestSignup tests the signup flow (automatically approved)
47-
// when the user clicks on "Try it" for the first time
48-
func TestSignup(t *testing.T) {
49-
page := sandboxui.Setup(t, "test-signup")
50-
87+
// performSignup executes the signup flow by clicking "Try it"
88+
// and waits for the signup to be approved
89+
func performSignup(t *testing.T, page playwright.Page, env, username string) {
5190
article := page.GetByRole("article")
5291
loadingIcon := page.Locator("svg.v5-MuiCircularProgress-svg").First()
5392

@@ -85,6 +124,15 @@ func TestSignup(t *testing.T) {
85124
err = tryItButton.Click()
86125
require.NoError(t, err)
87126

127+
if env == sandboxui.TestEnv {
128+
// add signup to cleanup
129+
awaitilities := testsupport.WaitForDeployments(t)
130+
hostAwait := awaitilities.Host()
131+
userSignup, err := sandboxui.WaitForUserSignup(t, hostAwait, username)
132+
require.NoError(t, err)
133+
cleanup.AddCleanTasks(t, hostAwait.Client, userSignup)
134+
}
135+
88136
// wait for loading icon to disappear
89137
err = loadingIcon.WaitFor(playwright.LocatorWaitForOptions{State: playwright.WaitForSelectorStateHidden})
90138
require.NoError(t, err)
@@ -100,18 +148,17 @@ func TestSignup(t *testing.T) {
100148
require.NoError(t, err)
101149

102150
trialText := article.GetByText("Your free trial expires in 30 days")
103-
err = trialText.WaitFor()
151+
err = trialText.WaitFor(playwright.LocatorWaitForOptions{
152+
Timeout: playwright.Float(30000),
153+
})
104154
require.NoError(t, err)
105155
}
106156

107-
// TestDevSandbox tests the access to Openshift after the user is signed up
108-
func TestDevSandbox(t *testing.T) {
109-
page := sandboxui.Setup(t, "test-devsandbox")
110-
env := viper.GetString("ENVIRONMENT")
111-
157+
// verifyDevSandboxAccess tests access to OpenShift after the user is signed up
158+
func verifyDevSandboxAccess(t *testing.T, page playwright.Page, env string) {
112159
imgName := "Red Hat OpenShift Service on"
113160
logMessage := "Log in with…"
114-
if env == sandboxui.UIE2ETestsEnv {
161+
if env == sandboxui.TestEnv {
115162
imgName = "Red Hat OpenShift"
116163
logMessage = "Log in with"
117164
}
@@ -135,14 +182,16 @@ func TestDevSandbox(t *testing.T) {
135182

136183
sandboxui.IsVisible(t, tryItBtn)
137184

138-
// open the article in a new popup and wait for it to fully load
185+
// open the "Try it" button in a new popup and wait for it to fully load
139186
devSandboxPage, err := sandboxui.ClickAndWaitForPopup(page, tryItBtn)
140187
require.NoError(t, err)
141188

142189
img := devSandboxPage.GetByRole("img", playwright.PageGetByRoleOptions{
143190
Name: imgName,
144191
})
145-
err = img.WaitFor()
192+
err = img.WaitFor(playwright.LocatorWaitForOptions{
193+
Timeout: playwright.Float(30000),
194+
})
146195
require.NoError(t, err)
147196

148197
h := devSandboxPage.GetByRole("heading", playwright.PageGetByRoleOptions{})

testsupport/devsandbox-dashboard/login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewLoginPage(page playwright.Page, environment string) *LoginPage {
2626
}
2727

2828
switch environment {
29-
case "dev":
29+
case DevEnv:
3030
lp.LoginUsernameLoc = page.GetByRole("textbox", playwright.PageGetByRoleOptions{
3131
Name: "Red Hat login",
3232
})
@@ -39,7 +39,7 @@ func NewLoginPage(page playwright.Page, environment string) *LoginPage {
3939
lp.LoginBtn = page.GetByRole("button", playwright.PageGetByRoleOptions{
4040
Name: "Log in",
4141
})
42-
case UIE2ETestsEnv:
42+
case TestEnv:
4343
lp.LoginUsernameLoc = page.GetByRole("textbox", playwright.PageGetByRoleOptions{
4444
Name: "Username or email",
4545
})
@@ -67,7 +67,7 @@ func (lp *LoginPage) Login(t *testing.T, loginUsername, loginPw string) {
6767
err := lp.LoginUsernameLoc.Fill(loginUsername)
6868
require.NoError(t, err)
6969

70-
if lp.Env == "dev" {
70+
if lp.Env == DevEnv {
7171
err := lp.NextBtn.Click()
7272
require.NoError(t, err)
7373
}

testsupport/devsandbox-dashboard/setup.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package sandboxui
22

33
import (
4-
"os"
54
"path/filepath"
5+
"runtime"
66
"sync"
77
"testing"
88

@@ -11,21 +11,31 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
const (
15+
TestEnv = "ui-e2e-tests"
16+
DevEnv = "dev"
17+
)
18+
1419
var (
1520
setupOnce sync.Once
16-
17-
UIE2ETestsEnv = "ui-e2e-tests"
1821
)
1922

23+
func LoadConfig(t *testing.T) {
24+
_, filename, _, ok := runtime.Caller(0)
25+
require.True(t, ok)
26+
27+
configPath := filepath.Join(filepath.Dir(filename), ".env")
28+
viper.SetConfigFile(configPath)
29+
30+
err := viper.ReadInConfig()
31+
require.NoError(t, err)
32+
33+
viper.AutomaticEnv()
34+
}
35+
2036
func Setup(t *testing.T, testName string) playwright.Page {
2137
setupOnce.Do(func() {
22-
dir, err := os.Getwd()
23-
require.NoError(t, err)
24-
25-
viper.SetConfigFile(filepath.Join(dir, "../../../testsupport/devsandbox-dashboard/.env"))
26-
err = viper.ReadInConfig()
27-
require.NoError(t, err)
28-
viper.AutomaticEnv()
38+
LoadConfig(t)
2939
})
3040

3141
env := viper.GetString("ENVIRONMENT")
@@ -39,7 +49,7 @@ func Setup(t *testing.T, testName string) playwright.Page {
3949
browser := launchBrowser(t, pw)
4050

4151
opts := playwright.BrowserNewContextOptions{}
42-
if env == UIE2ETestsEnv {
52+
if env == TestEnv {
4353
opts.IgnoreHttpsErrors = playwright.Bool(true)
4454
}
4555

@@ -55,7 +65,7 @@ func Setup(t *testing.T, testName string) playwright.Page {
5565
login := NewLoginPage(page, env)
5666
login.Navigate(t, baseURL)
5767

58-
if env == "dev" {
68+
if env == DevEnv {
5969
// handle cookie consent
6070
// on dev environment, the cookie consent appears after the login page is loaded
6171
handleCookiesConsent(t, page)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sandboxui
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
9+
"github.com/codeready-toolchain/toolchain-e2e/testsupport/wait"
10+
"k8s.io/apimachinery/pkg/api/errors"
11+
)
12+
13+
// WaitForUserSignup retrieves the UserSignup for the configured SSO username
14+
func WaitForUserSignup(t *testing.T, hostAwait *wait.HostAwaitility, username string) (*toolchainv1alpha1.UserSignup, error) {
15+
return hostAwait.WithRetryOptions(wait.TimeoutOption(time.Minute*2)).WaitForUserSignup(t, username,
16+
wait.UntilUserSignupHasConditions(wait.ConditionSet(wait.Default(), wait.ApprovedAutomatically())...),
17+
wait.UntilUserSignupHasStateLabel(toolchainv1alpha1.UserSignupStateLabelValueApproved))
18+
}
19+
20+
// DeleteUserSignup deletes the UserSignup for the configured SSO username
21+
// and waits until it's fully removed from the cluster
22+
func DeleteUserSignup(t *testing.T, hostAwait *wait.HostAwaitility, userSignup *toolchainv1alpha1.UserSignup) error {
23+
username := userSignup.Name
24+
25+
t.Logf("Deleting UserSignup %s", username)
26+
27+
if err := hostAwait.Client.Delete(context.TODO(), userSignup); err != nil {
28+
if !errors.IsNotFound(err) {
29+
return err
30+
}
31+
}
32+
33+
// Wait until deletion is complete
34+
t.Logf("Waiting until UserSignup %s is completely deleted", username)
35+
return hostAwait.WaitUntilUserSignupDeleted(t, username)
36+
}

0 commit comments

Comments
 (0)