drupalGet(''); $session = $this->getSession(); $session->resizeWindow(400, 300); $javascript = <<assertJsCondition($javascript); } public function testAssertJsCondition() { $this->drupalGet(''); $session = $this->getSession(); $session->resizeWindow(500, 300); $javascript = <<expectException(AssertionFailedError::class); $this->assertJsCondition($javascript, 100); } /** * Tests creating screenshots. */ public function testCreateScreenshot() { $this->drupalGet(''); $this->createScreenshot('public://screenshot.jpg'); $this->assertFileExists('public://screenshot.jpg'); } /** * Tests assertEscaped() and assertUnescaped(). * * @see \Drupal\Tests\WebAssert::assertNoEscaped() * @see \Drupal\Tests\WebAssert::assertEscaped() */ public function testEscapingAssertions() { $assert = $this->assertSession(); $this->drupalGet('test-escaped-characters'); $assert->assertNoEscaped('
'); $assert->responseContains('
'); $assert->assertEscaped('Escaped: <"\'&>'); $this->drupalGet('test-escaped-script'); $assert->assertNoEscaped('
'); $assert->responseContains('
'); $assert->assertEscaped(""); $this->drupalGetWithAlert('test-unescaped-script'); $assert->assertNoEscaped('
'); $assert->responseContains('
'); $assert->responseContains(""); $assert->assertNoEscaped(""); } /** * Retrieves a Drupal path or an absolute path. * * @param string|\Drupal\Core\Url $path * Drupal path or URL to load into Mink controlled browser. * @param array $options * (optional) Options to be forwarded to the url generator. * @param string[] $headers * An array containing additional HTTP request headers, the array keys are * the header names and the array values the header values. This is useful * to set for example the "Accept-Language" header for requesting the page * in a different language. Note that not all headers are supported, for * example the "Accept" header is always overridden by the browser. For * testing REST APIs it is recommended to obtain a separate HTTP client * using getHttpClient() and performing requests that way. * * @return string * The retrieved HTML string, also available as $this->getRawContent() * * @see \Drupal\Tests\BrowserTestBase::getHttpClient() */ protected function drupalGetWithAlert($path, array $options = [], array $headers = []) { $options['absolute'] = TRUE; $url = $this->buildUrl($path, $options); $session = $this->getSession(); $this->prepareRequest(); foreach ($headers as $header_name => $header_value) { $session->setRequestHeader($header_name, $header_value); } $session->visit($url); // There are 2 alerts to accept before we can get the content of the page. $session->getDriver()->getWebdriverSession()->accept_alert(); $session->getDriver()->getWebdriverSession()->accept_alert(); $out = $session->getPage()->getContent(); // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); // Replace original page output with new output from redirected page(s). if ($new = $this->checkForMetaRefresh()) { $out = $new; // We are finished with all meta refresh redirects, so reset the counter. $this->metaRefreshCount = 0; } // Log only for JavascriptTestBase tests because for Goutte we log with // ::getResponseLogHandler. if ($this->htmlOutputEnabled && !($this->getSession()->getDriver() instanceof GoutteDriver)) { $html_output = 'GET request to: ' . $url . '
Ending URL: ' . $this->getSession()->getCurrentUrl(); $html_output .= '
' . $out; $html_output .= $this->getHtmlOutputHeaders(); $this->htmlOutput($html_output); } return $out; } }