feat: enhance signup entry click logic with retry mechanism and improved logging
This commit is contained in:
+23
-3
@@ -962,6 +962,8 @@ async function waitForSignupEntryState(options = {}) {
|
|||||||
logDiagnostics = false,
|
logDiagnostics = false,
|
||||||
} = options;
|
} = options;
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
const maxSignupEntryClickRetries = 5;
|
||||||
|
const maxSignupEntryClickAttempts = maxSignupEntryClickRetries + 1;
|
||||||
let lastTriggerClickAt = 0;
|
let lastTriggerClickAt = 0;
|
||||||
let clickAttempts = 0;
|
let clickAttempts = 0;
|
||||||
let lastState = '';
|
let lastState = '';
|
||||||
@@ -1016,12 +1018,19 @@ async function waitForSignupEntryState(options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Date.now() - lastTriggerClickAt >= 1500) {
|
if (Date.now() - lastTriggerClickAt >= 1500) {
|
||||||
|
if (clickAttempts >= maxSignupEntryClickAttempts) {
|
||||||
|
log(`步骤 ${step}:官网注册入口已完成 ${maxSignupEntryClickRetries} 次重试,页面仍未进入邮箱输入页,停止重试。`, 'warn');
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
lastTriggerClickAt = Date.now();
|
lastTriggerClickAt = Date.now();
|
||||||
clickAttempts += 1;
|
clickAttempts += 1;
|
||||||
|
const retryAttempt = clickAttempts - 1;
|
||||||
if (logDiagnostics) {
|
if (logDiagnostics) {
|
||||||
log(`步骤 ${step}:正在点击官网注册入口(第 ${clickAttempts} 次):"${getActionText(snapshot.signupTrigger).slice(0, 80)}"`);
|
log(`步骤 ${step}:正在点击官网注册入口(第 ${clickAttempts}/${maxSignupEntryClickAttempts} 次):"${getActionText(snapshot.signupTrigger).slice(0, 80)}"`);
|
||||||
}
|
}
|
||||||
log('步骤 2:已找到官网注册入口,等待 3 秒后点击...');
|
log(retryAttempt > 0
|
||||||
|
? `步骤 ${step}:上次点击后仍未进入邮箱输入页,等待 3 秒后重试点击官网注册入口(重试 ${retryAttempt}/${maxSignupEntryClickRetries})...`
|
||||||
|
: `步骤 ${step}:已找到官网注册入口,等待 3 秒后点击...`);
|
||||||
await sleep(3000);
|
await sleep(3000);
|
||||||
throwIfStopped();
|
throwIfStopped();
|
||||||
simulateClick(snapshot.signupTrigger);
|
simulateClick(snapshot.signupTrigger);
|
||||||
@@ -2204,7 +2213,10 @@ async function waitForSignupPhoneEntryState(options = {}) {
|
|||||||
step = 2,
|
step = 2,
|
||||||
} = options;
|
} = options;
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
const maxSignupEntryClickRetries = 5;
|
||||||
|
const maxSignupEntryClickAttempts = maxSignupEntryClickRetries + 1;
|
||||||
let lastTriggerClickAt = 0;
|
let lastTriggerClickAt = 0;
|
||||||
|
let clickAttempts = 0;
|
||||||
let lastSwitchToPhoneAt = 0;
|
let lastSwitchToPhoneAt = 0;
|
||||||
let lastMoreOptionsClickAt = 0;
|
let lastMoreOptionsClickAt = 0;
|
||||||
let slowSnapshotLogged = false;
|
let slowSnapshotLogged = false;
|
||||||
@@ -2246,8 +2258,16 @@ async function waitForSignupPhoneEntryState(options = {}) {
|
|||||||
|
|
||||||
if (snapshot.state === 'entry_home' && snapshot.signupTrigger) {
|
if (snapshot.state === 'entry_home' && snapshot.signupTrigger) {
|
||||||
if (Date.now() - lastTriggerClickAt >= 1500) {
|
if (Date.now() - lastTriggerClickAt >= 1500) {
|
||||||
|
if (clickAttempts >= maxSignupEntryClickAttempts) {
|
||||||
|
log(`步骤 ${step}:官网注册入口已完成 ${maxSignupEntryClickRetries} 次重试,页面仍未进入手机号输入页,停止重试。`, 'warn');
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
lastTriggerClickAt = Date.now();
|
lastTriggerClickAt = Date.now();
|
||||||
log(`步骤 ${step}:已找到官网注册入口,等待 3 秒后点击...`);
|
clickAttempts += 1;
|
||||||
|
const retryAttempt = clickAttempts - 1;
|
||||||
|
log(retryAttempt > 0
|
||||||
|
? `步骤 ${step}:上次点击后仍未进入手机号输入页,等待 3 秒后重试点击官网注册入口(重试 ${retryAttempt}/${maxSignupEntryClickRetries})...`
|
||||||
|
: `步骤 ${step}:已找到官网注册入口,等待 3 秒后点击...`);
|
||||||
await sleep(3000);
|
await sleep(3000);
|
||||||
throwIfStopped();
|
throwIfStopped();
|
||||||
simulateClick(snapshot.signupTrigger);
|
simulateClick(snapshot.signupTrigger);
|
||||||
|
|||||||
@@ -389,11 +389,6 @@
|
|||||||
const totalUses = data.total_uses ?? data.totalUses;
|
const totalUses = data.total_uses ?? data.totalUses;
|
||||||
const usedUses = data.used_uses ?? data.usedUses;
|
const usedUses = data.used_uses ?? data.usedUses;
|
||||||
const status = String(data.status || data.card_status || data.cardStatus || '').trim();
|
const status = String(data.status || data.card_status || data.cardStatus || '').trim();
|
||||||
const hasAutoModeField = data.auto_mode_enabled !== undefined
|
|
||||||
|| data.autoModeEnabled !== undefined
|
|
||||||
|| data.auto_enabled !== undefined
|
|
||||||
|| data.autoEnabled !== undefined;
|
|
||||||
const autoModeEnabled = isGpcAutoModeEnabled(data);
|
|
||||||
const flowId = String(data.flow_id || data.flowId || '').trim();
|
const flowId = String(data.flow_id || data.flowId || '').trim();
|
||||||
const parts = [];
|
const parts = [];
|
||||||
if (firstValue !== undefined) {
|
if (firstValue !== undefined) {
|
||||||
@@ -407,9 +402,6 @@
|
|||||||
if (status) {
|
if (status) {
|
||||||
parts.push(`状态 ${status}`);
|
parts.push(`状态 ${status}`);
|
||||||
}
|
}
|
||||||
if (hasAutoModeField) {
|
|
||||||
parts.push(`自动模式 ${autoModeEnabled ? '已开通' : '未开通'}`);
|
|
||||||
}
|
|
||||||
if (flowId) {
|
if (flowId) {
|
||||||
parts.push(`flow_id ${flowId}`);
|
parts.push(`flow_id ${flowId}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ test('GoPay utils formats balance and maps linked-account errors', () => {
|
|||||||
message: 'ok',
|
message: 'ok',
|
||||||
data: { remaining_uses: 0, total_uses: 3, used_uses: 3, status: 'active', auto_mode_enabled: false },
|
data: { remaining_uses: 0, total_uses: 3, used_uses: 3, status: 'active', auto_mode_enabled: false },
|
||||||
}),
|
}),
|
||||||
'余额 0/3,已用 3,状态 active,自动模式 未开通'
|
'余额 0/3,已用 3,状态 active'
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
api.formatGpcBalancePayload({
|
api.formatGpcBalancePayload({
|
||||||
@@ -95,7 +95,7 @@ test('GoPay utils formats balance and maps linked-account errors', () => {
|
|||||||
message: 'ok',
|
message: 'ok',
|
||||||
data: { remaining_uses: 998, total_uses: 1000, used_uses: 2, status: 'active', auto_mode_enabled: true },
|
data: { remaining_uses: 998, total_uses: 1000, used_uses: 2, status: 'active', auto_mode_enabled: true },
|
||||||
}),
|
}),
|
||||||
'余额 998/1000,已用 2,状态 active,自动模式 已开通'
|
'余额 998/1000,已用 2,状态 active'
|
||||||
);
|
);
|
||||||
assert.equal(api.getGpcBalanceRemainingUses({ data: { remaining_uses: 998 } }), 998);
|
assert.equal(api.getGpcBalanceRemainingUses({ data: { remaining_uses: 998 } }), 998);
|
||||||
assert.equal(api.isGpcAutoModeEnabled({ data: { auto_mode_enabled: true } }), true);
|
assert.equal(api.isGpcAutoModeEnabled({ data: { auto_mode_enabled: true } }), true);
|
||||||
|
|||||||
@@ -440,6 +440,125 @@ return {
|
|||||||
assert.equal(api.run()?.kind, 'localized-email');
|
assert.equal(api.run()?.kind, 'localized-email');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('waitForSignupEntryState retries the signup entry click five times before giving up', async () => {
|
||||||
|
const api = new Function(`
|
||||||
|
const logs = [];
|
||||||
|
const clicks = [];
|
||||||
|
let now = 0;
|
||||||
|
|
||||||
|
const signupButton = {
|
||||||
|
textContent: '免费注册',
|
||||||
|
value: '',
|
||||||
|
disabled: false,
|
||||||
|
getAttribute(name) {
|
||||||
|
if (name === 'type') return 'button';
|
||||||
|
return '';
|
||||||
|
},
|
||||||
|
getBoundingClientRect() {
|
||||||
|
return { width: 120, height: 36 };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const document = {
|
||||||
|
querySelector() {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
querySelectorAll(selector) {
|
||||||
|
if (selector === 'a, button, [role="button"], [role="link"]') {
|
||||||
|
return [signupButton];
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const location = {
|
||||||
|
href: 'https://chatgpt.com/',
|
||||||
|
};
|
||||||
|
|
||||||
|
const Date = {
|
||||||
|
now() {
|
||||||
|
return now;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
${extractConst('SIGNUP_ENTRY_TRIGGER_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_EMAIL_INPUT_SELECTOR')}
|
||||||
|
${extractConst('SIGNUP_PHONE_INPUT_SELECTOR')}
|
||||||
|
|
||||||
|
function isVisibleElement(el) {
|
||||||
|
return Boolean(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isActionEnabled(el) {
|
||||||
|
return Boolean(el) && !el.disabled && el.getAttribute('aria-disabled') !== 'true';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActionText(el) {
|
||||||
|
return [el?.textContent, el?.value, el?.getAttribute?.('aria-label'), el?.getAttribute?.('title')]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')
|
||||||
|
.replace(/\\s+/g, ' ')
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordInput() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSignupPasswordPage() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordSubmitButton() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordDisplayedEmail() {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function throwIfStopped() {}
|
||||||
|
|
||||||
|
function log(message, level = 'info') {
|
||||||
|
logs.push({ message, level });
|
||||||
|
}
|
||||||
|
|
||||||
|
function simulateClick(target) {
|
||||||
|
clicks.push(getActionText(target));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sleep(ms) {
|
||||||
|
now += ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
${extractFunction('getSignupEmailInput')}
|
||||||
|
${extractFunction('getSignupPhoneInput')}
|
||||||
|
${extractFunction('getSignupEmailContinueButton')}
|
||||||
|
${extractFunction('findSignupEntryTrigger')}
|
||||||
|
${extractFunction('inspectSignupEntryState')}
|
||||||
|
${extractFunction('waitForSignupEntryState')}
|
||||||
|
|
||||||
|
return {
|
||||||
|
async run() {
|
||||||
|
return waitForSignupEntryState({ timeout: 30000, autoOpenEntry: true, step: 2 });
|
||||||
|
},
|
||||||
|
getClicks() {
|
||||||
|
return clicks.slice();
|
||||||
|
},
|
||||||
|
getLogs() {
|
||||||
|
return logs.slice();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
`)();
|
||||||
|
|
||||||
|
const snapshot = await api.run();
|
||||||
|
|
||||||
|
assert.equal(snapshot.state, 'entry_home');
|
||||||
|
assert.equal(api.getClicks().length, 6);
|
||||||
|
assert.equal(api.getLogs().some(({ message }) => message.includes('重试 5/5')), true);
|
||||||
|
assert.equal(api.getLogs().some(({ message, level }) => level === 'warn' && /已完成 5 次重试/.test(message)), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('ensureSignupPhoneEntryReady opens free signup before switching to the phone entry', async () => {
|
test('ensureSignupPhoneEntryReady opens free signup before switching to the phone entry', async () => {
|
||||||
const api = new Function(`
|
const api = new Function(`
|
||||||
const logs = [];
|
const logs = [];
|
||||||
@@ -625,6 +744,137 @@ return {
|
|||||||
assert.deepEqual(api.getClicks(), ['免费注册', 'Continue with phone number']);
|
assert.deepEqual(api.getClicks(), ['免费注册', 'Continue with phone number']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('waitForSignupPhoneEntryState retries the signup entry click five times before giving up', async () => {
|
||||||
|
const api = new Function(`
|
||||||
|
const logs = [];
|
||||||
|
const clicks = [];
|
||||||
|
let now = 0;
|
||||||
|
|
||||||
|
const signupButton = {
|
||||||
|
textContent: '免费注册',
|
||||||
|
value: '',
|
||||||
|
disabled: false,
|
||||||
|
getAttribute(name) {
|
||||||
|
if (name === 'type') return 'button';
|
||||||
|
return '';
|
||||||
|
},
|
||||||
|
getBoundingClientRect() {
|
||||||
|
return { width: 120, height: 36 };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const document = {
|
||||||
|
querySelector() {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
querySelectorAll(selector) {
|
||||||
|
if (selector === 'a, button, [role="button"], [role="link"]') {
|
||||||
|
return [signupButton];
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const location = {
|
||||||
|
href: 'https://chatgpt.com/',
|
||||||
|
};
|
||||||
|
|
||||||
|
const Date = {
|
||||||
|
now() {
|
||||||
|
return now;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
${extractConst('SIGNUP_ENTRY_TRIGGER_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_EMAIL_INPUT_SELECTOR')}
|
||||||
|
${extractConst('SIGNUP_PHONE_INPUT_SELECTOR')}
|
||||||
|
${extractConst('SIGNUP_SWITCH_TO_EMAIL_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_SWITCH_ACTION_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_EMAIL_ACTION_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_WORK_EMAIL_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_PHONE_ACTION_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_SWITCH_TO_PHONE_PATTERN')}
|
||||||
|
${extractConst('SIGNUP_MORE_OPTIONS_PATTERN')}
|
||||||
|
|
||||||
|
function isVisibleElement(el) {
|
||||||
|
return Boolean(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isActionEnabled(el) {
|
||||||
|
return Boolean(el) && !el.disabled && el.getAttribute('aria-disabled') !== 'true';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActionText(el) {
|
||||||
|
return [el?.textContent, el?.value, el?.getAttribute?.('aria-label'), el?.getAttribute?.('title')]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ')
|
||||||
|
.replace(/\\s+/g, ' ')
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordInput() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSignupPasswordPage() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordSubmitButton() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSignupPasswordDisplayedEmail() {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function throwIfStopped() {}
|
||||||
|
|
||||||
|
function log(message, level = 'info') {
|
||||||
|
logs.push({ message, level });
|
||||||
|
}
|
||||||
|
|
||||||
|
function simulateClick(target) {
|
||||||
|
clicks.push(getActionText(target));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function humanPause() {}
|
||||||
|
|
||||||
|
async function sleep(ms) {
|
||||||
|
now += ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
${extractFunction('getSignupEmailInput')}
|
||||||
|
${extractFunction('getSignupPhoneInput')}
|
||||||
|
${extractFunction('findSignupUseEmailTrigger')}
|
||||||
|
${extractFunction('findSignupUsePhoneTrigger')}
|
||||||
|
${extractFunction('findSignupMoreOptionsTrigger')}
|
||||||
|
${extractFunction('getSignupEmailContinueButton')}
|
||||||
|
${extractFunction('findSignupEntryTrigger')}
|
||||||
|
${extractFunction('inspectSignupEntryState')}
|
||||||
|
${extractFunction('waitForSignupPhoneEntryState')}
|
||||||
|
|
||||||
|
return {
|
||||||
|
async run() {
|
||||||
|
return waitForSignupPhoneEntryState({ timeout: 30000, step: 2 });
|
||||||
|
},
|
||||||
|
getClicks() {
|
||||||
|
return clicks.slice();
|
||||||
|
},
|
||||||
|
getLogs() {
|
||||||
|
return logs.slice();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
`)();
|
||||||
|
|
||||||
|
const snapshot = await api.run();
|
||||||
|
|
||||||
|
assert.equal(snapshot.state, 'entry_home');
|
||||||
|
assert.equal(api.getClicks().length, 6);
|
||||||
|
assert.equal(api.getLogs().some(({ message }) => message.includes('重试 5/5')), true);
|
||||||
|
assert.equal(api.getLogs().some(({ message, level }) => level === 'warn' && /已完成 5 次重试/.test(message)), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('submitSignupPhoneNumberAndContinue auto-switches signup country before filling the local phone number', async () => {
|
test('submitSignupPhoneNumberAndContinue auto-switches signup country before filling the local phone number', async () => {
|
||||||
const api = new Function(`
|
const api = new Function(`
|
||||||
const logs = [];
|
const logs = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user