install selenium

$ npm -g install selenium-webdriver
$ npm install selenium-webdriver

https://sites.google.com/a/chromium.org/chromedriver/downloads

sudo yum install -y google-chrome-unstable libOSMesa google-noto-cjk-fonts

var webdrive = require('selenium-webdriver');
var driver;
var By = webdriver.By;

driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();

driver.get('https://www.google.co.jp/').then(function(){
	driver.findElement(By.id('hplog')).click()
	.then(function(){
		console.log('clicked logo');
	});
});

[vagrant@localhost test]$ node chromeTest.js

/home/vagrant/local/app/test/node_modules/selenium-webdriver/index.js:25
const _http = require(‘./http’);
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/home/vagrant/local/app/test/chromeTest.js:1:78)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

Error: EACCES

[vagrant@localhost test]$ sudo chmod 777 /usr/local/lib;

[vagrant@localhost test]$ sudo npm install -g bower
npm http GET https://registry.npmjs.org/bower
npm http 200 https://registry.npmjs.org/bower
npm WARN deprecated bower@1.8.8: We don’t recommend using Bower for new projects. Please consider Yarn and Webpack or Parcel. You can read how to migrate legacy project here: https://bower.io/blog/2017/how-to-migrate-away-from-bower/
npm http GET https://registry.npmjs.org/bower/-/bower-1.8.8.tgz
npm http 200 https://registry.npmjs.org/bower/-/bower-1.8.8.tgz
/usr/bin/bower -> /usr/lib/node_modules/bower/bin/bower
npm WARN unmet dependency /usr/lib/node_modules/block-stream requires inherits@’~2.0.0′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream requires inherits@’~2.0.0′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-ignore requires inherits@’2′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-npm requires inherits@’2′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/glob requires inherits@’2′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/npmconf requires inherits@’~2.0.0′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/tar requires inherits@’2′ but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
bower@1.8.8 /usr/lib/node_modules/bower

Node.jp update

[vagrant@localhost test]$ node –version
v0.10.48
[vagrant@localhost test]$ npm –version
1.3.6
[vagrant@localhost test]$ n –latest
-bash: n: コマンドが見つかりません

There is a handy version control tool, n, which lets you check and update versions.

[vagrant@localhost test]$ npm install -g n
npm http GET https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n
npm ERR! Error: CERT_UNTRUSTED
npm ERR! at SecurePair. (tls.js:1430:32)
npm ERR! at SecurePair.emit (events.js:92:17)
npm ERR! at SecurePair.maybeInitFinished (tls.js:1029:10)
npm ERR! at CleartextStream.read [as _read] (tls.js:521:13)
npm ERR! at CleartextStream.Readable.read (_stream_readable.js:341:10)
npm ERR! at EncryptedStream.write [as _write] (tls.js:418:25)
npm ERR! at doWrite (_stream_writable.js:226:10)
npm ERR! at writeOrBuffer (_stream_writable.js:216:5)
npm ERR! at EncryptedStream.Writable.write (_stream_writable.js:183:11)
npm ERR! at write (_stream_readable.js:602:24)
npm ERR! If you need help, you may report this log at:
npm ERR!
npm ERR! or email it to:
npm ERR!

npm ERR! System Linux 2.6.32-754.3.5.el6.x86_64
npm ERR! command “node” “/usr/bin/npm” “install” “-g” “n”
npm ERR! cwd /home/vagrant/local/app/test
npm ERR! node -v v0.10.48
npm ERR! npm -v 1.3.6
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/vagrant/local/app/test/npm-debug.log
npm ERR! not ok code 0

The reason is that SSL key validation is performed on the https registry, so once this setting is set to false, npm install will succeed.

vagrant@localhost test]$ npm config set strict-ssl false

[vagrant@localhost test]$ npm install -g n
npm http GET https://registry.npmjs.org/n
npm http 200 https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n/-/n-4.1.0.tgz
npm http 200 https://registry.npmjs.org/n/-/n-4.1.0.tgz
npm ERR! Error: EACCES, mkdir ‘/usr/lib/node_modules/n’
npm ERR! { [Error: EACCES, mkdir ‘/usr/lib/node_modules/n’]
npm ERR! errno: 3,
npm ERR! code: ‘EACCES’,
npm ERR! path: ‘/usr/lib/node_modules/n’,
npm ERR! fstream_type: ‘Directory’,
npm ERR! fstream_path: ‘/usr/lib/node_modules/n’,
npm ERR! fstream_class: ‘DirWriter’,
npm ERR! fstream_stack:
npm ERR! [ ‘/usr/lib/node_modules/fstream/lib/dir-writer.js:36:23’,
npm ERR! ‘/usr/lib/node_modules/mkdirp/index.js:37:53’,
npm ERR! ‘Object.oncomplete (fs.js:108:15)’ ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Linux 2.6.32-754.3.5.el6.x86_64
npm ERR! command “node” “/usr/bin/npm” “install” “-g” “n”
npm ERR! cwd /home/vagrant/local/app/test
npm ERR! node -v v0.10.48
npm ERR! npm -v 1.3.6
npm ERR! path /usr/lib/node_modules/n
npm ERR! fstream_path /usr/lib/node_modules/n
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir ‘/usr/lib/node_modules/n’
npm ERR! fstream_stack /usr/lib/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/lib/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:108:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/vagrant/local/app/test/npm-debug.log
npm ERR! not ok code 0

This time with a different error, it says “Error: EACCES”.

About PhantomJS

WebKit based headless browser.
Webkit is a rendering engine mainly used in web browsers. PhantomJS uses a JavaScript engine called JavaScriptCore (which also uses safari) which is built-in Web-kit.

An environment in which a browser can be executed by commands programs.
Because the program can control the movement, PhantomJS is good at
– Automation test
– Scraping

In automated testing, Selenium + PhantomJS often come out as a set.

headless chrome 2

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub

$ sudo yum update

$ sudo yum install google-chrome-stable

rpm -Va –nofiles –nodigest

[vagrant@localhost ~]$ google-chrome –disable-setuid-sandbox –no-sandbox –headless –disable-gpu –dump-dom https://www.chromestatus.com/
-bash: google-chrome: コマンドが見つかりません

なんじゃこりゃーーーーーーーーーーーー

Headless chrom on CentOS

I would like to operate headless chrome on CentOS.

About Headless Chrome
It is a mode to operate without displaying the secreen, which will be available from Google Chrome 59. Useful for automated testing and web scraping.

Just because it’s Headless doesn’t change much from using regular Chrome. Control Chrome from Selenium through ChromeDriver. Pass ChromeOptions as an argument when creating a Chrome WebDriver, and specify the Chrome path and arguments to be executed in it.

Install required library.

$ sudo yum install -y libX11 GConf2 fontconfig
...
依存性関連をインストールしました:
  ConsoleKit.x86_64 0:0.4.1-6.el6      ConsoleKit-libs.x86_64 0:0.4.1-6.el6
  ORBit2.x86_64 0:2.14.17-7.el6        dbus.x86_64 1:1.2.24-9.el6
  eggdbus.x86_64 0:0.6-3.el6           libIDL.x86_64 0:0.8.13-2.1.el6
  polkit.x86_64 0:0.96-11.el6_10.1     sgml-common.noarch 0:0.6.3-33.el6

完了しました!

ほう。

[vagrant@localhost ~]$ cd /etc/yum.repos.d
[vagrant@localhost yum.repos.d]$ sudo touch google-chrome.repo
[vagrant@localhost yum.repos.d]$ ls
CentOS-Base.repo jenkins.repo remi-glpi92.repo
CentOS-Debuginfo.repo kibana.repo remi-glpi93.repo
CentOS-Media.repo logstash.repo remi-php54.repo
CentOS-Vault.repo mariadb.repo remi-php70.repo
CentOS-fasttrack.repo mysql-community-source.repo remi-php71.repo
elasticsearch.repo mysql-community.repo remi-php72.repo
epel-testing.repo nginx.repo remi-php73.repo
epel.repo nodesource-el.repo remi-safe.repo
google-chrome.repo remi-glpi91.repo remi.repo

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub

$ sudo yum update

Google Map API

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>GoogleMap</title>
	<script src="http://maps.google.com/maps/api/js?key={api code}&language=ja"></script>
	<style>
	html {
		height:100%;
	}
	body {
		height:100%;
	}
	#map {
		height:100%;
		width:100%;
	}
</style>
</head>

<body>
<div id="map"></div>
<script>
var MyLatLng = new google.maps.LatLng(35.6811673, 139.7670516);
var Options = {
	zoom: 15,
	center: MyLatLng,
	mapTypeId: 'roadmap'
};
var map = new google.maps.Map(document.getElementById('map'), Options);
</script>
</body>
</html>

UTF8 and Shift_JIS

Shift_JIS
Merit
-The number of bytes consumed is relatively small.
-Code that can be read on domestic mobile phones.

Demerit
It is garbled by how to use it.
Since encoded data often contains control characters, they may malfunction or be garbled in environments where they are not assumed.
The character type is about 9000.

UTF-8
The character range is wide and the characters of any contry can not be garbled.
It can be read by default in almost any PC environment.

PHPUnit

Using PHPUnit does not mean that you can test any source code. You should have designed your classes to be easy to test. Also, there are places where you can not put them into Unit test even if you have designed that way. If you can not put in the Unit test, you must divide it. Rather, how much code can I put in a unit test?

Unit test here refers to “method-only test” and “class-only test”.

object
setting value
external system(DB, API, file, command)
We separate those “dense dependencies” and “design the class”, “implement the class” and “make a test of the class”. “Dense dependence” refers to, for example the following.

The method using the object directly news the object(depends on the object)
Directly reference configuration files(file_get_contents, json_decode) in logic(depends on file system)
Where file(file_put_contents) directly in logic(depends on file system)
Peek directly into DB(mysqli, PDO) in logic(depends on network and database)
Direct API(file_get_contents, curl) in logic(depends on network and API)
Hit command(system) directly in logic(depends on OS)

namespace UnitTest\Sample;

class Hoge {
	protected $settings = null;
	protected $fileManager = null;
	protected $dbManager = null;
	protected $apiManger = null;
	protected $commandManager = null;

	public function __construct (
		$settings,
		$fileManager,
		$dbManager,
		$apiManager,
		$commandManager){

		$this->settings = $settings;
		$this->fileManager = $fileManager;
		$this->dbManager = $dbManager;
		$this->apiManager = $apiManager;
		$this->commandManager = $commandManager;
		}
}
namespace UnitTest\Sample;
class DatabaseSession
{
		private $connection = null;
		public function __construct($connection)
		{
				$this->connection = $connection;
		}

		public function save($tableName, $object)
		{
				if(!$tableName || !is_scalar($tableName)|| !$object){
						throw new \InvalidArgumentException('$tableName or $object is empty or invalid type');

				}
				if(is_object($object) && $object instanceof \stdClass){
						$ojbect = (array)$object;
				}

				if(!is_array($object)){
						throw new \InvalidArgumentException('$object must be array or \stdClass');
				}

				$values = array_values($object);
				$tableName = '`' . str_replace('`', '\\`', $tableName).'`';
				$columnBlock = implode(',', array_map(function($col) {return '`' . str_replace('`', '\\`',  $col) . '`';}, array_keys($object)));
		        $valueBlock  = implode(',', array_map(function($val) {return '?'; }, $values));
		        $valueMarker = implode('', array_map(function($val) {return strval(intval($val)) === strval($val) ? 'i' : 's'; }, $values));
		        $stmt = $this->connection->prepare("REPLACE INTO $tableName ($columnBlock) VALUES ($valueBlock);");
		        array_unshift($values, $valueMarker);
		        $pointer = array();
		        foreach($values as $key => $val){
		        	$pointer[$key] = &$val;
		        }
		        call_user_func_array(array($stmt, 'bind_param'), $pointer);
		        $stmt->execute();
		        $stmt->close();
		}	
}

Diffrence between external and internal design

External Design
External design is also called basic design or external design, and generally up to external design is included in the upstream process.
The basic design of the system is performed based on the functions, performance, and constraints determined in the requirements definition. Basically, the specifications for the user are designed, such as determining the specifications of the interface part that can be seen by the user such as the operation screen, operation method, data output etc., designing security and operation rules, schedule and expenses of system development, etc. It is external design to do.

Internal Design
The internal design is based on the external design to design details that are difficult for the user to see, such as the operation and functions inside the system and physical data. Although this is a process between external design and detailed design to performed afterward, it is sometimes treated as the same process as detailed design because it is specialized for internal system. Divide program functions into single units and design physical data and input / output to be used there. The role of internal design is to make the result of external design easy to program.

ummmmm,
I would like to accumulate more know-how and knowledge on internal design.