Use Device buttons using WD with Protractor tests

Spread the love

This is a short build up to my previous post where I gave an explanation of setting up Ionic E2E tests with Protractor and Appium. We started using them at LeanAgri and going further we faced some challenges with Protractor.
Protractor by default uses selenium-webdriver which does not integrate completely with Appium’s APIs. The API we were looking for was pressing Hardware buttons such as the hardware Back button(Protractor’s browser.navigate().back() doesn’t work for hybrid apps). Appium’s documentation lists down that WD.js supports the press_keycode API which can allow us to send Device keycodes such as Press Back button. You can get list of all Keycodes here: https://developer.android.com/reference/android/view/KeyEvent
So what we need to do is use WD.js as the underlying driver instead of selenium-webdriver. It’s fairly straightforward to do using WD-bridge

var wd = require('wd'),
wdBridge = require('wd-bridge')(wd);
// in Protractor Config, add wd-bridge
onPrepare: function () {
wdBridge.initFromProtractor(config);
}

Now in your tests you can access the WD driver object using wdBrowser and also access any of the WD APIs which can’t be used with Protractor’s browser object.

it('Test which allows to Press button and back', async () => {
await element(by.css('.button')).click();
await(browser.waitForAngular());
// Verify we are on next page
// Go back
await wdBrowser.pressKeycode(4);
await(browser.waitForAngular());
// Verify that we are back on previous page
});

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *