Skip to content

Commit

Permalink
[JS] Add code sample for executeScript and takeElementScreenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
harsha509 committed Apr 18, 2024
1 parent 9b8bb91 commit 1196cf2
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions examples/javascript/test/interactions/windows.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const {Builder} = require('selenium-webdriver');
const {Builder, By} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const assert = require("node:assert");
let opts = new chrome.Options();
opts.addArguments('--headless');

let startIndex = 0
let endIndex = 5
let pdfMagicNumber = 'JVBER'
let imgMagicNumber = 'iVBOR'
let base64Code

describe('Interactions - Windows', function () {
let driver;

before(async function () {
driver = await new Builder().forBrowser('chrome').setChromeOptions(opts).build();
});

after(async () => await driver.quit());

it('Should be able to print page to pdf', async function () {

await driver.get('https://www.selenium.dev/selenium/web/alerts.html');
let base64 = await driver.printPage({pageRanges: ["1-2"]});
// page can be saved as a PDF as below
Expand All @@ -27,4 +27,24 @@ describe('Interactions - Windows', function () {
base64Code = base64.slice(startIndex, endIndex)
assert.strictEqual(base64Code, pdfMagicNumber)
});

it('Should be able to get text using executeScript', async function () {
await driver.get('https://www.selenium.dev/selenium/web/javascriptPage.html');
// Stores the header element
let header = await driver.findElement(By.css('h1'));

// Executing JavaScript to capture innerText of header element
let text = await driver.executeScript('return arguments[0].innerText', header);
assert.strictEqual(text, `Type Stuff`)
});

it('Should be able to take Element Screenshot', async function () {
await driver.get('https://www.selenium.dev/selenium/web/javascriptPage.html');

let header = await driver.findElement(By.css('h1'));
// Captures the element screenshot
let encodedString = await header.takeScreenshot(true);
base64Code = encodedString.slice(startIndex, endIndex)
assert.strictEqual(base64Code, imgMagicNumber)
});
});

0 comments on commit 1196cf2

Please sign in to comment.