grafana/k6 · error

getting page title: expected string, got %T

Error message

getting page title: expected string, got %T

What it means

Thrown by Page.Title() when the result of evaluating `() => document.title` is not a Go string. In JavaScript document.title is always a string, so in practice this type assertion (v.(string)) fails only when the evaluation returns a non-string sobek/Go value — for example an error object or a serialized wrapper from the evaluation bridge. It is a defensive invariant check, not an expected runtime condition.

Solutions

  1. Upgrade to the latest k6 release — this indicates the evaluation bridge returned an unexpected type, likely a fixed bug.
  2. Reproduce with a minimal script (page.goto + page.title()) and report it with the k6 version to the k6 issue tracker.
  3. As a workaround, read the title via page.evaluate(() => document.title) and coerce with String() in your script.

Example fix

// before
const title = page.title(); // internal assertion failure

// after (workaround in test script)
const title = String(page.evaluate(() => document.title));
Defensive patterns

Strategy: fallback

Try / catch

let title;
try {
  title = page.title();
} catch (e) {
  if (/expected string/i.test(String(e))) {
    title = String(page.evaluate(() => document.title)); // fallback read
  } else throw e;
}

Prevention

When it happens

Trigger: The Evaluate call succeeds but returns a value that is not a string — e.g. undefined/null leaked through an evaluation bridge bug, or a sobek value wrapper that does not convert to string. Extremely rare; indicates a k6/browser bridge issue rather than a user-script problem.

Common situations: Almost never seen in normal use; if it appears, it usually coincides with a k6 browser module version regression or an exotic page whose title evaluation returns a proxy/undefined during teardown.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/96abb81f682c8777. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/page.go:1803

// Timeout will return the default timeout or the one set by the user.
// It's an internal method not to be exposed as a JS API.
func (p *Page) Timeout() time.Duration {
	return p.defaultTimeout()
}

// Title returns the page title.
func (p *Page) Title() (string, error) {
	p.logger.Debugf("Page:Title", "sid:%v", p.sessionID())

	js := `() => document.title`
	v, err := p.Evaluate(js)
	if err != nil {
		return "", fmt.Errorf("getting page title: %w", err)
	}
	s, ok := v.(string)
	if !ok {
		return "", fmt.Errorf("getting page title: expected string, got %T", v)
	}

	return s, nil
}

// ThrottleCPU will slow the CPU down from chrome's perspective to simulate
// a test being run on a slower device.
func (p *Page) ThrottleCPU(cpuProfile CPUProfile) error {
	p.logger.Debugf("Page:ThrottleCPU", "sid:%v", p.sessionID())

	p.frameSessionsMu.RLock()
	defer p.frameSessionsMu.RUnlock()

	for _, fs := range p.frameSessions {
		if err := fs.throttleCPU(cpuProfile); err != nil {
			return err
		}
	}

View on GitHub (pinned to 3fcf5388d7)