Commit c15bcc06 authored by Dries's avatar Dries

- Patch #297869 by boombatower: add xpath method to simpletest and refactor existing tests.

parent f0b1d9f8
......@@ -876,7 +876,7 @@ function drupalPost($path, $edit, $submit, $options = array()) {
if ($this->parse()) {
$edit_save = $edit;
// Let's iterate over all the forms.
$forms = $this->elements->xpath('//form');
$forms = $this->xpath('//form');
foreach ($forms as $form) {
// We try to set the fields of this form as specified in $edit.
$edit = $edit_save;
......@@ -1067,6 +1067,24 @@ protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
return $submit_matches;
}
/**
* Peform an xpath search on the contents of the internal browser. The search
* is relative to the root element (HTML tag normally) of the page.
*
* @param $xpath
* The xpath string to use in the search.
* @return
* The return value of the xpath search. For details on the xpath string
* format and return values see the SimpleXML documentation.
* http://us.php.net/manual/function.simplexml-element-xpath.php
*/
public function xpath($xpath) {
if ($this->parse()) {
return $this->elements->xpath($xpath);
}
return FALSE;
}
/**
* Get all option elements, including nested options, in a select.
*
......@@ -1108,15 +1126,13 @@ private function getAllOptions(SimpleXMLElement $element) {
function clickLink($label, $index = 0) {
$url_before = $this->getUrl();
$ret = FALSE;
if ($this->parse()) {
$urls = $this->elements->xpath('//a[text()="' . $label . '"]');
if (isset($urls[$index])) {
$url_target = $this->getAbsoluteUrl($urls[$index]['href']);
$curl_options = array(CURLOPT_URL => $url_target);
$ret = $this->curlExec($curl_options);
}
$this->assertTrue($ret, t('Clicked link !label (!url_target) from !url_before', array('!label' => $label, '!url_target' => $url_target, '!url_before' => $url_before)), t('Browser'));
$urls = $this->xpath('//a[text()="' . $label . '"]');
if (isset($urls[$index])) {
$url_target = $this->getAbsoluteUrl($urls[$index]['href']);
$curl_options = array(CURLOPT_URL => $url_target);
$ret = $this->curlExec($curl_options);
}
$this->assertTrue($ret, t('Clicked link !label (!url_target) from !url_before', array('!label' => $label, '!url_target' => $url_target, '!url_before' => $url_before)), t('Browser'));
return $ret;
}
......@@ -1322,7 +1338,7 @@ function assertNoPattern($pattern, $message = '%s', $group = 'Other') {
* TRUE on pass, FALSE on fail.
*/
function assertTitle($title, $message, $group = 'Other') {
return $this->_assert($this->parse() && $this->elements->xpath('//title[text()="' . $title . '"]'), $message, $group);
return $this->_assert($this->xpath('//title[text()="' . $title . '"]') !== FALSE, $message, $group);
}
/**
......@@ -1340,18 +1356,17 @@ function assertTitle($title, $message, $group = 'Other') {
* TRUE on pass, FALSE on fail.
*/
function assertFieldByXPath($xpath, $value, $message, $group = 'Other') {
$fields = array();
if ($this->parse()) {
$fields = $this->elements->xpath($xpath);
}
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if ($value) {
$found = FALSE;
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
}
}
}
}
......@@ -1373,18 +1388,17 @@ function assertFieldByXPath($xpath, $value, $message, $group = 'Other') {
* TRUE on pass, FALSE on fail.
*/
function assertNoFieldByXPath($xpath, $value, $message, $group = 'Other') {
$fields = array();
if ($this->parse()) {
$fields = $this->elements->xpath($xpath);
}
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if ($value) {
$found = FALSE;
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
}
}
}
}
......
......@@ -190,7 +190,7 @@ class SimpleTestTestCase extends DrupalWebTestCase {
* @return fieldset containing the results for group this test is in.
*/
function getResultFieldSet() {
$fieldsets = $this->elements->xpath('//fieldset');
$fieldsets = $this->xpath('//fieldset');
$info = $this->getInfo();
foreach ($fieldsets as $fieldset) {
if ($fieldset->legend == $info['group']) {
......
......@@ -40,7 +40,7 @@ class SyslogTestCase extends DrupalWebTestCase {
$this->drupalGet('admin/settings/logging/syslog');
if ($this->parse()) {
$field = $this->elements->xpath('//option[@value="' . $edit['syslog_facility'] . '"]'); // Should be one field.
$field = $this->xpath('//option[@value="' . $edit['syslog_facility'] . '"]'); // Should be one field.
$this->assertTrue($field[0]['selected'] == 'selected', t('Facility value saved.'));
}
}
......
......@@ -283,7 +283,7 @@ class AdminOverviewTestCase extends DrupalWebTestCase {
if ($this->parse()) {
$found = 0;
$extra = 0;
$divs = $this->elements->xpath("//div[@class='admin-panel']");
$divs = $this->xpath("//div[@class='admin-panel']");
foreach ($divs as $panel) {
if (in_array(trim($panel->h3), $panels)) {
$found++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment