Miscellaneous Techniques
Explore more about Selenium debugging by learning advanced techniques.
We'll cover the following...
We'll cover the following...
Take a screenshot
Another way of debugging is to take a screenshot of the current browser window when an error/failure occurs. Selenium supports this in a very easy way:
var fs = require('fs');
driver.takeScreenshot().then(function(data) {
// Base64 encoded png
fs.writeFileSync(__dirname + "/tmp/screenshot1.png", data, 'base64');
});
However, there is one problem with the above script. When the above script runs for the second time, it will return an error The file already exists. So, a simple workaround is to write a file with the timestamped file name as:
// save to timestamped file, e.g. screenshot-12070944.pngDate.prototype.yyyymmdd = function() {var mm = this.getMonth() + 1; // getMonth() is zero-basedvar dd = this.getDate();return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('');};function writeScreenshot(data) {name = new Date().yyyymmdd() + ".png";var screenshotPath = __dirname + "/tmp/";var fs = require('fs');fs.writeFileSync(screenshotPath + name, data, 'base64');};test.it("Take screenshot saved to timestamped file", function() {driver.takeScreenshot().then(function(data) {writeScreenshot(data);});});
Testing the recipe
Practice the screenshot code given in the recipe above. To execute the example test script, click the Run button.
var assert = require('assert');
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var test = require('selenium-webdriver/testing');
var driver;
const timeOut = 15000;
const chromeCapabilities = webdriver.Capabilities.chrome();
chromeCapabilities.set('chromeOptions', {args: ['--headless', '--disable-gpu','--no-sandbox','--disable-dev-shm-usage']});
var site_root_url = "file://" + __dirname + "/../../site"
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
test.describe('Debug', function() {
test.before(function() {
this.timeout(timeOut);
driver = new webdriver.Builder()
.forBrowser('chrome')
.withCapabilities(chromeCapabilities)
.build();
});
test.after(function() {
driver.quit();
});
test.beforeEach(function() {
// runs before each test in this block
this.timeout(timeOut);
driver.get("file://" + __dirname + "/../../site/assert.html");
});
// test cases
var url = "";
test.it("Take screenshot", function() {
// this won't save to file, as Take screenshot returns a promise
// driver.takeScreenshot(__dirname + "/tmp/screenshot1.png");
var fs = require('fs');
driver.takeScreenshot().then(function(data) {
// Base64 encoded png
fs.writeFileSync(__dirname + "/tmp/screenshot1.png", data, 'base64');
});
});
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join(''); // padding
};
function writeScreenshot(data) {
name = new Date().yyyymmdd() + ".png";
var screenshotPath = __dirname + "/tmp/";
var fs = require('fs');
fs.writeFileSync(screenshotPath + name, data, 'base64');
};
test.it("Take screenshot saved to timestamped file", function() {
driver.takeScreenshot().then(function(data) {
writeScreenshot(data);
});
});
});Practice recipe for taking a time-stamped screenshot using Selenium
Leave browser open after test finishes
Whenever an error or a failure occurs during the test execution, a ...