Khối mã trong JavaScript là gì

Một khối trong JavaScript được sử dụng để nhóm 0 hoặc nhiều câu lệnh bằng dấu ngoặc nhọn ({}). Hãy nhớ rằng một câu lệnh chỉ đơn giản là một đơn vị mã thực hiện một việc gì đó hoặc tạo ra một số hành vi

Các khối thường được sử dụng nhất với các câu lệnh while, if...elsefor. Tất cả chúng ta đều đã thấy điều này, tuy nhiên, có một hàm ý thú vị khi sử dụng các khối có letconst

Ở chế độ không nghiêm ngặt, các hàm và biến

let foo = 'yo';
{
  let foo = 'hey';
}

console.log(foo); // Logs 'yo'
0 không có phạm vi khối

var foo = 'yo';
{
  var foo = 'hey';
}

console.log(foo); // Logs 'hey'

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Nhưng khi sử dụng let hoặc const, các khối sẽ giữ phạm vi của từng biến

let foo = 'yo';
{
  let foo = 'hey';
}

console.log(foo); // Logs 'yo'

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

const foo = 'yo';
{
  const foo = 'hey';
}

console.log(foo); // Logs 'yo'

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Lưu ý rằng không có SyntaxError nào được đưa ra trong ví dụ const cho một khai báo trùng lặp. Điều này là do biến

let foo = 'yo';
{
  let foo = 'hey';
}

console.log(foo); // Logs 'yo'
4 bên trong khối được chứa trong phạm vi của chính nó và do đó không xung đột với phạm vi bên ngoài

Chúng tôi sử dụng các khối trong JavaScript rất nhiều nên đôi khi rất dễ quên khái niệm về chúng. Chúng thường có vẻ gắn liền với mã liền kề của chúng, chẳng hạn như câu lệnh hoặc hàm

let foo = 'yo';
{
  let foo = 'hey';
}

console.log(foo); // Logs 'yo'
5. Nhưng như chúng ta đã thấy trong ví dụ trên, bạn có thể tạo mã hoàn toàn hợp lệ với các khối bị cô lập của riêng mình nếu muốn

{
  console.log('I run inside my very own block!');
}

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Mặc dù điều này có vẻ lạ (gần giống như thể bạn đang tạo một đối tượng khi đang di chuyển), nhưng nó hợp lệ 100%. Khối là niềm vui. 🧱

Cho đến nay, chúng tôi chủ yếu làm việc với các bước thực hiện một hành động tại một thời điểm, chẳng hạn như các bước HTTP trong Tập lệnh giao thức hoặc các bước điều hướng, nhấp và nhập trong Tập lệnh trình duyệt. Những bước đơn lẻ đó có thể thực hiện được rất nhiều nếu tập lệnh của bạn tiến hành theo kiểu tuyến tính (như hầu hết các tập lệnh thử nghiệm nên làm), nhưng nếu bạn cần luồng điều khiển đặc biệt hoặc logic có điều kiện hoặc vòng lặp thì sao?

Câu trả lời của Loadster là Code Blocks

Khối mã JavaScript

Các khối mã có thể tồn tại ở bất kỳ đâu trong tập lệnh của bạn. Trên thực tế, nếu muốn, bạn có thể tạo một tập lệnh chỉ gồm một khối mã và thực hiện mọi thứ bằng JavaScript

Khối mã trong JavaScript là gì
Một khối mã

Các khối mã linh hoạt hơn so với kịch bản từng bước thông thường vì bạn có luồng điều khiển của một ngôn ngữ lập trình thực tế. vòng lặp, điều kiện, hàm, v.v.

Để thêm khối mã vào tập lệnh của bạn, hãy chọn Thêm khối mã từ thanh trên cùng

Biến khối mã và Phạm vi chức năng

Các khối mã được đặt trong phạm vi riêng cho bot đang thực thi chúng. Điều đó có nghĩa là nếu bạn khai báo một biến hoặc hàm trong một tập lệnh, nó sẽ chỉ tồn tại cho bot đó đang chạy tập lệnh và không tồn tại cho bất kỳ bot nào khác cũng có thể đang chạy tập lệnh đó

Cũng giống như người dùng thực sự, mỗi bot tương tác với trang web của bạn một cách độc lập với tất cả những người khác

Ngoài ra, vì ngôn ngữ kịch bản là JavaScript, các biến JavaScript thông thường (được khai báo bằng

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
1 hoặc
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
2 hoặc
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
3) trong một khối mã có thể không được xác định bên ngoài khối mã đó. Nếu bạn cần một biến để duy trì giữa các bước, hãy đặt biến đó làm biến bot đặc biệt với
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
4 để giá trị
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
5 nằm trong phạm vi xuyên suốt tập lệnh

Đối tượng JavaScript toàn cầu

Ngoài tất cả các cấu trúc ngôn ngữ JavaScript tiêu chuẩn, các khối mã hiển thị một số đối tượng quan trọng dành riêng cho Loadster mà bạn có thể sử dụng trong tập lệnh của mình

người máy

Đối tượng

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
6 là toàn cầu trong ngữ cảnh của một bot duy nhất. Nó đại diện cho bot hiện đang thực thi khối mã. Đối tượng
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
6 này hiển thị các phương thức sau

/**
* Pause for a specific number of milliseconds (these are synonymous).
*/
bot.wait(milliseconds);
bot.sleep(milliseconds);

/**
* Get the time, in milliseconds, relative to the start of the test.
*/
bot.getTime();

/**
* Get and set bot variables.
*/
bot.getVariable('account'); // get the value of the "account" bot variable
bot.getVariable('account', 1); // get the second column, if it's a multi-column dataset
bot.setVariable('user', 'a1350'); // set a bot variable

/**
* Override the global halt setting, just for this bot.
* This will allow HTTP 4xx/5xx and other things that would normally stop the script, without stopping the script.
*/
bot.setHaltOnErrors(false);

/**
* Tells how many iterations of the script have been completed by this bot, starting with 0.
*/
bot.getIteration();

/**
* Identifies which bot is currently running the script.
* This can be useful in special cases when only certain bots should perform certain actions.
*/
bot.getBotNumber(); // index of the bot in the group, starting with 0
bot.getBotGroupNumber(); // index of the bot group, starting with 0
bot.getBotIdentifier(); // unique bot identifier string with a guid

/**
* Starts and stops a timer. When you use custom timers, the amount of time elapsed between starting and
* stopping the timer will be reported in the results.
*/
bot.startTimer("My Checkout Flow");
bot.stopTimer("My Checkout Flow");

/**
* Runs a custom function inside a timer. This is shorthand for starting and stopping the timer separately.
*/
bot.timer("My Checkout Flow", () => {
});

/**
 * Makes the bot exit the script early.
 */
bot.exit();

Điều quan trọng cần lưu ý là mặc dù cú pháp là JavaScript nhưng các phương thức này đều đồng bộ. Không cần phải thực hiện một chuỗi lời hứa hoặc gọi lại hoặc bất cứ điều gì tương tự, bởi vì quá trình xử lý thực tế được thực hiện ở hậu trường bởi các công nhân đa luồng, vì vậy lập trình đồng bộ ở đây không phải là một thực hành nguy hiểm nếu bạn xuất thân từ nền tảng

http

Mọi bot đều có quyền truy cập vào ứng dụng khách HTTP có tên là

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
8 có thể đưa ra yêu cầu. Các bước này tương đương với các bước HTTP thông thường trong tập lệnh HTTP, nhưng bạn cũng có thể chạy chúng theo chương trình từ một khối mã

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');

trình duyệt

Trong tập lệnh trình duyệt, các khối mã cũng cung cấp cho bạn quyền truy cập có thể lập trình trực tiếp vào phiên bản trình duyệt của bot thông qua đối tượng

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
9

Tương tác trang trong một khối mã

Dưới đây là một vài ví dụ về tập lệnh trình duyệt cơ bản trong một khối mã, rất giống với những gì bạn có thể thực hiện với các bước riêng lẻ

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);

Chúng đang chặn các cuộc gọi đồng bộ với quá trình xử lý diễn ra ở hậu trường, vì vậy bạn không cần sử dụng

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
0 hoặc lệnh gọi lại hoặc chuỗi lời hứa cho các hành động trình duyệt tuần tự

Đánh giá JavaScript trong trình duyệt từ một khối mã

Các khối mã chạy bên ngoài trình duyệt, vì vậy chúng không có quyền truy cập vào trạng thái JavaScript bên trong trình duyệt của bot, chẳng hạn như các phần tử DOM và các biến trên trang

Để truy cập từ khối mã của bạn, bạn có thể sử dụng

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
1, giống như khối đánh giá nhưng được nhúng trong khối mã

Đây là một ví dụ về luồng điều khiển trong một khối mã kiểm tra xem có bao nhiêu phần tử

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
2 tồn tại trong trình duyệt. Nếu nó lớn hơn 0, nó sẽ lưu trữ một. Nếu không, nó sẽ ghi một tin nhắn và không làm gì cả

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}

Hãy nhớ rằng luồng điều khiển đang diễn ra trong khối mã bên ngoài trình duyệt. Chỉ JavaScript được chuyển đến

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
3 xảy ra bên trong trình duyệt

Làm việc với cửa sổ trình duyệt

Mỗi bot trình duyệt có cửa sổ trình duyệt ẩn danh riêng để bắt đầu và bạn có thể mở các cửa sổ/tab bổ sung trong tập lệnh của mình. Trang web của bạn cũng có thể tự động mở các cửa sổ/tab khác

Dưới đây là một số cách bạn có thể mở, đóng, thay đổi kích thước và chuyển đổi giữa các cửa sổ trong tập lệnh trình duyệt

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);

Truyền tiêu đề tùy chỉnh trong tập lệnh trình duyệt

Bạn có thể cần bot gửi tiêu đề yêu cầu tùy chỉnh với mọi yêu cầu

Đôi khi điều này là cần thiết để bỏ qua bộ lọc bot hoặc bảo vệ DDoS của trang web của bạn để cho phép lưu lượng truy cập thử nghiệm. Bạn có thể cần ghi đè tiêu đề

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
4 vì những lý do tương tự

// Set a global request header to send to all domains
browser.setGlobalHeader('X-Loadster', 'true');

// Remove a global request header
browser.removeGlobalHeader('X-Loadster');

// Set HTTP Basic or NTLM credentials for any site that requires them
browser.setHttpAuthentication('myUsername', 'myPassword');

Bạn có thể đặt và bỏ đặt các tiêu đề chung tại bất kỳ điểm nào trong tập lệnh của mình bằng một khối mã. Sau khi bạn đặt tiêu đề chung, bot sẽ gửi tiêu đề đó cùng với tất cả các yêu cầu tiếp theo, cho đến khi bạn xóa tiêu đề đó hoặc đạt đến phần cuối của tập lệnh

Xác thực HTTP Basic hoặc NTLM trong tập lệnh trình duyệt

Nếu trang web của bạn yêu cầu xác thực HTTP Basic hoặc NTLM, bạn có thể cung cấp thông tin đăng nhập trong một khối mã

// Set HTTP Basic or NTLM credentials for any site that requires them
browser.setHttpAuthentication('myUsername', 'myPassword');

Sau khi bạn đặt thông tin xác thực tên người dùng và mật khẩu trong tập lệnh của mình, bot sẽ gửi chúng đến bất kỳ trang web nào yêu cầu chúng, vì vậy hãy đảm bảo bạn không kiểm tra bất kỳ trang web độc hại nào có thể đánh cắp chúng

Hãy ghi nhớ, phương pháp này chỉ hoạt động để xác thực giao thức HTTP, không phải là xác thực dựa trên biểu mẫu phổ biến hơn được sử dụng trong các ứng dụng web hiện nay

Trả lời hộp thoại xác nhận của trình duyệt

Các trình duyệt vốn hỗ trợ xác nhận và cảnh báo để nhận đầu vào của người dùng. Đây là một chút lỗi thời và thay vào đó, nhiều ứng dụng sử dụng các hộp thoại phương thức tùy chỉnh trên trang. Tuy nhiên, các bot Loadster biết cách phản hồi các cửa sổ bật lên xác nhận này

Theo mặc định, các bot luôn trả lời phủ định bằng cách nhấp vào Hủy trên một thông báo xác nhận và chúng chỉ cần đóng một thông báo cảnh báo. Nếu bạn cần trả lời xác nhận ở dạng khẳng định (nhấp vào OK), bạn có thể báo trước cho bot

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);

// Answer confirm() dialogs with "Cancel" instead of "OK"
browser.setDialogConfirmation(false);

Nếu bạn vượt qua

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
5 tại đây, bot sẽ trả lời OK hoặc Yes cho các lời nhắc xác nhận tiếp theo. Bạn có thể trở lại hành vi ít dễ chịu ban đầu bằng cách vượt qua
let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
6

bảng điều khiển

Các khối mã cũng hiển thị một

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
7 đơn giản để ghi nhật ký

console.log(message);
console.warn(message);
console.error(message);

Thông báo được ghi vào bảng điều khiển hiển thị trong nhật ký tập lệnh và hỗ trợ bạn gỡ lỗi tập lệnh

JSON

Nếu đang thử nghiệm API, bạn sẽ thường cần phân tích cú pháp dữ liệu JSON để xem xét các thuộc tính cụ thể. Bạn có thể sử dụng

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
8 thông thường cho việc này

var simple = JSON.parse("{a: 1}"); // parse an arbitrary JSON string
var body = JSON.parse(response.string()); // parse the response body in a validator

XML

Vì phân tích cú pháp XML không phải là một tính năng ngôn ngữ tiêu chuẩn của JavaScript, Loadster bao gồm trình phân tích cú pháp xmldoc mã nguồn mở. Tài liệu bổ sung cho trình phân tích cú pháp này có sẵn trên GitHub, nhưng đây là một ví dụ nhanh

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
0

định dạng

Loadster cung cấp thư viện

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
9 tích hợp để giúp bạn mã hóa và giải mã chuỗi cũng như tạo dấu thời gian, UUID và dữ liệu ngẫu nhiên. Dưới đây là một số ví dụ về đầu vào và đầu ra

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
1

tiền điện tử

Đôi khi, bạn có thể cần áp dụng hàm băm cho một phần dữ liệu (với bí mật tùy chọn) để tạo giá trị băm mà máy chủ của bạn mong đợi. Một số hàm băm được sử dụng phổ biến hơn được bao gồm trong thư viện

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
0 tích hợp của Loadster mà bạn có thể gọi từ bất kỳ khối mã nào

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
2

Đừng quá phấn khích với từ “tiền điện tử” – đối tượng được gọi như vậy bởi vì nó là nơi chứa các hàm mã hóa và hàm băm, không nhất thiết phải liên quan đến tiền điện tử

Ví dụ về những việc cần làm trong Code Blocks

Không gì bằng học bằng ví dụ. Dưới đây là một vài ví dụ giả tạo về những điều bạn có thể làm với các khối mã

ví dụ 1. Thực hiện các yêu cầu HTTP có lập trình theo chuỗi

Đối tượng

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
8 (đại diện cho tác nhân người dùng HTTP thuộc bot hiện đang chạy) có các phương thức cho tất cả các phương thức HTTP phổ biến (GET, POST, v.v.)

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
3

ví dụ 2. Chuyển tiêu đề yêu cầu tùy chỉnh bằng yêu cầu HTTP

Bạn có thể chuyển các tiêu đề yêu cầu tùy chỉnh với mỗi yêu cầu, dưới dạng một đối tượng có các cặp khóa-giá trị hoặc trong một mảng

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
4

ví dụ 3. Xác thực các phản hồi HTTP bằng các hàm trình xác thực

Trình xác thực (tương tự như Quy tắc xác thực mà bạn có thể sử dụng với các bước HTTP thông thường) gọi hàm JavaScript để kiểm tra phản hồi và trả về true nếu nó hợp lệ hoặc sai nếu không

Các hàm trình xác thực có thể là các hàm JavaScript bình thường hoặc các hàm mũi tên ES2016+ mới hơn

Bạn có thể chỉ định nhiều hàm trình xác thực cho một phản hồi

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
5

Ví dụ 4. Nắm bắt các biến theo chương trình từ phản hồi HTTP

Thông thường, máy chủ sẽ gửi cho bạn một số dữ liệu mà bạn cần lưu và sử dụng sau này trong tập lệnh của mình

Trong các khối mã, bạn có thể nắm bắt chúng từ phản hồi và lưu trữ chúng bằng chức năng trình xác thực. Lưu ý rằng chúng tôi cũng sử dụng

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
2 để chụp ảnh;

Chỉ cần gọi

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
3 ở bất kỳ đâu trong khối mã của bạn để đặt biến trong phạm vi bot. Các biến đặc biệt này có sẵn cho toàn bộ lần lặp lại tập lệnh Loadster của bot, không giống như các biến JavaScript thông thường nằm trong phạm vi khối mã riêng lẻ và có thể không có sẵn cho các bước tiếp theo

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
6

Ví dụ 5. Chấp nhận mã trạng thái HTTP 4xx/5xx

Theo mặc định, Loadster tự động diễn giải mọi mã trạng thái HTTP 400 trở lên là lỗi và báo cáo như vậy

Nhưng có những lúc bạn thực sự muốn mã trạng thái HTTP 4xx hoặc 5xx. Ví dụ: bạn có thể đang thử nghiệm API REST và mong đợi nó trả về Xung đột HTTP 409 khi tài nguyên đã tồn tại

Trong những trường hợp như vậy, bạn có thể sử dụng

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
4 với yêu cầu của mình để Loadster sẽ bỏ qua mã trạng thái HTTP và bạn có thể tự diễn giải mã trạng thái

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
7

Ví dụ 6. Mã hóa và giải mã với Base64

Mã hóa Base64 thường được sử dụng để mã hóa dữ liệu không phải chuỗi dưới dạng chuỗi ASCII, vì vậy dữ liệu này có thể được gửi qua các giao thức dựa trên văn bản hoặc được so sánh trong trình soạn thảo văn bản. Trong các khối mã, chúng tôi hiển thị thư viện

let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
9 để xử lý mã hóa và giải mã Base64

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
8

Nếu dữ liệu được mã hóa Base64 thực sự là một chuỗi, bạn sẽ cần chuyển đổi mảng byte thành chuỗi sau đó bằng cách sử dụng

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
6, như thế này

// Make HTTP requests
http.get(url, args);
http.post(url, body, args);
http.put(url, body, args);
http.patch(url, body, args);
http.delete(url, args);
http.options(url, args);
http.trace(url, args);

// Add a header to all future requests to a host
http.addHostHeader('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Remove host headers matching a host/name/value
http.removeHostHeaders('slothereum.cc');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication');
http.removeHostHeaders('slothereum.cc', 'X-Slothentication', 'ymmv2880');

// Add or remove global headers for all requests to all hosts
http.addGlobalHeader('X-Automated-Testing', 'yes');
http.removeGlobalHeader('X-Automated-Testing');
9

Luồng điều khiển, vòng lặp và logic có điều kiện

Vòng lặp và điều kiện có thể được thực hiện với tất cả các cấu trúc ngôn ngữ JavaScript thông thường. Ví dụ đơn giản này cho thấy vòng lặp với vòng lặp

// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
7, hàm mô đun
// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
8, câu lệnh
// Resize the bot's browser viewport
browser.setViewportSize(1800, 1200);

// List all of the bot's browser windows/tabs
browser.listWindows();

// Opens a new window/tab and makes it active
browser.openWindow();

// Get the bot's currently active browser window/tab
browser.getActiveWindow();  // 0, 1, 2...

// Focus a different browser window/tab
browser.setActiveWindow(0); // the bot's original tab
browser.setActiveWindow(1); // the first opened tab
browser.setActiveWindow(2); // and so on...

// Close a browser window/tab
browser.closeWindow(1);
9, thiết lập và nhận các biến bot đặc biệt với phép nội suy biến của
// Set a global request header to send to all domains
browser.setGlobalHeader('X-Loadster', 'true');

// Remove a global request header
browser.removeGlobalHeader('X-Loadster');

// Set HTTP Basic or NTLM credentials for any site that requires them
browser.setHttpAuthentication('myUsername', 'myPassword');
0

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
0

Hạn chế của khối mã

Hãy nhớ rằng các khối mã không chạy trong trình duyệt, vì vậy chúng không có quyền truy cập trực tiếp vào DOM trực tiếp theo cách mà JavaScript dựa trên trình duyệt thực hiện. Hãy cẩn thận để không nhầm lẫn chúng với JavaScript trên trang (jQuery, React, v.v.). Thay vào đó, các tập lệnh được thực thi bởi các bot của bạn chạy trên công cụ tải và chúng có quyền truy cập vào các đối tượng như

// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
6,
// Navigate to a page and click on a link
browser.navigate('https://slothereum.cc');
browser.click('a[href=/login]');

// Wait 3 seconds
bot.wait(3000);

// Wait for an element to be "visible", "hidden", "attached", or "detached"
browser.waitFor('.spinning-overlay', 'hidden');

// Wait up to 5 seconds for an element to be hidden, and then silently move on
browser.waitFor('.spinning-overlay', 'hidden', { timeout: 5000, silent: true });

// Type a username and password and submit the form
browser.type('.username', 'sloth');
browser.type('.password', 'chunk');

// Type instantly with no delay between keystrokes
browser.type('.greeting', 'Hello', { delay: 0 });

// Type very slowly with 1250ms between keystrokes
browser.type('.greeting', 'Hello', { delay: 1250 });

// Press individual keys on the keyboard (for testing games, etc)
browser.keyboard.press('ArrowUp');
browser.keyboard.press('X');
browser.keyboard.press('Enter');

// Click the submit button on a form
browser.click('form input[type=submit]');

// Click something if it shows up within 3 seconds, otherwise move on without complaining
browser.click('#cookie-warning', { timeout: 3000, silent: true });

// Take a screenshot
browser.screenshot();

// Choose a file in a file input
browser.chooseFiles('input[type=file]', [{ name: 'lolcat', contentType: 'image/jpeg', contentBase64: 'UklGRuZ/AABXRUJQVlA4INp/AABwrAGdASr0AQACPjkYi0QiIaET' }]);

// Hover a menu and click the first item
browser.hover('.menu');
browser.click('.menu li:first-child');

// Select an option from a select element
browser.selectByIndex('select[name=countries]', 1);
browser.selectByValue('select[name=countries]', 'HR');
browser.selectByText('select[name=countries]', 'Croatia');

// Answer confirm() dialogs with "OK" instead of "Cancel"
browser.setDialogConfirmation(true);
8,
// Set a global request header to send to all domains
browser.setGlobalHeader('X-Loadster', 'true');

// Remove a global request header
browser.removeGlobalHeader('X-Loadster');

// Set HTTP Basic or NTLM credentials for any site that requires them
browser.setHttpAuthentication('myUsername', 'myPassword');
3,
let invoiceCount = browser.eval("document.querySelectorAll('.invoice').length");

if (invoiceCount > 0) {
    browser.click('.invoice .archive-button');
} else {
    console.log('There are no more invoices!');
}
7, v.v.

Nếu bạn cần chạy mã bên trong trình duyệt, hãy xem Đánh giá khối

Nhận trợ giúp với các khối mã của bạn

Nếu bạn gặp khó khăn với các khối mã hoặc muốn yêu cầu một thư viện trợ giúp khác, chúng tôi rất sẵn lòng trợ giúp. Chỉ cần gửi email cho chúng tôi. Chúng tôi luôn quan tâm đến cách bạn sử dụng các khối mã trong Loadster

Khối mã nghĩa là gì?

Trong lập trình máy tính, khối hoặc khối mã hoặc khối mã là cấu trúc từ vựng của mã nguồn được nhóm lại với nhau . Các khối bao gồm một hoặc nhiều khai báo và câu lệnh.

Ví dụ về khối mã là gì?

Ví dụ về mã khối là Mã Reed–Solomon, mã Hamming, mã Hadamard, mã Expander, mã Golay và mã Reed–Muller . Những ví dụ này cũng thuộc loại mã tuyến tính và do đó chúng được gọi là mã khối tuyến tính.

Mục đích của các khối mã * là gì?

CodeBlocks là một C++ IDE (Môi trường phát triển tích hợp) đa nền tảng cho phép các nhà phát triển viết mã, gỡ lỗi, xây dựng, chạy và triển khai các dự án . Nó cung cấp các tùy chọn mạnh mẽ để tùy chỉnh môi trường phát triển của bạn, chẳng hạn như tích hợp kiểm soát nguồn và chế độ xem đồ họa về mức sử dụng bộ nhớ và CPU.

CodeBlocks có thể chạy JavaScript không?

Các khối mã chạy bên ngoài trình duyệt nên chúng không có quyền truy cập vào trạng thái JavaScript bên trong trình duyệt của bot , chẳng hạn như các phần tử DOM và biến trên trang.