LoginSignup
4
4

More than 5 years have passed since last update.

SeleniumでCookie Clickerを自動化

Last updated at Posted at 2016-07-30

Seleniumの勉強をかねて、ブラウザゲーム「Cookie Clicker」を自動化してみました。

Cookie Clickerについて

クッキーをひたすらクリックしてクッキーを増やす、ブラウザゲームです。
Flashが使われていないので、Seleniumでの自動化が簡単にできます。
http://orteil.dashnet.org/cookieclicker/

Cookie Clickerの自動化

自動化の処理の流れ

  1. セーブファイルをインポートする
    1. [Options]ボタンをクリックする
    2. [Import Save]ボタンをクリックする ⇒ [Import Save]ダイアログが開く
    3. セーブファイルの中身を、テキストボックスに入力する
    4. [Load]ボタンを押す。
  2. クッキーをひたすらクリックする
  3. セーブファイルをエクスポートする
    1. [Save to file]ボタンを押す ⇒ セーブファイルがダウンロードされる

Cookie Clicker_全体像.png
Import Saveダイアログ.png

環境

  • Firefox 46.0
  • Selenium 2.53.0
  • Cookie Clicker 2016/07/24版

ソース

CookieClickerTest.java
package mytest.gui;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class CookieClickerTest {
    /** Cookie ClickerのImport File. 自動クリックさせる前に読み込ませる */
    private static String importFile = "E:/workspace/mytest/input/cookieclicker/import.txt";

    /** クッキーをクリックする回数 */
    private static int countOfClick = 10;

    public static void main(String[] args) {
        //初期処理
        FirefoxProfile profile = new FirefoxProfile();
        //Downloadディレクトリにダウンロードする
        profile.setPreference("browser.download.folderList",1); 
        //textファイルのダウンロード時に確認ダイアログを表示しない
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/plain");
        FirefoxDriver driver = new FirefoxDriver(profile);

        // Cookie Clickerを開く
        driver.get("http://orteil.dashnet.org/cookieclicker/");

        //ファイルを読み込みSave用のCode値を取得する
        try {
            //Optionsボタンをクリック
            driver.findElement(By.id("prefsButton")).click();

            //Import Saveボタンをクリックする
            driver.findElement(By.linkText("Import save")).click();

            //Import Fileを読み込む
            String saveCode = FileUtils.readFileToString(new File(importFile), "UTF-8");

            //Save用Codeを入力する
            driver.findElement(By.id("textareaPrompt")).sendKeys(saveCode);

            //Loadボタンをクリックする。
            driver.findElement(By.id("promptOption0")).click();

        } catch (IOException e) {
            e.printStackTrace();
        }

        //クッキーを指定回数分クリックする
        for (int i=0; i<countOfClick; i++) {
            driver.findElement(By.id("bigCookie")).click();
        }

        //Save to fileボタンをクリックする ⇒ ファイルがダウンロードされる
        driver.findElement(By.linkText("Save to file")).click();

        //ブラウザを終了する
        driver.quit();
    }
}

ファイルのダウンロードについて

ダウンロード確認ダイアログが表示されると、次の処理に進みません。
ダウンロードダイアログが表示されないよう、FirefoxProfileで設定しました。
https://developer.mozilla.org/ja/docs/Download_Manager_preferences

まとめ

  • セーブファイルのインポートは、[Load from file]ボタンを押して、ファイルアップロードする予定でした。しかしうまく自動化できなかったので、[Import Save]ボタンでセーブコードを入力する方法に変更しました。

  • Cookie Clickerには、クッキーを効率良く増やすアイテムがあります。それらのアイテムも自動化に取り入れ、さらに効率よくクッキーが取得できるでしょう。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4