# Instructions

- Following Playwright test failed.
- Explain why, be concise, respect Playwright best practices.
- Provide a snippet of code with the fix, if possible.

# Test info

- Name: tests/ui/entities.spec.ts >> state profile - dynamic candidates >> state profile loads candidates filtered to state
- Location: tests/ui/entities.spec.ts:264:7

# Error details

```
TimeoutError: page.waitForResponse: Timeout 15000ms exceeded while waiting for event "response"
```

```
Error: page.goto: net::ERR_ABORTED; maybe frame was detached?
Call log:
  - navigating to "http://10.1.74.226/states/tx/", waiting until "load"

```

# Test source

```ts
  172 |     expect(indexResponse?.status(), '/elections/ should answer 200').toBe(200);
  173 |     await expect(page.locator('h1')).toContainText(/Election/i);
  174 | 
  175 |     // Wait for GraphQL browseEntities to populate results
  176 |     await expect(page.locator('.entity-search-result').first()).toBeVisible({ timeout: DATA_LOAD_TIMEOUT });
  177 | 
  178 |     const links = await page.locator('.entity-search-result').all();
  179 |     expect(links.length).toBeGreaterThan(0);
  180 | 
  181 |     const hrefs = await Promise.all(links.slice(0, 20).map(l => l.getAttribute('href')));
  182 |     const randomLinks = getRandomItems(hrefs.filter(Boolean) as string[], 3);
  183 | 
  184 |     for (const href of randomLinks) {
  185 |       const entityResponse = await page.goto(toRelativePath(href));
  186 |       expect(entityResponse?.status(), `${toRelativePath(href)} should answer 200`).toBe(200);
  187 |       await expect(page.locator('h1')).toBeVisible();
  188 |     }
  189 |   });
  190 | });
  191 | 
  192 | test.describe('office profile - dynamic candidates', () => {
  193 |   // President of the United States office page
  194 |   const PRESIDENT_PATH = '/offices/684f6e2d-7105-932b-8b7f-94016580f727/';
  195 | 
  196 |   test('office profile loads candidates filtered to office', async ({ page }) => {
  197 |     // Intercept the browseEntities GraphQL request to verify filter is applied
  198 |     const responsePromise = page.waitForResponse(resp => {
  199 |       if (!resp.url().includes('/api/graphql')) return false;
  200 |       if (resp.request().method() !== 'POST') return false;
  201 |       const body = resp.request().postData() || '';
  202 |       return body.includes('browseEntities');
  203 |     }, { timeout: DATA_LOAD_TIMEOUT });
  204 | 
  205 |     await page.goto(PRESIDENT_PATH);
  206 |     const response = await responsePromise;
  207 | 
  208 |     // Verify the request includes the office filter
  209 |     const requestBody = JSON.parse(response.request().postData() || '{}');
  210 |     expect(requestBody.variables?.filter).toContain('office:P');
  211 | 
  212 |     // Verify the response has results
  213 |     const json = await response.json();
  214 |     const results = json.data?.browseEntities?.results || [];
  215 |     expect(results.length).toBeGreaterThan(0);
  216 |   });
  217 | 
  218 |   test('office profile candidates sorted by total contributions descending', async ({ page }) => {
  219 |     const responsePromise = page.waitForResponse(resp => {
  220 |       if (!resp.url().includes('/api/graphql')) return false;
  221 |       if (resp.request().method() !== 'POST') return false;
  222 |       const body = resp.request().postData() || '';
  223 |       return body.includes('browseEntities');
  224 |     }, { timeout: DATA_LOAD_TIMEOUT });
  225 | 
  226 |     await page.goto(PRESIDENT_PATH);
  227 |     const response = await responsePromise;
  228 | 
  229 |     const json = await response.json();
  230 |     const results = json.data?.browseEntities?.results || [];
  231 |     expect(results.length).toBeGreaterThan(1);
  232 | 
  233 |     // Extract total_contributions from metadata to verify sort order
  234 |     const contributions = results.map((r: { metadata?: string }) => {
  235 |       try {
  236 |         const meta = JSON.parse(r.metadata || '{}');
  237 |         return meta.total_contributions || 0;
  238 |       } catch { return 0; }
  239 |     });
  240 | 
  241 |     for (let i = 1; i < contributions.length; i++) {
  242 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  243 |     }
  244 |   });
  245 | 
  246 |   test('office profile renders candidate results in UI', async ({ page }) => {
  247 |     await page.goto(PRESIDENT_PATH);
  248 | 
  249 |     // Wait for candidate results to render
  250 |     await expect(page.locator('.entity-search-result').first()).toBeVisible({ timeout: DATA_LOAD_TIMEOUT });
  251 | 
  252 |     const results = await page.locator('.entity-search-result').all();
  253 |     expect(results.length).toBeGreaterThan(0);
  254 | 
  255 |     // First result should be a well-known presidential candidate with high contributions
  256 |     const firstResultText = await results[0].textContent();
  257 |     expect(firstResultText).toMatch(/\$/); // Should show dollar amount
  258 |   });
  259 | });
  260 | 
  261 | test.describe('state profile - dynamic candidates', () => {
  262 |   const TEXAS_PATH = '/states/tx/';
  263 | 
  264 |   test('state profile loads candidates filtered to state', async ({ page }) => {
  265 |     const responsePromise = page.waitForResponse(resp => {
  266 |       if (!resp.url().includes('/api/graphql')) return false;
  267 |       if (resp.request().method() !== 'POST') return false;
  268 |       const body = resp.request().postData() || '';
  269 |       return body.includes('browseEntities');
  270 |     }, { timeout: DATA_LOAD_TIMEOUT });
  271 | 
> 272 |     await page.goto(TEXAS_PATH);
      |                ^ Error: page.goto: net::ERR_ABORTED; maybe frame was detached?
  273 |     const response = await responsePromise;
  274 | 
  275 |     const requestBody = JSON.parse(response.request().postData() || '{}');
  276 |     expect(requestBody.variables?.filter).toContain('office_state:TX');
  277 | 
  278 |     const json = await response.json();
  279 |     const results = json.data?.browseEntities?.results || [];
  280 |     expect(results.length).toBeGreaterThan(0);
  281 |   });
  282 | 
  283 |   test('state profile candidates sorted by total contributions descending', async ({ page }) => {
  284 |     const responsePromise = page.waitForResponse(resp => {
  285 |       if (!resp.url().includes('/api/graphql')) return false;
  286 |       if (resp.request().method() !== 'POST') return false;
  287 |       const body = resp.request().postData() || '';
  288 |       return body.includes('browseEntities');
  289 |     }, { timeout: DATA_LOAD_TIMEOUT });
  290 | 
  291 |     await page.goto(TEXAS_PATH);
  292 |     const response = await responsePromise;
  293 | 
  294 |     const json = await response.json();
  295 |     const results = json.data?.browseEntities?.results || [];
  296 |     expect(results.length).toBeGreaterThan(1);
  297 | 
  298 |     const contributions = results.map((r: { metadata?: string }) => {
  299 |       try {
  300 |         const meta = JSON.parse(r.metadata || '{}');
  301 |         return meta.total_contributions || 0;
  302 |       } catch { return 0; }
  303 |     });
  304 | 
  305 |     for (let i = 1; i < contributions.length; i++) {
  306 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  307 |     }
  308 |   });
  309 | });
  310 | 
  311 | test.describe('district profile - dynamic candidates', () => {
  312 |   const DISTRICT_PATH = '/districts/tx-10/';
  313 | 
  314 |   test('district profile loads candidates filtered to district', async ({ page }) => {
  315 |     const responsePromise = page.waitForResponse(resp => {
  316 |       if (!resp.url().includes('/api/graphql')) return false;
  317 |       if (resp.request().method() !== 'POST') return false;
  318 |       const body = resp.request().postData() || '';
  319 |       return body.includes('browseEntities');
  320 |     }, { timeout: DATA_LOAD_TIMEOUT });
  321 | 
  322 |     await page.goto(DISTRICT_PATH);
  323 |     const response = await responsePromise;
  324 | 
  325 |     const requestBody = JSON.parse(response.request().postData() || '{}');
  326 |     const filter = requestBody.variables?.filter || '';
  327 |     expect(filter).toContain('office:H');
  328 |     expect(filter).toContain('office_state:TX');
  329 |     expect(filter).toContain('office_district:10');
  330 | 
  331 |     const json = await response.json();
  332 |     const results = json.data?.browseEntities?.results || [];
  333 |     expect(results.length).toBeGreaterThan(0);
  334 |   });
  335 | 
  336 |   test('district profile candidates sorted by total contributions descending', async ({ page }) => {
  337 |     const responsePromise = page.waitForResponse(resp => {
  338 |       if (!resp.url().includes('/api/graphql')) return false;
  339 |       if (resp.request().method() !== 'POST') return false;
  340 |       const body = resp.request().postData() || '';
  341 |       return body.includes('browseEntities');
  342 |     }, { timeout: DATA_LOAD_TIMEOUT });
  343 | 
  344 |     await page.goto(DISTRICT_PATH);
  345 |     const response = await responsePromise;
  346 | 
  347 |     const json = await response.json();
  348 |     const results = json.data?.browseEntities?.results || [];
  349 |     expect(results.length).toBeGreaterThan(1);
  350 | 
  351 |     const contributions = results.map((r: { metadata?: string }) => {
  352 |       try {
  353 |         const meta = JSON.parse(r.metadata || '{}');
  354 |         return meta.total_contributions || 0;
  355 |       } catch { return 0; }
  356 |     });
  357 | 
  358 |     for (let i = 1; i < contributions.length; i++) {
  359 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  360 |     }
  361 |   });
  362 | });
  363 | 
  364 | // Helper to intercept browseEntities GraphQL response and extract score values
  365 | async function getBrowseScores(page: import('@playwright/test').Page, url: string): Promise<number[]> {
  366 |   const responsePromise = page.waitForResponse(resp => {
  367 |     if (!resp.url().includes('/api/graphql')) return false;
  368 |     if (resp.request().method() !== 'POST') return false;
  369 |     const body = resp.request().postData() || '';
  370 |     return body.includes('browseEntities');
  371 |   }, { timeout: DATA_LOAD_TIMEOUT });
  372 |   await page.goto(url);
```