Lần đầu tìm hiểu Selenium framework
const fs = require('fs');
const webdriver = require('selenium-webdriver');
const By = webdriver.By;
async function startTesting() {
let browser = await new webdriver.Builder()
.forBrowser('chrome')
.build();
browser.manage().window().maximize();
// Hiện cursor khi automation di chuyển chuột
// https://gist.github.com/primaryobjects/70087610d9aef0f4bddbe2101dda7649
let cursorJs = fs.readFileSync('js/cursor.js', 'utf8');
await browser.get('https://www.mine.haotam.vn');
await browser.executeScript(cursorJs);
// console.log(await browser.getTitle());
browser.getTitle().then(function (title) {
console.log(title);
});
let moreLinks = await browser.findElements(By.className('more-link'));
if (moreLinks.length) {
let i = 0;
setInterval(async () => {
// Di chuyển cursor tới element đó
browser.actions().mouseMove(moreLinks[i]).perform();
console.log(await moreLinks[i].findElement(By.className('meta-nav')).getText() + ' ' + i);
i += (i < (moreLinks.length - 1)) ? 1 : -i;
}, 500);
}
// setTimeout(() => {
// browser.quit();
// }, 10000);
}
startTesting();
// cursor.js
// Create mouse following image.
var seleniumFollowerImg = document.createElement('img');
// Set image properties.
seleniumFollowerImg.setAttribute('src', 'https://pngimg.com/uploads/cursor/cursor_PNG102.png');
seleniumFollowerImg.setAttribute('id', 'selenium_mouse_follower');
seleniumFollowerImg.setAttribute('style', 'position: absolute; z-index: 2147483647; width: 16px; height: 16px;');
// Add mouse follower to the web page.
document.body.appendChild(seleniumFollowerImg);
// Track mouse movements and re-position the mouse follower.
document.onmousemove = function (e) {
const mousePointer = document.getElementById('selenium_mouse_follower');
mousePointer.style.left = e.pageX + 'px';
mousePointer.style.top = e.pageY + 'px';
}