Skip to content

Instantly share code, notes, and snippets.

@gomo
gomo / gist:9130020
Last active August 29, 2015 13:56
seleniumをphantomjsで動かす

seleniumを起動

java -jar /path/to/selenium-server-standalone-2.39.0.jar -role hub

phantomjsを起動

phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
@gomo
gomo / gist:9678115
Last active August 29, 2015 13:57
STDOUTに即座に`flush`する`java.util.logging.Handler`
Handler handler = new StreamHandler(){
//初期化ブロック
{
setOutputStream(System.out);
setLevel(Level.ALL);
}
@Override
public void publish(LogRecord record){
super.publish(record);
@gomo
gomo / post.java
Last active August 29, 2015 13:59
HttpPostとServletFileUploadでContentTypeを取得できない
//POSTを投げる側のコード
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/test/post");
MultipartEntityBuilder params = MultipartEntityBuilder.create();
params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//ContentTypeをセットしないと文字化けします
params.addTextBody("text", "マルチバイトテキスト", ContentType.create("text/plain", MIME.UTF8_CHARSET));
@gomo
gomo / promise-queue.md
Last active February 18, 2021 06:02
Promise Queue

非同期処理を含むイベントが連続で発火されて、それを、同期に実行したい(発火された順番で必ず前の処理が終わってから実行したい)時に使えるテクニック。めちゃめちゃに実行される非同期処理を一列に整列させるイメージです。

var promises = [];
promises.push(Promise.resolve());

promises.push(
  promises.shift().then(() => {
    return new Promise(resolve => {
 setTimeout(() => {
@gomo
gomo / test.js
Created June 21, 2017 04:47
Javascript selenium with headless chrome
import webdriver from 'selenium-webdriver'
import test from 'selenium-webdriver/testing'
test.describe('Test', () => {
test.it('with headless chrome.', () => {
const chromeCapabilities = webdriver.Capabilities.chrome();
chromeCapabilities.set('chromeOptions', {
'args': ['--headless', '--disable-gpu']
});
driver = new webdriver.Builder()
@gomo
gomo / plane_obj.js
Last active June 29, 2017 02:55
A function in a plane object is the named function in nodejs (v6.10.2).
const obj = {
foobar: function(){}
}
obj.foobar.toString() == (function(){}).toString() //false
obj.foobar.toString() == (function foobar(){}).toString() //true
obj.foobar.toString() //function foobar() {}
@gomo
gomo / README.md
Last active July 5, 2017 07:01
How to check checkboxes by values using javascript selenium.

Both getAttribute and isSelected are asynchronous methods, so my solution has become such complicated. Does anyone know a simpler solution?

@gomo
gomo / array_length_validator.rb
Last active February 5, 2018 03:41
Array length validator for rails
# app/validators/array_length_validator.rb
class ArrayLengthValidator < ActiveModel::Validations::LengthValidator
def initialize(options)
ActiveModel::Validations::LengthValidator::MESSAGES.each do |key, value|
options[value] = I18n.t("errors.messages.array_#{value}")
end
super(options)
end
end
@gomo
gomo / Usage.md
Last active March 14, 2018 08:48
The utility for grouped_collection_select of rails.

In your controller.

users = [
  [1, "Rollam Westerling"],
  [2, "Jeffory Mallister"],
  [3, "Rhaenys Targaryen"],
  [4, "Ulrick Dayne"],
@gomo
gomo / recruit.rb
Created June 12, 2018 01:14
Validator for an array with a whitelist.
# app/models/forms/recruit.rb
class Forms::Recruit
include ActiveModel::Model
attr_accessor :job_types
validates :job_types, presence: true, white_list: {list: JOB_TYPES, allow_blank: true}
end