TRY AND ERROR

気になったこと、勉強したこと、その他雑記など色々メモしていきます。。Sometimes these posts will be written in English.,

Goでsclevine/agoutiを使ってページ全体をキャプチャする方法

ページの内容によってagouti.Pageのwindowサイズを変えて内容全体が写るようにキャプチャしたい場合、
RunScriptでbodyのサイズをとるjavascriptを実行 -> ページ毎にSize()で指定すれば綺麗にフルスクリーンキャプチャがとれます。
(省略してますが、ChromeDriver使ってます。)

type MyPage struct {
	*agouti.Page
}


/*
 * Take screenshot
 *
 */
func (mypage *MyPage) SS(filename string) {
	var width, height int
	mypage.RunScript("return document.body.scrollHeight;", nil, &height)
	mypage.RunScript("return document.body.scrollWidth;", nil, &width)
	mypage.Size(width, height)
	mypage.Screenshot(filename)
}

PHPのFacebookWebdriverでも確かexecuteScriptみたいなメソッドあったので、同じ感じでいけるかも。

Trap on findElement with cssSelector for Facebook WebDriver

github.com


In my case using Facebook WebDriver for Headless browser Testing, there is a trap on findElement with cssSelector.
Assume you want to find the element(s) by css selector that you specify like what their style attribute doesn't have "display:none",
for example you would use WebDriverBy::cssSelector like below.

Facebook WebDriverをテストで使っていて、その時にcssSelectorを使ってfindElementする際のトラップの話。
style属性にdisplay:noneを含まない要素を取得する際、WebDriverBy::cssSelectorでこんな感じに書ける。(部分一致的なやつです)

WebDriverBy::cssSelector("ul li:not([style*='display:none'])")


Although above aims to specify "li" which is visible element located in "ul", you cannot find them if they have half space between "display" and "none". In this case
you have to do insert a half space in the selector like this.

上記はulの中で可視化されているliを特定するためのものだが、displayとnoneの間に半角スペースがあると特定できない、、、
この場合、以下のようにcss selectorに半角スペースを入れる必要がある。

WebDriverBy::cssSelector("ul li:not([style*='display: none'])")

Besides I tried to use ":visible" selector for WebDriverBy::cssSelector(), but It didn't work with log "invalid selector ~".

また、:visibleセレクターも試したけどinvalid selector~~と言われてダメでした。。

今更ながらphpでword(docx)を読み込んでplain textを得る

word(docx)ファイルを出力する、とかのサンプルやライブラリは結構あるんですが、
今回はそもそもただ単に中身のプレーンテキストが欲しいという場合の話。

docx(確かword2007以降)は実はzipアーカイブなので、unzipとかで解凍できる。
その中にword/document.xmlというのがあって、それにテキストの情報が入っている。
これをパースしてパラグラフ -> テキストで回せば改行含めプレーンテキストとして取得できます。

今回はPHPのZipArchiveでパースしますが、別にphpでなくても全然大丈夫です。
PHP: ZipArchive - Manual

$NL = "\n";
$contents = "";
$file_path = "your.docx";
$zip = new \ZipArchive();

if ($zip->open($file_path) === true) {
	$xml = $zip->getFromName("word/document.xml");
	if ($xml) {
		$dom = new \DOMDocument();
		$dom->loadXML($xml);
		$paragraphs = $dom->getElementsByTagName("p");
		foreach ($paragraphs as $p) {
			$texts = $p->getElementsByTagName("t");
			foreach ($texts as $t) {
				$contents .= $t->nodeValue;
			}
                        // Add New Line after a paragraph.
			$contents .= $NL;
		}
	}
}
return $contents;
English sub


Although There are many samples and libraries on the web, this post is about that simple way you can get the plain text from docx file.
docx file is essentially zip archive that consists some rels, So we could unzip it.
there is a xml file named document.xml in the rels which has information of text.
We could fetch plain text by parsing xml and iterating its specific node which are like paragraph or text,,

CodeDeployでALB-EC2へのデプロイ時間を短縮する

AWS CodeDeployでALB配下のEC2にインプレースデプロイする際、本番にデプロイした際にいきなり遅くなった、、、


結論、本番のALBターゲットのヘルスチェック間隔がdevのそれよりも長かったのが原因でした。


以下のフォーラムによると、デプロイ時にトラフィックを止める際、ヘルスチェックの設定に依存するとのことなので、
デプロイする時だけヘルスチェックの間隔を短くするとかなり速くなります。

https://forums.aws.amazon.com/thread.jspa?threadID=254752


ちなみに、今回は間隔を30秒 -> 5秒(最小の閾値)に、さらに間隔に付随するタイムアウトなどの設定も合わせて変更したところ、
デプロイ時間が半分以下になりましたw

My Impressions About Using Vim In Earnest.

At first, I've been light vim user who use it only in environment of like CLI.
But I decided to use Vim for every kind of my development, taking memo or something like that.
The reason why I did above is Vim's mobility, reasonableness and extensibility...some reasons are there but the most important for me is just "Vim is Cool".

Before using vim for development in earnest, I thought that it's realy going to confuse me and hard to use it, But actually it wasn't no matter enough.
I could smoothly migrated usual editor from "atom" to "vim".
And now it's pretty comfortable for me.

I use 3rd party plugins to improve vim experiment and I really respect people who develop them.
These're my favorites like below.

'Shougo/unite.vim'
'Shougo/neomru.vim'
"tyru/caw.vim.git"
'scrooloose/nerdtree'
'jistr/vim-nerdtree-tabs'
'mattn/emmet-vim'
'grep.vim'
'Shougo/neocomplcache'
'Shougo/neosnippet'
'Shougo/neosnippet-snippets'
'tomtom/tcomment_vim'
'surround.vim'
'Townk/vim-autoclose'
'rhysd/accelerated-jk'
'thinca/vim-quickrun'
'ctrlpvim/ctrlp.vim'


In the middle of fixing though, This is my .vimrc.
GitHub - kentaro-a/evil-vim

My convenient snippet for showing information of EC2 instance.

If you want to make sure ec2 instance information from inner itself, you should check this post.
(Describes about in the case of EC2-AmazonLinux which has already installed `aws` command.)

For instance this is an alias named `me` that output instance's specified tag, type, private ip and public ip to the console.

/etc/profile.d/me.sh

alias me="me"
function me() {
	aws ec2 describe-instances \
		--region [region] \
		--instance-ids `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id` \
		--query 'Reservations[].Instances[].Tags[?Key==`[tag key]`].Value' \
		--output text;

	curl http://169.254.169.254/latest/meta-data/instance-type;
	echo;

	curl http://169.254.169.254/latest/meta-data/local-ipv4;
	echo;

	curl http://169.254.169.254/latest/meta-data/public-ipv4;
	echo
}


Type `me` on the console.

$ me