Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate testing of Chrome packaged apps?

For regular web sites there are various tools to perform automated UI testing of applications, e.g. Selenium. However, how do I do this for Chrome Packaged Apps? My applications heavily uses Chrome App-specific APIs, so hosting it as a regular web page for testing won't work.

Any best practices or tools for this?

like image 279
Zef Hemel Avatar asked Nov 02 '22 07:11

Zef Hemel


1 Answers

If you can get the app window handle, then Selenium Webdriver will still work. I discovered this by accident when I had a call to driver.getAllWindowHandles() at the end of a bunch of failed tests. The tests failed because they didn't have the reference to the app window handle, but -- unexpectedly -- the last call to returned two window handles instead of one.

It may seem like the app window handle never appears after calling driver.getAllWindowHandles(), but if you keep calling this function, eventually its callback will receive an array of window handles containing both the browser [0] and app [1] window handles. I've gotten this to work via recursion but not with a simple while loop (something something webdriver being asynchronous).

For example, if you dig the javascript implementation of webdriver, try running the following test from this relevant bug report.

describe('A chrome app html page', function() {
  var appHandle = "";
  var recursionDepth= 0, maxDepth = 100; // edit as necessary.
  function getAppWindow(){
    browser.driver.getAllWindowHandles().then(function(handles){   
      if(handles.length == 1){
        recursionDepth += 1;
        if (recursionDepth == maxDepth) return false;
        getAppWindow();
      }
      if(handles.length == 2){
        browser.driver.switchTo().window(handles[1]);
        browser.driver.getWindowHandle().then(function(currentHandle){
          console.log("handles are" + handles);
          console.log("current handle is " + currentHandle);
          appHandle = currentHandle;
        });
      }
    });
  }
  getAppWindow();
  it('is on the second window handle', function(){
    expect(browser.driver.getWindowHandle()).toEqual(appHandle);
  }, 20000);
}); 

You will need the --load-and-launch-app= flag set somewhere, as Antony has helpfully pointed out. My protractor config file looks like this:

exports.config = {
  seleniumAddress: '<address of server>',
  capabilities: {
    'browserName': 'chrome',
    'chromeOptions':{
      'args': ['load-and-launch-app=<path to app code directory>']
    }
  },
  specs: ['<path to tests>']
}
like image 98
Frofroggy Avatar answered Nov 11 '22 04:11

Frofroggy