From ce4abd1a35883f86385e8fe17757cb86a79bb2e7 Mon Sep 17 00:00:00 2001 From: liyang Date: Mon, 15 Apr 2024 17:19:21 +0800 Subject: [PATCH] * Adjust get, add and delete cookie methods in page. --- test/lib/result.class.php | 5 ++- test/lib/ui.php | 61 +++++++++++++++++++++----- test/lib/webdriver/webdriver.class.php | 27 +++++++++--- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/test/lib/result.class.php b/test/lib/result.class.php index 7725b04db0..612167f4d1 100644 --- a/test/lib/result.class.php +++ b/test/lib/result.class.php @@ -15,9 +15,10 @@ class result * @access public * @return object */ - function success() + function success($message = '') { - $this->status = 'SUCCESS'; + $this->status = 'SUCCESS'; + $this->message = $message; return $this->response(); } diff --git a/test/lib/ui.php b/test/lib/ui.php index a2c482f3a6..66f8f7cf31 100644 --- a/test/lib/ui.php +++ b/test/lib/ui.php @@ -129,7 +129,7 @@ class tester extends result public $page; public $config; public $cookieFile; - public $langFile; + public $lang; /** * Initialize the basic configuration of the ui test framework. @@ -161,16 +161,17 @@ class tester extends result if(!$password) $password = $this->config->uitest->defaultPassword; $webRoot = $this->getWebRoot(); - $this->page->openURL($webRoot)->deleteCookie(); + $this->page->openURL($webRoot)->deleteCookie()->refresh(); if($this->config->uitest->langClient == 'en') { + $this->page->deleteCookie('lang'); $cookie = array(); $cookie['lang'] = 'en'; $this->addCookie($webRoot, $cookie); + $this->page->refresh()->wait(1); } - $this->page->openURL($webRoot); $this->checkError(); $this->page->dom->account->setValue($account); @@ -180,8 +181,7 @@ class tester extends result $this->page->saveCookie($this->cookieFile); sleep(1); - $this->langFile = '/tmp/lang_' . str_replace('.', '_', parse_url($webRoot, PHP_URL_HOST)); - if(!file_exists($this->langFile)) $this->initLang(); + if(empty($this->lang)) $this->initLang(); return $this->page; } @@ -248,7 +248,6 @@ class tester extends result $this->module = $module; $this->method = $method; $webRoot = $this->getWebRoot(); - $cookies = json_decode(file_get_contents($this->cookieFile), true); if($this->config->requestType == 'GET') { @@ -262,8 +261,12 @@ class tester extends result $url .= ".html"; } - $this->page->openURL($webRoot)->deleteCookie(); - foreach($cookies as $cookie) if($cookie["name"] == "zentaosid") $this->page->addCookie($cookie); + if(!$this->page->getCookie('zentaosid')) + { + $zentaosid = $this->getCookieValueFromFile('zentaosid'); + $this->addCookie($webRoot, array('zentaosid' => $zentaosid)); + $this->page->refresh()->wait(1); + } $this->page->openURL($webRoot . $url); $appIframeID = $iframeID ? $iframeID : "appIframe-{$module}"; @@ -368,17 +371,53 @@ class tester extends result $zentaosid = $this->getCookieValueFromFile('zentaosid'); if($zentaosid) { - $url = $webRoot . "api.php/v1/langs?modules=all&lang={$this->config->uitest->langClient}&zentaosid={$zentaosid}"; - $response = common::http($url); + $url = $webRoot . "api.php/v1/langs?modules=all&lang={$this->config->uitest->langClient}&zentaosid={$zentaosid}"; + $header = array('Cookie: device=desktop; lang=' . $this->config->uitest->langClient . '; theme=default; zentaosid=' . $zentaosid); + + $response = common::http($url, null, array(), $header); if(empty($response)) return false; - file_put_contents(json_encode($response), $this->langFile); + $this->lang = json_decode($response); return true; } return false; } + /** + * Check fields tip in form page. + * + * @param string $module + * @access public + * @return void + */ + public function checkFormTips($module) + { + $tips = $this->page->dom->getFormTips(); + if(empty($tips)) return false; + + foreach($tips as $field => $tip) + { + if(strpos($tip, '|') !== false) list($tip, $fieldValue) = explode('|', $tip); + $field = preg_replace('/Tip$/', '', $field); + if(!isset($this->lang->{$module}->{$field})) return false; + + $field = $this->lang->{$module}->{$field}; + + $checkList = array(); + $checkList[] = sprintf($this->lang->error->notempty, $field); + if(isset($fieldValue)) + { + $checkList[] = sprintf($this->lang->error->unique, $field, $fieldValue); + $checkList[] = sprintf($this->lang->error->repeat, $field, $fieldValue); + } + + if(!in_array($tip, $checkList)) return false; + } + + return true; + } + /** * Close the Browser. * diff --git a/test/lib/webdriver/webdriver.class.php b/test/lib/webdriver/webdriver.class.php index 27338fc17d..5ef8bc6229 100644 --- a/test/lib/webdriver/webdriver.class.php +++ b/test/lib/webdriver/webdriver.class.php @@ -183,10 +183,16 @@ class webdriver { if(!$cookieFile) return; - $cookie = $this->getCookieList(); - $isSave = file_put_contents($cookieFile, json_encode($cookie)); + $cookies = $this->getCookie(); + if(empty($cookie)) $cookies = $this->wait(1)->getCookie(); - return $isSave ? $cookie : false; + $cookiesArray = array_map(function($cookie) { + return $cookie->toArray(); + }, $cookies); + + $isSave = file_put_contents($cookieFile, json_encode($cookiesArray)); + + return $isSave; } /** @@ -208,9 +214,18 @@ class webdriver * @access public * @return void */ - public function deleteCookie() + public function deleteCookie($name = '') { - return $this->driver->manage()->deleteAllCookies(); + if($name) + { + $this->driver->manage()->deleteCookieNamed($name); + } + else + { + $this->driver->manage()->deleteAllCookies(); + } + + return $this; } /** @@ -434,7 +449,9 @@ class dom foreach($this->element as $element) { $id = $element->getAttribute('id'); + $value = $element->findElement(WebDriverBy::xpath('../input'))->getAttribute('value'); $tips[$id] = $element->getText(); + if($value) $tips[$id] .= '|' . $value; } return $tips;