This code automates interactions on a "YouEarnedIt" website, including logging in and simulating "High-Five" button clicks. It uses functions to handle each step of the process, making it modular and reusable.
npm run import -- "high five people in YouEarnedIt"
var importer = require('../Core');
function loginYouEarnedIt() {
var credentials = getCredentials('swiftpage.youearnedit.com');
return client
.click('input[name="user[login]"]')
.keys(credentials.username)
.pause(1000)
.click('input[name="user[password]"]')
.keys(credentials.password)
.pause(1000)
.submitForm('form[action]')
.pause(2000)
}
function doHighFive(el) {
return client.elementIdClick(el)
.pause(1000)
.isExisting('//div[contains(@class, "modal")]//button[contains(., "Continue")]')
.then(is => is
? client.click('//div[contains(@class, "modal")]//button[contains(., "Continue")]')
: Promise.resolve([]))
// TODO: check for multiple dialog?
}
function highFive() {
return client.url('https://swiftpage.youearnedit.com/posts')
.pause(2000)
.isExisting('form[action*="sign_in"]')
.then(is => is ? loginYouEarnedIt() : Promise.resolve([]))
.elements('//button[not(@disabled)]/i[contains(@class, "High-Five")]')
.then(els => {
console.log(els);
return importer.runAllPromises(els.value
.map(el => resolve => doHighFive(el.ELEMENT)
.then(() => resolve())
.catch(e => {
console.log(e);
resolve();
})));
})
.catch(e => console.log(e))
// TODO: repeat or just first page?
}
module.exports = highFive;
const { Client } = require('playwright');
const Core = require('../Core');
const { getCredentials } = require('./credentials'); // assume credentials function is in this file
class YouEarnedItClient {
constructor(browser, url) {
this.browser = browser;
this.client = browser.newPage();
this.client.goto(url);
}
async navigateLogin() {
await this.client.fill('input[name="user[login]"]', await getCredentials('swiftpage.youearnedit.com').username);
await this.client.fill('input[name="user[password]"]', await getCredentials('swiftpage.youearnedit.com').password);
await this.client.click('form[action]');
await this.client.waitForNavigation();
}
async clickHighFive(element) {
try {
await this.client.click(element);
await this.client.waitForSelector('//div[contains(@class, "modal")]//button[contains(., "Continue")]');
await this.client.click('//div[contains(@class, "modal")]//button[contains(., "Continue")]');
} catch (e) {
console.log(e);
}
}
async highFive() {
try {
await this.client.goto('https://swiftpage.youearnedit.com/posts');
await this.client.waitForSelector('form[action*="sign_in"]');
if (await this.client.isVisible('form[action*="sign_in"]')) {
await this.navigateLogin();
}
const highFiveButtons = await this.client.$('//button[not(@disabled)]/i[contains(@class, "High-Five")]');
const promises = highFiveButtons.map(button => this.clickHighFive(button));
await Core.runAllPromises(promises);
} catch (e) {
console.log(e);
}
}
}
module.exports = async (browser) => {
const youEarnedItClient = new YouEarnedItClient(browser, 'https://swiftpage.youearnedit.com/posts');
await youEarnedItClient.highFive();
};
This code defines a series of functions to automate interactions with a website, likely related to a "YouEarnedIt" platform.
Here's a breakdown:
loginYouEarnedIt()
:
getCredentials
.doHighFive(el)
:
el
.highFive()
:
doHighFive
for each, simulating a high-five action.Module Export:
highFive
function is exported as the main module export, making it callable from other parts of the application.In essence, this code automates a workflow on the "YouEarnedIt" website, handling login and interacting with "High-Five" buttons.