开发者

How to run Google Apps script from HtmlUnit?

I have a link in a Google Sites page that runs a Google Apps script. It works fine when I manually click the link in Firefox.

Everything seems to work OK when I do the same using HtmlUnit. With HtmlUnit I can sign in (have to be signed in when ru开发者_JAVA百科nning Google Apps scripts), get to the Web page, identify the link and click it, but the script does not run.

The guts of the code is straightforward:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = webClient.getPage("https://sites.google.com/site/mywebpage");
HtmlAnchor anchor = page.getAnchorByName("MyAnchor");
anchor.click();

So what is missing, or what I am doing wrong?

I created the original link in the Web page with:

<a href="/macros/sites?action=run&lib=094bahv51120b3d65&funcName=MyFunctionName" name="MyAnchor">Click here to run function</a>

On saving the Web page edit, Google Sites automatically changed this to:

<a href="#" name="MyAnchor" class="apps-script-run-link" id="ACiQKe2HyNeo..etc...01120b3d65#MyFunctionName">Click here to run function</a>

Is click() not working because it acts on the href attribute that is now just "#" instead of the link definition? Or does HtmlUnit's language support for JavaScript (Mozilla Rhino) not execute the Javascript correctly to process the clicked link (meaning that I need to replace Rhino with something that does recognised the Javascript in a Google Sites Web page)? Or some other basic mistake I have made?

I am lost, so any help much appreciated!!

UPDATE

I followed your suggestion, Rodney, and found that the issue was not logging off Google sites. Using HtmlUnit to click on the 'Sign Out' link after clicking the link to execute the script, makes the script run OK. The only problem is it brings up the following error (which doesn't stop the script running):

SEVERE: Job run failed with unexpected RuntimeException:

TypeError: Cannot set property "innerHTML" of null to "Finished script: Finished script MyFunctionName"

UPDATE 2

Have now solved this prob too. There was insufficient time to allow the script to complete before going on to sign-out. Adding this fixed it:

webClient.waitForBackgroundJavaScriptStartingBefore(5000);

Many thanks for your help, Rodney - and Naveen for the good edit!


HtmlUnit has rather good Javascript support, so I doubt that's the issue.

How do you know the script hasn't run? Or better, how would you know, from an HtmlUnit point-of-view, if the script has run successfully? When you click the link in FF, you presumably see something happen. You are relying on human vision and a browser meant for human use. With HtmlUnit, you need to instruct your code to "see" the same result, whether by examining the subsequent DOM tree, or handling a popup, or whatever is needed. It's a bit of a shift in thinking.

What does your script do? Manipulate the content of the page? Open a dialog? Redirect to another page? Some things HtmlUnit will handle automatically, and some (like opening dialogs) need handlers defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜