11import { chromium , Page , Browser , BrowserContext } from "playwright"
22
3- // NOTE: this is hard-coded and passed as an environment variable
4- // See the test job in ci.yml
5- const PASSWORD = "e45432jklfdsab"
6-
73describe ( "login" , ( ) => {
84 let browser : Browser
95 let page : Page
106 let context : BrowserContext
117
128 beforeAll ( async ( ) => {
13- browser = await chromium . launch ( { headless : false } )
9+ browser = await chromium . launch ( )
1410 context = await browser . newContext ( )
1511 } )
1612
1713 afterAll ( async ( ) => {
1814 await browser . close ( )
15+ await context . close ( )
1916 } )
2017
2118 beforeEach ( async ( ) => {
@@ -29,22 +26,32 @@ describe("login", () => {
2926 } )
3027
3128 it ( "should see a 'Go Home' button in the Application Menu that goes to coder.com" , async ( ) => {
32- await page . goto ( "http://localhost:8080" )
29+ // waitUntil: "networkidle"
30+ // In case the page takes a long time to load
31+ await page . goto ( process . env . CODE_SERVER_ADDRESS || "http://localhost:8080" , { waitUntil : "networkidle" } )
3332 // Type in password
34- await page . fill ( ".password" , PASSWORD )
33+ await page . fill ( ".password" , process . env . PASSWORD || "password" )
3534 // Click the submit button and login
3635 await page . click ( ".submit" )
37- // Click the Applicaiton menu
36+ // Click the Application menu
3837 await page . click ( ".menubar-menu-button[title='Application Menu']" )
3938 // See the Go Home button
40- const goHomeButton = ".home-bar [aria-label='Home'] li "
39+ const goHomeButton = "a.action-menu-item span [aria-label='Go Home']"
4140 expect ( await page . isVisible ( goHomeButton ) )
42- // Hover over element without clicking
43- await page . hover ( goHomeButton )
44- // Click the top left corner of the element
41+ // Click it and navigate to coder.com
4542 await page . click ( goHomeButton )
46- // Note: we have to click on <li> in the Go Home button for it to work
47- // Land on coder.com
48- // expect(await page.url()).toBe("https://coder.com/")
43+
44+ // If there are unsaved changes it will show a dialog
45+ // asking if you're sure you want to leave
46+ page . on ( "dialog" , ( dialog ) => dialog . accept ( ) )
47+
48+ // We give it a second to load in case playwright moves too quickly
49+ // This resolves after 'networkidle'
50+ await page . waitForLoadState ( "networkidle" )
51+
52+ // We do this rather than using something like http://coder.com or an outside url
53+ // so that it's guaranteed to pass everytime
54+ expect ( await page . url ( ) ) . toBe ( `${ process . env . CODE_SERVER_ADDRESS } /test-home` )
55+ expect ( await page . title ( ) ) . toBe ( `404 - code-server` )
4956 } )
5057} )
0 commit comments