使用CasperJS上传APK文件到Google Play

时间:2016-03-23 21:14:27

标签: javascript google-play casperjs

我需要创建一个脚本,首次将APK文件上传到Google Play(他们的API只允许在第一个版本发布后上传APK)。

这是我到目前为止的脚本:

var fs = require('fs');
var data = fs.read('test.cookies');
phantom.cookies = JSON.parse(data);

var x = require('casper').selectXPath;
var casper = require('casper').create({   
    //verbose: true, 
    //logLevel: 'debug',
    stepTimeout: 60000,
    pageSettings: {
        loadImages:  false,
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

casper.start().thenOpen('https://play.google.com/apps/publish/?dev_acc=14088528817615018970', function() {
    if (this.getCurrentUrl().indexOf('accounts.google.com') == -1) {
        // cookies working
        this.bypass(3);
    } else {
        console.log('sign in page opened!')
    }
});

casper.then(function() {
    console.log('populating form with email');

    this.evaluate(function() {
        document.querySelector('input#Email').value = "email@address.com";
        document.querySelector('input#next').click();
    });
})

casper.waitForSelector("input#Email-hidden", function() {
    console.log('populating form with password');

    this.evaluate(function() {
        document.querySelector('input#Passwd').value = "passwd";
        document.querySelector('input#signIn').click();
    });
})

//Wait to be redirected to the Home page, and then make a screenshot
casper.then(function(){
  console.log(this.getCurrentUrl());

    var cookies = JSON.stringify(phantom.cookies);
    fs.write('test.cookies', cookies, 644);
});

// Google play page
casper.then(function() {
  console.log('Current page: ' + this.getCurrentUrl());
});

casper.waitForSelector(".BVO4BTD-b-a.BVO4BTD-b-o.BVO4BTD-Be-a", function() {
    console.log('Add new app');

    this.evaluate(function() {
        document.querySelector('.BVO4BTD-b-a.BVO4BTD-b-o.BVO4BTD-Be-a').click();
    });
});

casper.waitForSelector(".BVO4BTD-hj-a", function() {
    console.log('Filling form');
    this.capture('5.png',{
      top: 0,
      left: 0,
      width: 500,
      height: 400
  });

    this.evaluate(function() {
        document.querySelector('.BVO4BTD-hj-a > input.gwt-TextBox').value = "App Teste";
        document.querySelector('.BVO4BTD-g-K .BVO4BTD-b-a.BVO4BTD-b-o').click();
    });
});

casper.waitForSelector(".BVO4BTD-gg-a .BVO4BTD-b-a.BVO4BTD-b-o", function() {
    this.capture('6.png',{
      top: 0,
      left: 0,
      width: 500,
      height: 400
  });
  console.log('Current page: ' + this.getCurrentUrl());

  console.log('Click for app upload');
  this.evaluate(function() {
    document.querySelector(".BVO4BTD-gg-a .BVO4BTD-b-a.BVO4BTD-b-o").click();
  });
});

casper.waitForSelector(".BVO4BTD-Re-b.BVO4BTD-g-U.BVO4BTD-d-b", function() {

    this.capture('7.png',{
      top: 0,
      left: 0,
      width: 500,
      height: 400
  });

    /**/


  this.evaluate(function() {
      document.querySelector(".BVO4BTD-Re-b.BVO4BTD-g-U.BVO4BTD-d-b .BVO4BTD-b-a.BVO4BTD-b-m").click();
      casper.page.uploadFile(".BVO4BTD-Re-b.BVO4BTD-g-U.BVO4BTD-d-b input[type=file]", '/path/to/apk/file.apk');
    });

    this.wait(3000, function() {
        this.capture('8.png',{
          top: 0,
          left: 0,
          width: 500,
          height: 400
      });
  });
});

casper.run(function() {
    console.log('end');
});

这些步骤已经奏效:

  • 在Google Play上登录(保存Cookie,以便跳过此部分)
  • 创建名为App Teste
  • 的应用
  • 转到上传APK屏幕

我无法使上传脚本正常工作,我猜Google Play的上传机制背后有很多javascript。

我上传的尝试发生在最后waitForSelector部分。

谢谢。

2 个答案:

答案 0 :(得分:1)

casper.page实际上只是一个幻想的WebPage对象,可能不应该在this.evaluate内使用。

要实现相同的目标,您可以尝试:

casper.waitForSelector(".form-selector", function() {
  // your capture code
  casper.page.uploadFile('selector', '/path/to/file');
});

casper.then(function() {
  // whatever you need to do after file upload
});

另外,要填写表单,您可以尝试casper.fill()

来源:http://docs.casperjs.org/en/latest/modules/casper.html#then(casper),http://phantomjs.org/api/webpage/method/upload-file.html(phantomjs)

答案 1 :(得分:0)

检查以下脚本是否可行 注意:'filename'是本地目录中文件的路径。

public class MyHolder extends RecyclerView.ViewHolder { private Model mMyModel; public MyHolder(Model myModel) { mMyModel = myModel; } public Model getMyModel() { return mMyModel; } } holder.button.setOnClickListener(v -> { holder.state = LOADING; notifyItemChanged(holder.getAdapterPosition()); callNetwork(..., () -> { /* this is the callback that runs on the main thread */ holder.state = CLOSED; int position = myList.indexOf(holder.getMyModel()); notifyItemChanged(position); }); });

var filename = /home/xyz/android/file_you_want_to_upload.apk