+ [task#152439,doing,2h] add ui test for user module login

This commit is contained in:
wanshan
2025-11-24 10:55:47 +08:00
parent 6e98a62fb2
commit 2d98cbb540
3 changed files with 110 additions and 0 deletions
@@ -0,0 +1,53 @@
<?php
require_once dirname(__FILE__, 6) . '/test/lib/ui.php';
class loginTester extends tester
{
/**
* 验证使用正确的用户名/密码登录
* Verify login with correct account and password
*/
public function verifyLoginCorrectCredentials()
{
$this->login();
$form = $this->initForm('user', 'login', [], 'appIframe-my');
$form->wait(1);
$userInfo = $form->dom->avatar->getText();
if(empty($userInfo)) return $this->failed('登录后用户信息为空');
return $this->success('使用正确账号密码登录测试通过');
}
/**
* 验证使用错误的登录信息登录系统
* Verify login with incorrect account or password
*/
public function verifyLoginIncorrectCredentials($account = 'admin', $password = 'wrongpass')
{
$webRoot = $this->getWebRoot();
// 确保干净会话,避免前序用例的登录态影响。
$this->page->openURL($webRoot)->deleteCookie()->refresh();
$form = $this->loadPage('user', 'login');
$form->wait(1);
if($form->dom->account) $form->dom->account->setValue($account);
if($form->dom->password) $form->dom->password->setValue($password);
if($form->dom->submit) $form->dom->submit->click();
$form->wait(1);
$alertText = $form->dom->alertModal('text');
if(empty($this->lang)) $this->initLang();
if($alertText != $this->lang->user->loginFailed) return $this->failed('登录失败提示与预期不符', $alertText);
// 关闭提示框,避免遮挡后续元素。
$form->dom->alertModal();
// 再次检查当前仍停留在登录页(未发生成功跳转)。
$html = $form->getPageSource();
$stillLogin = (strpos($html, 'id=\"loginForm\"') !== false) || (strpos($html, 'm=user') !== false && strpos($html, 'f=login') !== false);
if(!$stillLogin) return $this->failed('错误凭据却跳转到了其他页面');
if($account == 'admin') return $this->success('使用错误密码登录测试通过');
return $this->success('不存在的用户登录测试通过');
}
}
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 2) . '/lib/ui/login.ui.class.php';
/**
title=开源版m=user&f=login测试
timeout=0
cid=1
- 使用正确账号密码登录测试
- 最终测试状态 @SUCCESS
- 测试结果 @使用正确账号密码登录测试通过
- 使用错误密码登录测试
- 最终测试状态 @SUCCESS
- 测试结果 @使用错误密码登录测试通过
- 不存在的用户登录测试
- 最终测试状态 @SUCCESS
- 测试结果 @不存在的用户登录测试通过
*/
ob_start();
$user = zenData('user');
$user->id->range('1');
$user->account->range('admin');
$user->realname->range('管理员');
$user->password->range($config->uitest->defaultPassword)->format('md5');
$user->role->range('admin');
$user->gen(1, false);
$__out = ob_get_clean();
if($__out && strpos($__out, 'Duplicate entry') === false) echo $__out;
$tester = new loginTester();
r($tester->verifyLoginCorrectCredentials()) && p('status,message') && e('SUCCESS,使用正确账号密码登录测试通过'); // 使用正确账号密码登录测试
r($tester->verifyLoginIncorrectCredentials()) && p('status,message') && e('SUCCESS,使用错误密码登录测试通过'); // 使用错误密码登录测试
r($tester->verifyLoginIncorrectCredentials('notexist')) && p('status,message') && e('SUCCESS,不存在的用户登录测试通过'); // 不存在的用户登录测试
$tester->closeBrowser();
+17
View File
@@ -0,0 +1,17 @@
<?php
class loginPage extends page
{
public function __construct($webdriver)
{
parent::__construct($webdriver);
$this->xpath = array(
'avatar' => '//*[@id="userMenu-toggle"]/div/div',
'account' => "//*[@id='account']",
'password' => "//*[@id='password']",
'submit' => "//*[@id='submit']",
);
$this->dom->xpath = array_merge($this->dom->xpath, $this->xpath);
}
}