ExampleSelenium2DriverTest.php 2.15 KB
Newer Older
1
2
3
<?php

// Use your module's testing namespace such as the one below.
4
namespace Drupal\Tests\moduleName\ExampleSelenium2DriverTest;
5

6
7
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Vocabulary;
8
use Drupal\user\Entity\User;
9
use weitzman\DrupalTestTraits\ExistingSiteSelenium2DriverTestBase;
10
11
12
13

/**
 * A WebDriver test suitable for testing Ajax and client-side interactions.
 */
14
class ExampleSelenium2DriverTest extends ExistingSiteSelenium2DriverTestBase
15
{
16
    public function testContentCreation()
17
    {
18
19
      // Create a taxonomy term. Will be automatically cleaned up at the end of the test.
        $web_assert = $this->assertSession();
20
21
22
        $vocab = Vocabulary::load('tags');
        $this->createTerm($vocab, ['name' => 'Term 1']);
        $this->createTerm($vocab, ['name' => 'Term 2']);
23
24
25
        $admin = User::load(1);
        $admin->passRaw = 'password';
        $this->drupalLogin($admin);
26

27
28
      // @codingStandardsIgnoreStart
      // These lines are left here as examples of how to debug requests.
29
30
      // \weitzman\DrupalTestTraits\ScreenShotTrait::captureScreenshot();
      // $this->capturePageContent();
31
      // @codingStandardsIgnoreStop
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
      // Test autocomplete on article creation.
      $this->drupalGet('/node/add/article');
      $page = $this->getCurrentPage();
      $page->fillField('title[0][value]', 'Article Title');
      $tags = $page->findField('field_tags[target_id]');
      $tags->setValue('Ter');
      $tags->keyDown('m');
      $result = $web_assert->waitForElementVisible('css', '.ui-autocomplete li');
      $this->assertNotNull($result);
      // Click the autocomplete option
      $result->click();
      // Verify that correct the input is selected.
      $web_assert->pageTextContains('Term 1');
      $submit_button = $page->findButton('Save');
      $submit_button->press();
      // Verify the URL and get the nid.
      $this->assertTrue((bool) preg_match('/.+node\/(?P<nid>\d+)/', $this->getUrl(), $matches));
      $node = Node::load($matches['nid']);
      $this->markEntityForCleanup($node);
      // Verify the text on the page.
      $web_assert->pageTextContains('Article Title');
      $web_assert->pageTextContains('Term 1');
  }
56
}