@@ -56,7 +56,7 @@ export const before = async ({ page }: { page: Page }) => {
5656
5757 // Sometimes we run the test server on a different port, but we should
5858 // only change the drive if it is non-default.
59- if ( SERVER_URL !== 'http://localhost:9883' ) {
59+ if ( SERVER_URL !== FRONTEND_URL ) {
6060 await changeDrive ( SERVER_URL , page ) ;
6161 }
6262
@@ -260,8 +260,21 @@ export async function openNewSubjectWindow(browser: Browser, url: string) {
260260 await page . goto ( FRONTEND_URL ) ;
261261
262262 // Only when we run on `localhost` we don't need to change drive during tests
263- if ( SERVER_URL !== defaultDevServer ) {
264- await changeDrive ( SERVER_URL , page ) ;
263+ if ( SERVER_URL !== FRONTEND_URL ) {
264+ try {
265+ await page . waitForSelector ( '[data-test="sidebar-drive-open"]' , {
266+ timeout : 5000 ,
267+ } ) ;
268+ await changeDrive ( SERVER_URL , page ) ;
269+ } catch ( error ) {
270+ console . error ( 'Error changing drive in new window:' , error ) ;
271+ // Try reloading the page if the sidebar drive element is not found
272+ await page . reload ( ) ;
273+ await page . waitForSelector ( '[data-test="sidebar-drive-open"]' , {
274+ timeout : 5000 ,
275+ } ) ;
276+ await changeDrive ( SERVER_URL , page ) ;
277+ }
265278 }
266279
267280 await openSubject ( page , url ) ;
@@ -283,13 +296,83 @@ export async function openConfigureDrive(page: Page) {
283296}
284297
285298export async function changeDrive ( subject : string , page : Page ) {
286- await openConfigureDrive ( page ) ;
287- await expect ( page . locator ( 'text=Drive Configuration' ) ) . toBeVisible ( ) ;
288- await page . fill ( '[data-test="server-url-input"]' , subject ) ;
289- await page . click ( '[data-test="server-url-save"]' ) ;
290- await expect (
291- page . getByRole ( 'heading' , { name : 'Default Ontology' } ) ,
292- ) . toBeVisible ( ) ;
299+ try {
300+ // Check if the current drive matches the requested subject using both methods
301+ if ( await isCurrentDrive ( subject , page ) ) {
302+ return ;
303+ }
304+
305+ // Also check the drive title text
306+ const driveTitleText = await currentDriveTitle ( page ) . textContent ( ) ;
307+ // Get the domain from the subject to compare with the drive title
308+ const subjectDomain = new URL ( subject ) . hostname ;
309+ if ( driveTitleText && driveTitleText . trim ( ) . includes ( subjectDomain ) ) {
310+ return ;
311+ }
312+
313+ const sidebarDriveOpen = page . locator ( '[data-test="sidebar-drive-open"]' ) ;
314+ if ( await sidebarDriveOpen . isVisible ( ) ) await openConfigureDrive ( page ) ;
315+ await expect ( page . locator ( 'text=Drive Configuration' ) ) . toBeVisible ( ) ;
316+ await page . fill ( '[data-test="server-url-input"]' , subject ) ;
317+ await page . click ( '[data-test="server-url-save"]' ) ;
318+ await expect (
319+ page . getByRole ( 'heading' , { name : 'Default Ontology' } ) ,
320+ ) . toBeVisible ( ) ;
321+ } catch ( error ) {
322+ console . error ( 'Error in changeDrive:' , error ) ;
323+ throw error ;
324+ }
325+ }
326+
327+ /**
328+ * Checks if the current drive matches the given URL
329+ * @param url The URL to compare with the current drive
330+ * @param page The Playwright Page object
331+ * @returns True if the current drive matches the URL
332+ */
333+ export async function isCurrentDrive (
334+ url : string ,
335+ page : Page ,
336+ ) : Promise < boolean > {
337+ try {
338+ const sidebarDriveOpen = page . locator ( '[data-test="sidebar-drive-open"]' ) ;
339+
340+ if ( ! ( await sidebarDriveOpen . isVisible ( ) ) ) {
341+ return false ;
342+ }
343+
344+ // Get the title attribute which contains the current drive URL
345+ const titleAttr = await sidebarDriveOpen . getAttribute ( 'title' ) ;
346+
347+ if ( ! titleAttr ) {
348+ return false ;
349+ }
350+
351+ // Extract the URL from the title attribute
352+ // Format: "Your current baseURL is {url}"
353+ const currentUrl = titleAttr . replace ( 'Your current baseURL is ' , '' ) ;
354+
355+ // Normalize URLs for comparison (remove trailing slashes and protocol)
356+ const normalizeUrl = ( urlString : string ) : string => {
357+ try {
358+ // Remove trailing slashes
359+ const cleanUrl = urlString . replace ( / \/ $ / , '' ) ;
360+ const urlObj = new URL ( cleanUrl ) ;
361+ // Compare only hostname and path, ignoring protocol
362+ return `${ urlObj . hostname } ${ urlObj . pathname } ` ;
363+ } catch ( e ) {
364+ return urlString . replace ( / \/ $ / , '' ) ;
365+ }
366+ } ;
367+
368+ const normalizedCurrentUrl = normalizeUrl ( currentUrl ) ;
369+ const normalizedUrl = normalizeUrl ( url ) ;
370+
371+ return normalizedCurrentUrl === normalizedUrl ;
372+ } catch ( error ) {
373+ console . error ( 'Error in isCurrentDrive:' , error ) ;
374+ return false ;
375+ }
293376}
294377
295378export async function editTitle ( title : string , page : Page ) {
0 commit comments