问题
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?
回答1:
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>']
}
回答2:
In some ways I think testing chrome apps is similar to testing native applications - typical approaches used there include building extra hooks into your code for tests, and making use of OS-supplied automation tools (accessibility services, OSX automator, etc.)
Here's one thing that may help: chrome supports a command-line flag:
<path to chrome> --load-and-launch-app=<path to app code directory>
which, on startup, will cause it to load an unpacked version of your app's source code (equivalent to hitting "load unpacked extension.." from the chrome://extensions page) and then send the chrome.app.runtime.onLaunched event to your code.
来源:https://stackoverflow.com/questions/21022837/how-to-automate-testing-of-chrome-packaged-apps