Skip to content

Commit

Permalink
Merge pull request #3 from nshelms/master
Browse files Browse the repository at this point in the history
Refactored HighlightCeption.php to remove dependency on javascript libraries
  • Loading branch information
arzzen authored Sep 19, 2017
2 parents 6543092 + 94ad3ce commit 6226154
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 79 deletions.
96 changes: 35 additions & 61 deletions src/HighlightCeption.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use Codeception\TestInterface;
use Codeception\Exception\ConfigurationException;
use Codeception\Util\Locator;
use Facebook\WebDriver\WebDriverBy;

class HighlightCeption extends Module
{

/**
* Configuration array
*
*
* @var array
*/
protected $config = [
Expand All @@ -27,10 +28,10 @@ class HighlightCeption extends Module

/**
* Css style for hightlight text or element
*
* @var array
*
* @var string
*/
private $cssStyle = [];
private $cssStyle = "";

/**
* Time wait
Expand Down Expand Up @@ -63,8 +64,8 @@ public function _before(TestInterface $test)

$this->webDriverModule = $this->getModule($this->config['module']);
$this->webDriver = $this->webDriverModule->webDriver;
$this->cssStyle = $this->getInlineStyleStringFrom($this->config['cssStyle']);
$this->timeWait = floatval($this->config['timeWait']);
$this->cssStyle = json_encode($this->config['cssStyle']);
$this->test = $test;
}

Expand All @@ -85,102 +86,97 @@ public function _after(TestInterface $test)
*/
public function see($text, $selector = null)
{
$this->highlightText($text);
$this->webDriverModule->see($text, $selector);
$this->highlightText($text);
}

/**
* @inheritdoc
*/
public function seeElement($selector, $attributes = [])
{
$this->highlightElement($selector);
$this->webDriverModule->seeElement($selector, $attributes);
$this->highlightElement($selector);
}

/**
* @inheritdoc
*/
public function seeLink($text, $url = null)
{
$this->highlightText($text);
$this->webDriverModule->seeLink($text, $url);
$this->highlightText($text);
}

/**
* @inheritdoc
*/
public function seeInField($field, $value)
{
$this->highlightElement($field);
$this->webDriverModule->seeInField($field, $value);
$this->highlightElement($field);
}

/**
* @inheritdoc
*/
public function click($link, $context = null)
{
$this->highlightElement($context);
$this->webDriverModule->click($link, $context);
$this->highlightElement($context);
}

/**
* @inheritdoc
*/
public function clickWithLeftButton($cssOfXPath = null, $offsetX = null, $offsetY = null)
{
$this->highlightElement($cssOfXPath);
$this->webDriverModule->clickWithLeftButton($cssOfXPath, $offsetX, $offsetY);
$this->highlightElement($cssOfXPath);
}

/**
* @inheritdoc
*/
public function clickWithRightButton($cssOfXPath = null, $offsetX = null, $offsetY = null)
{
$this->highlightElement($cssOfXPath);
$this->webDriverModule->clickWithRightButton($cssOfXPath, $offsetX, $offsetY);
$this->highlightElement($cssOfXPath);
}

/**
* Highlight text on site
*
*
* @param string $text
*/
private function highlightText($text)
{
try {
$this->loadJQuery();
$this->debug('[Highlight Text] ' . $text);
$this->webDriverModule->executeJs('jQuery(document).ready(function (){
jQuery("body").highlight("' . $text . '");
' . sprintf('jQuery(".highlight").css(%s);', $this->cssStyle) . '
});');
$el = $this->webDriver->findElement(WebDriverBy::xpath("//*[text()[contains(., '{$text}')]]"));
$this->webDriver->executeScript("let str = arguments[0].innerHTML.replace(/({$text})/g, '<span style=\"{$this->cssStyle}\">$1</span>'); arguments[0].innerHTML = str;", [$el]);
} catch(Exception $e) {
$this->debug(sprintf("[Highlight Exception] %s \n%s", $e->getMessage(), $e->getTraceAsString()));
}
}

/**
* Highlight element on site
*
*
* @param string|array $selector
*/
private function highlightElement($selector)
{
try {
$locator = $this->getSelector($selector);
if ($locator) {
$this->loadJQuery();
if (Locator::isXPath($locator)) {
$this->loadJQueryXPath();
$this->debug('[Highlight XPath] ' . Locator::humanReadableString($locator));
$this->webDriverModule->executeJs(sprintf('jQuery(document).xpath("%s").css(%s);', addslashes($locator), $this->cssStyle));
$el = $this->webDriver->findElement(WebDriverBy::xpath($locator));
} else {
$this->debug('[Highlight Selector] ' . Locator::humanReadableString($locator));
$this->webDriverModule->executeJs(sprintf('jQuery("%s").css(%s);', addslashes($locator), $this->cssStyle));
}
// assume css
$el = $this->webDriver->findElement(WebDriverBy::cssSelector($locator));
}
$this->webDriver->executeScript("arguments[0].setAttribute('style', '{$this->cssStyle}')", [$el]);
}
} catch(Exception $e) {
$this->debug(sprintf("[Highlight Exception] %s \n%s", $e->getMessage(), $e->getTraceAsString()));
Expand All @@ -189,10 +185,10 @@ private function highlightElement($selector)

/**
* Resolve selector
*
*
* @param string|array $selector
* @return boolean
* @todo
* @todo
*/
private function getSelector($selector)
{
Expand Down Expand Up @@ -220,38 +216,16 @@ private function getSelector($selector)
}

/**
* Load jQuery
*/
private function loadJQuery()
{
if ($this->webDriver->executeScript('return !window.jQuery;')) {
$jQueryString = file_get_contents(__DIR__ . "/jquery.min.js");
$this->webDriver->executeScript($jQueryString);
$this->webDriver->executeScript('jQuery.noConflict();');
}
$this->loadJQueryHighlight();
}

/**
* Load jQuery.XPath
*/
private function loadJQueryXPath()
{
if ($this->webDriver->executeScript('return !window.jQuery.fn.xpath;')) {
$jQueryXPath = file_get_contents(__DIR__ . "/jquery.xpath.min.js");
$this->webDriver->executeScript($jQueryXPath);
}
}

/**
* Load jQuery.Highlight
*/
private function loadJQueryHighlight()
{
if ($this->webDriver->executeScript('return !window.jQuery.fn.highlight;')) {
$jQueryString = file_get_contents(__DIR__ . "/jquery.highlight.min.js");
$this->webDriver->executeScript($jQueryString);
}
* Converts a css style array to inline css style string
*
* @param array $cssStyleArray
* @return string Inline CSS style string
*/
private function getInlineStyleStringFrom($cssStyleArray) {
$inlineCss = "";
foreach ($cssStyleArray as $key=>$value) {
$inlineCss .= "{$key}: {$value};";
}
return $inlineCss;
}

}
1 change: 0 additions & 1 deletion src/jquery.highlight.min.js

This file was deleted.

4 changes: 0 additions & 4 deletions src/jquery.min.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/jquery.xpath.min.js

This file was deleted.

0 comments on commit 6226154

Please sign in to comment.