0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

node puppetterでwait系の処理を結構書いたので、メモ

Posted at
const Util = require('./util');

const wait = {
	/**
	 * 指定時間処理を止める
	 */
	sleep : async (time)=>{
		if(time === undefined){
			time = process.env.puppet_wait_ms;
		}
		return new Promise(resolve => {
			setTimeout(() => {
				resolve();	
			}, time);
		});
	},
	/**
	 * 指定のエレメントにselecter要素が表示されるまで待つ
	 * @param  ElementHandle  elem puppeteerのElementHandle
	 * @param  string  selecter
	 * @return Promise  listで返す
	 */
	forChildSelecter:async (elem,selecter) =>{
		let isShow = false;
		let targElms = {};
		for(let i = 1 ; i <= process.env.puppet_wait_count; i++){
			targElms = await elem.$$(selecter);
			if(targElms.length > 0){
				isShow = true;
				break;
			}
			await wait.sleep();
		}
		if(isShow === false){
			throw new Error(process.env.puppet_wait_count+'回待ったけどダメ!');
		}else{
			return targElms;
		}
	},
	/**
	 * 指定のエレメントの中のselecter要素が消えるまで待つ
	 * @param  ElementHandle  elem puppeteerのElementHandle
	 * @param  string  selecter
	 */
	forChildSelecterHide: async(elm,selecter)=>{
		let isShow = true;
		let targElms = {};
		for(let i = 1 ; i <= process.env.puppet_wait_count; i++){
			targElms = await elm.$$(selecter);
			if(targElms.length == 0){
				isShow = false;
				break;
			}
			await wait.sleep();
		}
		if(isShow === true){
			throw new Error(process.env.puppet_wait_count+'回待ったけどダメ!');
		}else{
			return true;
		}
	},
	/**
	 * 指定のエレメントの中のselecter要素が作られて、消えるまで待つ
	 * @param  ElementHandle  elem puppeteerのElementHandle
	 * @param  string  selecter
	 */
	forChildSelecterShowAndHide:async (elm,selecter)=>{
		await wait.forChildSelecter(elm,selecter);
		await wait.forChildSelecterHide(elm,selecter);
		return true;
	},
	/**
	* ElementHandleから指定のClassNameが無くなるのを待つ。
	* 再帰確認時間はenv.puppet_waitでms
	*/
	removeClass : async (elem,targClassName)=>{
		let isRemove = false;
		for(let i = 1 ; i <= process.env.puppet_wait_count; i++){
			let handle = await elem.getProperty('className');
			let classNames = await handle.jsonValue();
			if(classNames.indexOf(targClassName) == -1){
				isRemove = true;
				break;
			}
			await wait.sleep();
		}
		if(isRemove === false){
			throw new Error(process.env.puppet_wait_count+'回待ったけどダメ!');
		}else{
			return true;
		}
	}
};
module.exports = wait;

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?