0

I'm trying to automate downloading the backup file created by Teamwork. The way it works is you login, go to a page which loads a dynamically generated iframe with a src="https://tw-backup.teamwork.com/ext.cfm?backupaction=downloadLatestMySQLBackup".

I've tried getting the actual link from from the iframe, but I haven't gotten that to work. However, the truncated link I'm using seems to work if I enter it in the browser. So I'm just trying to open it directly.

Unfortunately, it just seems to hang.

EDIT

phantomjs --version = 1.9.8;

Getting this error:

[debug] [phantom] url changed to "https://tw-backup.teamwork.com/ext.cfm?backupa
ction=downloadLatestMySQLBackup"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Done 8 steps in 3427ms
finished
Unsafe JavaScript attempt to access frame with URL about:blank from frame with U
RL file:///c:/Users/Brad/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap
.js. Domains, protocols and ports must match.

So is the problem that I'm trying to access a different sub-domain than from where I start?

You can get a free trial of Teamwork if you're interested in this problem. And BTW, we're making good use it for project management.

var casper = require('casper').create();

casper.start('https://myco.teamwork.com/', function () {

    console.log("start");

    this.waitForSelector("input[name='userLogin']",
        function success() {
            this.sendKeys("input[name='userLogin']", "me@myco.org");
        },
        function fail() {
            test.assertExists("input[name='userLogin']");
        });
    this.waitForSelector("input[name='password']",
        function success() {
            this.sendKeys("input[name='password']", "somePassword");
            console.log("login successful");
        },
        function fail() {
            test.assertExists("input[name='password']");
            console.log("login failed");
        });

    this.thenOpen('https://tw-backup.teamwork.com/ext.cfm?backupaction=downloadLatestMySQLBackup');   

});
Brad Rhoads
  • 1,828
  • 3
  • 29
  • 52
  • What PhantomJS version do you have? Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). Maybe there are errors. Try to run CasperJS with `--ssl-protocol=any --ignore-ssl-errors=true`. – Artjom B. Apr 19 '15 at 08:40
  • @ArtjomB. Please see edits.. – Brad Rhoads Apr 20 '15 at 17:24
  • That "error" (Unsafe JavaScript attempt...) suggests that you're using PhantomJS 1.9.8 and not 1.9.2. [It's actually not an error, but only some string that is printed when CasperJS is exiting](http://stackoverflow.com/questions/26608391/using-phantomjs-to-embed-all-images-of-a-webpage-produces-warnings-but-works). I don't see an error in your question. How do you verify that the second page load succeeded or failed? – Artjom B. Apr 20 '15 at 18:36
  • Right, it's 1.9.8 (typo). The log says, "[debug] [phantom] url changed to https://tw-backup.teamwork.com/ext.cfm?backupa ction=downloadLatestMySQLBackup". Doesn't that mean that the page load worked? – Brad Rhoads Apr 20 '15 at 18:45
  • Yes, it does, that's why I'm asking what steps come after that load and how you verify that it didn't work. – Artjom B. Apr 20 '15 at 18:46
  • I see. That link triggers a download of the backup file. The backup is a .zip. So obviously this is not a direct link to the actual file. But the response is the .zip, which I'm not getting. Putting that URL into a browser triggers the file download immediately. – Brad Rhoads Apr 20 '15 at 18:51
  • Yeah, PhantomJS doesn't download files. You might be successful with this: http://stackoverflow.com/questions/12461096/casperjs-download-file-without-specifying-url – Artjom B. Apr 20 '15 at 18:59

1 Answers1

0

I finally accomplished this by moving to Selenium and using the Chrome driver;The Firefox driver won't work because it pops up a save dialog.

Unfortunately, I haven't gotten this work headless. I was hoping that HtmlUnitDriver would work. And it still may be possible, but for my purposes having the browser open is actually OK.

Note that you need to have the Chrome webdriver installed. On the MAC, you just drop it in /usr/bin. On Windows, create a folder with both the .jar and the chromedriver.ext. Then execute it with:

C:\Program Files\teamworkbackup>java -Dwebdriver.chrome.driver=:"C:\Program files\teamworkdbackup\chromedriver.exe" -jar teamworkdbackup.jar

Source code:

package com.rhoads.teamwork.backup;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;


public class TeamworkBackup {

    public static void main(String[] args) throws Exception {

        WebDriver driver;

        String baseUrl;

        driver = new ChromeDriver();

        baseUrl = "https://myco.teamwork.com/index.cfm";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get(baseUrl + "/");
        Thread.sleep(4000);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys("superstrong");
        driver.findElement(By.id("userLogin")).clear();
        driver.findElement(By.id("userLogin")).sendKeys("someguy@example.com");
        driver.findElement(By.id("ordLoginSubmitBtn")).click();
        Thread.sleep(4000);
        driver.get("https://myco.teamwork.com/settings?display=export");
        Thread.sleep(4000);
        driver.switchTo().frame("backupFrame");
        Thread.sleep(4000);
        driver.findElement(By.linkText("Download")).click();
        Thread.sleep(4000);
        driver.quit();


    }
}
Brad Rhoads
  • 1,828
  • 3
  • 29
  • 52