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;
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme