Skip to content

Conversation

liruohrh
Copy link

@liruohrh liruohrh commented Sep 6, 2025

for errChan cap is 1 and receive both event timeout err and callback err( block on the err for errChan handle receiving(wait#waitFunc) after callback send err(waiter#RunAndWait) ).

my solution of old code fix ( ExpectFileChooser )

code which maybe cause deadlock

	uploadFileTimeout := 30 * 1000.0
	fileChooser, err := page.ExpectFileChooser(func() error {
		err = page.Locator(".fileChooser-trigger").Click(playwright.LocatorClickOptions{
			Timeout: playwright.Float(40 * 1000),
		})
		if err != nil {
			return fmt.Errorf("trigger fileChooser: %w", err)
		}
		return nil
	}, playwright.PageExpectFileChooserOptions{
		Timeout: playwright.Float(uploadFileTimeout),
	})
	if err := fileChooser.SetFiles(filepaths); err != nil {
		return "", fmt.Errorf("set files: %w", err)
	}

solution

	stopped := false
	defer func() {
		stopped = true
	}()
	topContext := context.Background()
	uploadFileTimeout := 30 * time.Second
	ctx, cancel := context.WithTimeout(topContext, uploadFileTimeout)
	defer cancel()
	var once sync.Once
	fileChooserCh := make(chan playwright.FileChooser)
	page.OnFileChooser(func(chooser playwright.FileChooser) {
		once.Do(func() {
			if stopped {
				return
			}
			fileChooserCh <- chooser
		})
	})
	err = page.Locator(".fileChooser-trigger").Click(playwright.LocatorClickOptions{
		Timeout: playwright.Float(40 * 1000),
	})
	if err != nil {
		stopped = true
		return "", fmt.Errorf("trigger fileChooser: %w", err)
	}
	select {
	case <-ctx.Done():
		stopped = true
		return "", fmt.Errorf("wait file chooser: %w", playwright.ErrTimeout)
	case fileChooser := <-fileChooserCh:
		if err := fileChooser.SetFiles(filepaths); err != nil {
			return "", fmt.Errorf("set files: %w", err)
		}
	}

for errChan cap is 1 and receive both event timeout err and callback err( block on the err for errChan handle receiving(wait#waitFunc) after callback send err(waiter#RunAndWait) ).
@liruohrh
Copy link
Author

liruohrh commented Sep 8, 2025

solution2

	var triggerErr error
	fileChooser, err := page.ExpectFileChooser(func() error {
	        err := page.Locator(".fileChooser-trigger").Click(playwright.LocatorClickOptions{
		        Timeout: playwright.Float(40 * 1000),
	        })
		if err != nil {
			triggerErr = fmt.Errorf("click upload file button: %w", err)
		}
		return nil
	}, playwright.PageExpectFileChooserOptions{
		Timeout: playwright.Float(30000),
	})
	if err != nil {
		if triggerErr != nil {
			err = fmt.Errorf("wait file chooser: %w: %w", err, triggerErr)
		}
		return fmt.Errorf("wait file chooser: %w", err)
	}
	err = fileChooser.SetFiles(filepaths)
	if err != nil {
		return fmt.Errorf("set files: %w", err)
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant