@rich-automation/lotto 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,14 +45,14 @@ class PuppeteerController {
45
45
  const pages = yield browser.pages();
46
46
  if (pages.length === 0) {
47
47
  const page = yield browser.newPage();
48
- return new puppeteer_page_1.PuppeteerPage(page);
48
+ return new puppeteer_page_1.PuppeteerPage(page, this.logger);
49
49
  }
50
50
  else {
51
51
  const isWithinRange = Math.max(0, Math.min(pageIndex, pages.length - 1)) === pageIndex;
52
52
  const page = pages.at(isWithinRange ? pageIndex : -1);
53
53
  if (!page)
54
54
  throw new Error('Page is not found');
55
- return new puppeteer_page_1.PuppeteerPage(page);
55
+ return new puppeteer_page_1.PuppeteerPage(page, this.logger);
56
56
  }
57
57
  });
58
58
  this.close = () => __awaiter(this, void 0, void 0, function* () {
@@ -13,7 +13,9 @@ exports.PuppeteerPage = void 0;
13
13
  const deferred_1 = require("../../utils/deferred");
14
14
  const lazyRun_1 = require("../../utils/lazyRun");
15
15
  class PuppeteerPage {
16
- constructor(page) {
16
+ constructor(page, logger) {
17
+ this.page = page;
18
+ this.logger = logger;
17
19
  this.page = page;
18
20
  }
19
21
  url() {
@@ -31,9 +33,17 @@ class PuppeteerPage {
31
33
  yield this.page.type(selector, value.toString());
32
34
  });
33
35
  }
34
- click(selector) {
36
+ click(selector, useWaitForSelector = false) {
37
+ var _a;
35
38
  return __awaiter(this, void 0, void 0, function* () {
36
- yield this.page.click(selector);
39
+ if (useWaitForSelector) {
40
+ const handle = yield this.page.waitForSelector(selector, { timeout: 15000 });
41
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('[PuppeteerPage]', '[click]', 'handle', handle);
42
+ handle === null || handle === void 0 ? void 0 : handle.click();
43
+ }
44
+ else {
45
+ yield this.page.click(selector);
46
+ }
37
47
  });
38
48
  }
39
49
  select(selector, value) {
@@ -110,15 +110,24 @@ class LottoService {
110
110
  yield page.click(selectors_1.SELECTORS.PURCHASE_TYPE_RANDOM_BTN);
111
111
  // set and confirm amount
112
112
  const amountString = String(Math.max(1, Math.min(5, amount)));
113
- this.logger.debug('[purchase]', `select purchase amount${amountString} -> amount confirm`);
113
+ this.logger.debug('[purchase]', `select purchase amount(${amountString}) -> amount confirm`);
114
114
  yield page.select(selectors_1.SELECTORS.PURCHASE_AMOUNT_SELECT, amountString);
115
115
  yield page.click(selectors_1.SELECTORS.PURCHASE_AMOUNT_CONFIRM_BTN);
116
116
  // click purchase button
117
117
  this.logger.debug('[purchase]', 'click purchase button');
118
118
  yield page.click(selectors_1.SELECTORS.PURCHASE_BTN);
119
- yield page.wait(500);
120
119
  this.logger.debug('[purchase]', 'click purchase confirm button');
121
- yield page.click(selectors_1.SELECTORS.PURCHASE_CONFIRM_BTN);
120
+ try {
121
+ yield page.click(selectors_1.SELECTORS.PURCHASE_CONFIRM_BTN, true);
122
+ }
123
+ catch (e) {
124
+ this.logger.debug('[purchase]', 'purchase confirm failure', e);
125
+ this.logger.debug('[purchase]', 'print node');
126
+ yield page.querySelectorAll(selectors_1.SELECTORS.PURCHASE_CONFIRM_BTN, elems => {
127
+ this.logger.debug('[purchase]', elems);
128
+ });
129
+ throw e;
130
+ }
122
131
  yield page.wait(1000);
123
132
  // game result
124
133
  this.logger.debug('[purchase]', 'print result');
@@ -40,14 +40,14 @@ export class PuppeteerController {
40
40
  const pages = yield browser.pages();
41
41
  if (pages.length === 0) {
42
42
  const page = yield browser.newPage();
43
- return new PuppeteerPage(page);
43
+ return new PuppeteerPage(page, this.logger);
44
44
  }
45
45
  else {
46
46
  const isWithinRange = Math.max(0, Math.min(pageIndex, pages.length - 1)) === pageIndex;
47
47
  const page = pages.at(isWithinRange ? pageIndex : -1);
48
48
  if (!page)
49
49
  throw new Error('Page is not found');
50
- return new PuppeteerPage(page);
50
+ return new PuppeteerPage(page, this.logger);
51
51
  }
52
52
  });
53
53
  this.close = () => __awaiter(this, void 0, void 0, function* () {
@@ -11,7 +11,9 @@ import { Page } from 'puppeteer';
11
11
  import { deferred } from '../../utils/deferred';
12
12
  import { lazyRun } from '../../utils/lazyRun';
13
13
  export class PuppeteerPage {
14
- constructor(page) {
14
+ constructor(page, logger) {
15
+ this.page = page;
16
+ this.logger = logger;
15
17
  this.page = page;
16
18
  }
17
19
  url() {
@@ -29,9 +31,17 @@ export class PuppeteerPage {
29
31
  yield this.page.type(selector, value.toString());
30
32
  });
31
33
  }
32
- click(selector) {
34
+ click(selector, useWaitForSelector = false) {
35
+ var _a;
33
36
  return __awaiter(this, void 0, void 0, function* () {
34
- yield this.page.click(selector);
37
+ if (useWaitForSelector) {
38
+ const handle = yield this.page.waitForSelector(selector, { timeout: 15000 });
39
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('[PuppeteerPage]', '[click]', 'handle', handle);
40
+ handle === null || handle === void 0 ? void 0 : handle.click();
41
+ }
42
+ else {
43
+ yield this.page.click(selector);
44
+ }
35
45
  });
36
46
  }
37
47
  select(selector, value) {
@@ -104,15 +104,24 @@ export class LottoService {
104
104
  yield page.click(SELECTORS.PURCHASE_TYPE_RANDOM_BTN);
105
105
  // set and confirm amount
106
106
  const amountString = String(Math.max(1, Math.min(5, amount)));
107
- this.logger.debug('[purchase]', `select purchase amount${amountString} -> amount confirm`);
107
+ this.logger.debug('[purchase]', `select purchase amount(${amountString}) -> amount confirm`);
108
108
  yield page.select(SELECTORS.PURCHASE_AMOUNT_SELECT, amountString);
109
109
  yield page.click(SELECTORS.PURCHASE_AMOUNT_CONFIRM_BTN);
110
110
  // click purchase button
111
111
  this.logger.debug('[purchase]', 'click purchase button');
112
112
  yield page.click(SELECTORS.PURCHASE_BTN);
113
- yield page.wait(500);
114
113
  this.logger.debug('[purchase]', 'click purchase confirm button');
115
- yield page.click(SELECTORS.PURCHASE_CONFIRM_BTN);
114
+ try {
115
+ yield page.click(SELECTORS.PURCHASE_CONFIRM_BTN, true);
116
+ }
117
+ catch (e) {
118
+ this.logger.debug('[purchase]', 'purchase confirm failure', e);
119
+ this.logger.debug('[purchase]', 'print node');
120
+ yield page.querySelectorAll(SELECTORS.PURCHASE_CONFIRM_BTN, elems => {
121
+ this.logger.debug('[purchase]', elems);
122
+ });
123
+ throw e;
124
+ }
116
125
  yield page.wait(1000);
117
126
  // game result
118
127
  this.logger.debug('[purchase]', 'print result');
@@ -1,12 +1,14 @@
1
1
  import type { BrowserPageEvents, BrowserPageInterface, FakeDOMElement, StringifiedCookies } from '../../types';
2
2
  import { Page } from 'puppeteer';
3
+ import type { LoggerInterface } from '../../logger';
3
4
  export declare class PuppeteerPage implements BrowserPageInterface {
4
5
  page: Page;
5
- constructor(page: Page);
6
+ logger?: LoggerInterface | undefined;
7
+ constructor(page: Page, logger?: LoggerInterface | undefined);
6
8
  url(): Promise<string>;
7
9
  goto(url: string): Promise<void>;
8
10
  fill(selector: string, value: string | number): Promise<void>;
9
- click(selector: string): Promise<void>;
11
+ click(selector: string, useWaitForSelector?: boolean): Promise<void>;
10
12
  select(selector: string, value: string): Promise<void>;
11
13
  querySelectorAll<T>(selector: string, callback: (elems: FakeDOMElement[]) => T): Promise<T>;
12
14
  getCookies(): Promise<string>;
@@ -30,7 +30,7 @@ export interface BrowserPageInterface {
30
30
  url(): Promise<string>;
31
31
  goto(url: string): Promise<void>;
32
32
  fill(selector: string, value: string | number): Promise<void>;
33
- click(selector: string): Promise<void>;
33
+ click(selector: string, useWaitForSelector?: boolean): Promise<void>;
34
34
  select(selector: string, value: string): Promise<void>;
35
35
  querySelectorAll<T>(selector: string, callback: (elems: FakeDOMElement[]) => T): Promise<T>;
36
36
  wait(time: number): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rich-automation/lotto",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Lotto module",
5
5
  "publishConfig": {
6
6
  "access": "public",