You are here

public function SelectorsContext::addSelectorNameForCssSelector in Varbase: The Ultimate Drupal CMS Starter Kit (Bootstrap Ready) 8.7

Same name and namespace in other branches
  1. 8.8 tests/features/bootstrap/SelectorsContext.php \SelectorsContext::addSelectorNameForCssSelector()
  2. 8.4 tests/features/bootstrap/SelectorsContext.php \SelectorsContext::addSelectorNameForCssSelector()
  3. 8.5 tests/features/bootstrap/SelectorsContext.php \SelectorsContext::addSelectorNameForCssSelector()
  4. 8.6 tests/features/bootstrap/SelectorsContext.php \SelectorsContext::addSelectorNameForCssSelector()
  5. 9.0.x tests/features/bootstrap/SelectorsContext.php \SelectorsContext::addSelectorNameForCssSelector()

#Selector : To add a new selector name with a css selector.

Exmaple 1: When I add "mobile logo" selector for "header img#logo" css selector Example 2: And I add "breadcrumb" selector for ".breadcrumb" css selector Example 3: And I add "breadcrumb first link" selector for ".breadcrumb li:nth-child(1) a" css selector.

You could add in the behat.yml file so that you do not need to add the most general selectors in your features. default: suites: default: contexts:

  • SelectorsContext: parameters: selectors: css: breadcrumb first link: ".breadcrumb li:nth-child(1) a" xpath: page title: "//h1[contains(@class, 'page-header')"

@When /^I add "(?P<selectorName>[^"]*)" selector for "(?P<cssSelecter>[^"]*)" css selector$/

File

tests/features/bootstrap/SelectorsContext.php, line 155

Class

SelectorsContext
Defines application features from the specific context.

Code

public function addSelectorNameForCssSelector($selectorName, $cssSelecter) {
  if (!empty($selectorName) && $selectorName != '' && !empty($cssSelecter) && $cssSelecter != '') {

    // Add the selector name for the css selector to the selectors array.
    $this->cssSelectors[$selectorName] = $cssSelecter;

    // Translate the CSS selector to XPath selector.
    $css = new CssSelector();
    $xpathSelector = $css
      ->translateToXPath($cssSelecter);

    // Registor the name for the XPath selector.
    $selectorHandler = $this
      ->getSession()
      ->getSelectorsHandler()
      ->getSelector('named');
    $selectorHandler
      ->registerNamedXpath($selectorName, $xpathSelector);
  }
  else {
    throw new Exception('The selector name and the CSS selector must not be empty.');
  }
}