playwright-advanced-ml-healer
Version:
Advanced AI-powered self-healing selectors for Playwright with 19+ healing types, neural networks, machine learning models, and Global DOM Learning ML Model
363 lines • 11.4 kB
JavaScript
"use strict";
/**
* HealingPage for Advanced ML Self-Healing
*
* A simplified interface for using Advanced ML Healing functionality
* similar to the original HealingPage but with advanced ML capabilities.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdvancedHealingPage = void 0;
const advanced_ml_healing_1 = require("./advanced-ml-healing");
class AdvancedHealingPage {
constructor(page, options = {}) {
this.page = page;
this.advancedML = new advanced_ml_healing_1.AdvancedMLHealing();
this.options = {
timeout: 30000,
retries: 3,
confidence: 0.7,
fallback: true,
analytics: true,
caching: true,
...options
};
}
/**
* Heal and click an element using Advanced ML
*/
async click(selector, options) {
const result = await this.healAndExecute(selector, { action: 'click', options });
if (result) {
await this.page.click(result.selector, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and fill an element using Advanced ML
*/
async fill(selector, value, options) {
const result = await this.healAndExecute(selector, { action: 'fill', value, options });
if (result) {
await this.page.fill(result.selector, value, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and type into an element using Advanced ML
*/
async type(selector, value, options) {
const result = await this.healAndExecute(selector, { action: 'type', value, options });
if (result) {
await this.page.type(result.selector, value, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and select an option using Advanced ML
*/
async selectOption(selector, value, options) {
const result = await this.healAndExecute(selector, { action: 'select', value: Array.isArray(value) ? value[0] : value, options });
if (result) {
await this.page.selectOption(result.selector, value, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and hover over an element using Advanced ML
*/
async hover(selector, options) {
const result = await this.healAndExecute(selector, { action: 'hover', options });
if (result) {
await this.page.hover(result.selector, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and focus an element using Advanced ML
*/
async focus(selector, options) {
const result = await this.healAndExecute(selector, { action: 'focus', options });
if (result) {
await this.page.focus(result.selector, options);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Heal and scroll to an element using Advanced ML
*/
async scrollTo(selector, options) {
const result = await this.healAndExecute(selector, { action: 'scroll', options });
if (result) {
await this.page.evaluate((sel) => {
const element = document.querySelector(sel);
if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, result.selector);
}
else {
throw new Error(`Failed to heal selector: ${selector}`);
}
}
/**
* Get healed selector without executing action
*/
async getHealedSelector(selector) {
const result = await this.advancedML.healWithAdvancedML(this.page, selector, {
action: 'get'
});
return result?.selector || null;
}
/**
* Check if element exists using Advanced ML healing
*/
async isVisible(selector) {
try {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector)
return false;
return await this.page.isVisible(healedSelector);
}
catch {
return false;
}
}
/**
* Wait for element using Advanced ML healing
*/
async waitForSelector(selector, options) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
await this.page.waitForSelector(healedSelector, options || {});
}
/**
* Get element text using Advanced ML healing
*/
async getText(selector) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
return await this.page.textContent(healedSelector) || '';
}
/**
* Get element attribute using Advanced ML healing
*/
async getAttribute(selector, attribute) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
return await this.page.getAttribute(healedSelector, attribute);
}
/**
* Get multiple elements using Advanced ML healing
*/
async getElements(selector) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
return await this.page.$$(healedSelector);
}
/**
* Get element count using Advanced ML healing
*/
async getElementCount(selector) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
return await this.page.$$eval(healedSelector, (elements) => elements.length);
}
/**
* Execute custom action with healed selector
*/
async executeAction(selector, action) {
const healedSelector = await this.getHealedSelector(selector);
if (!healedSelector) {
throw new Error(`Failed to heal selector: ${selector}`);
}
return await action(healedSelector);
}
/**
* Get healing statistics
*/
getHealingStats() {
return this.advancedML.getHealingStats();
}
/**
* Get advanced analytics
*/
getAdvancedAnalytics() {
return this.advancedML.getAdvancedAnalytics();
}
/**
* Generate analytics report
*/
generateAnalyticsReport() {
return this.advancedML.generateAnalyticsReport();
}
/**
* Get caching statistics
*/
getCachingStats() {
return this.advancedML.getCachingStats();
}
/**
* Clear cache
*/
clearCache() {
this.advancedML.clearCache();
}
/**
* Get adaptive learning statistics
*/
getAdaptiveLearningStats() {
return this.advancedML.getAdaptiveLearningStats();
}
/**
* Get comprehensive statistics
*/
getComprehensiveStats() {
return this.advancedML.getComprehensiveStats();
}
/**
* Generate comprehensive report
*/
generateComprehensiveReport() {
return this.advancedML.generateComprehensiveReport();
}
/**
* Optimize performance
*/
optimizePerformance() {
this.advancedML.optimizePerformance();
}
/**
* Debug healing process
*/
debugHealingProcess(selector, context) {
return this.advancedML.debugHealingProcess(selector, context);
}
/**
* Export learning data
*/
exportLearningData() {
return this.advancedML.exportLearningData();
}
/**
* Import learning data
*/
importLearningData(data) {
this.advancedML.importLearningData(data);
}
/**
* Train all models
*/
trainAllModels(trainingData) {
this.advancedML.trainAllModels(trainingData);
}
/**
* Predict with ensemble
*/
predictWithEnsemble(features) {
return this.advancedML.predictWithEnsemble(features);
}
/**
* Analyze element comprehensively
*/
analyzeElementComprehensive(element, selector) {
return this.advancedML.analyzeElementComprehensive(element, selector);
}
/**
* Update real-time learning
*/
updateRealTimeLearning(features, prediction, actual) {
this.advancedML.updateRealTimeLearning(features, prediction, actual);
}
/**
* Optimize performance comprehensively
*/
optimizePerformanceComprehensive() {
this.advancedML.optimizePerformanceComprehensive();
}
/**
* Core healing and execution method
*/
async healAndExecute(selector, action) {
let lastError = null;
for (let attempt = 1; attempt <= this.options.retries; attempt++) {
try {
const result = await this.advancedML.healWithAdvancedML(this.page, selector, {
action: action.action,
value: action.value,
options: action.options
});
if (result && result.confidence >= this.options.confidence) {
return result;
}
// If confidence is too low, try again
if (attempt < this.options.retries) {
await new Promise(resolve => setTimeout(resolve, 1000 * attempt)); // Exponential backoff
continue;
}
// Last attempt - use fallback if enabled
if (this.options.fallback && result) {
console.warn(`Using fallback selector with low confidence (${(result.confidence * 100).toFixed(1)}%): ${result.selector}`);
return result;
}
return null;
}
catch (error) {
lastError = error;
if (attempt < this.options.retries) {
await new Promise(resolve => setTimeout(resolve, 1000 * attempt)); // Exponential backoff
continue;
}
throw new Error(`Failed to heal selector "${selector}" after ${this.options.retries} attempts. Last error: ${lastError.message}`);
}
}
return null;
}
/**
* Get the underlying AdvancedMLHealing instance
*/
getAdvancedML() {
return this.advancedML;
}
/**
* Get the underlying Playwright Page instance
*/
getPage() {
return this.page;
}
/**
* Get current options
*/
getOptions() {
return { ...this.options };
}
/**
* Update options
*/
updateOptions(newOptions) {
this.options = { ...this.options, ...newOptions };
}
}
exports.AdvancedHealingPage = AdvancedHealingPage;
//# sourceMappingURL=healing-page.js.map