@rich-automation/lotto 0.1.2 → 0.1.4

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,8 +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
17
  this.page = page;
18
+ this.logger = logger;
18
19
  }
19
20
  url() {
20
21
  return __awaiter(this, void 0, void 0, function* () {
@@ -31,11 +32,12 @@ class PuppeteerPage {
31
32
  yield this.page.type(selector, value.toString());
32
33
  });
33
34
  }
34
- click(selector, useWaitForSelector = false) {
35
+ click(selector, domDirect = false) {
35
36
  return __awaiter(this, void 0, void 0, function* () {
36
- if (useWaitForSelector) {
37
- const handle = yield this.page.waitForSelector(selector, { timeout: 15000 });
38
- handle === null || handle === void 0 ? void 0 : handle.click();
37
+ if (domDirect) {
38
+ // @ts-ignore
39
+ yield this.page.evaluate(s => document.querySelector(s).click(), selector);
40
+ yield this.wait(250);
39
41
  }
40
42
  else {
41
43
  yield this.page.click(selector);
@@ -110,24 +110,14 @@ 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
- yield page.click(selectors_1.SELECTORS.PURCHASE_BTN);
118
+ yield page.click(selectors_1.SELECTORS.PURCHASE_BTN, true);
119
119
  this.logger.debug('[purchase]', 'click purchase confirm button');
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
- }
120
+ yield page.click(selectors_1.SELECTORS.PURCHASE_CONFIRM_BTN, true);
131
121
  yield page.wait(1000);
132
122
  // game result
133
123
  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,8 +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
15
  this.page = page;
16
+ this.logger = logger;
16
17
  }
17
18
  url() {
18
19
  return __awaiter(this, void 0, void 0, function* () {
@@ -29,11 +30,12 @@ export class PuppeteerPage {
29
30
  yield this.page.type(selector, value.toString());
30
31
  });
31
32
  }
32
- click(selector, useWaitForSelector = false) {
33
+ click(selector, domDirect = false) {
33
34
  return __awaiter(this, void 0, void 0, function* () {
34
- if (useWaitForSelector) {
35
- const handle = yield this.page.waitForSelector(selector, { timeout: 15000 });
36
- handle === null || handle === void 0 ? void 0 : handle.click();
35
+ if (domDirect) {
36
+ // @ts-ignore
37
+ yield this.page.evaluate(s => document.querySelector(s).click(), selector);
38
+ yield this.wait(250);
37
39
  }
38
40
  else {
39
41
  yield this.page.click(selector);
@@ -104,24 +104,14 @@ 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
- yield page.click(SELECTORS.PURCHASE_BTN);
112
+ yield page.click(SELECTORS.PURCHASE_BTN, true);
113
113
  this.logger.debug('[purchase]', 'click purchase confirm button');
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
- }
114
+ yield page.click(SELECTORS.PURCHASE_CONFIRM_BTN, true);
125
115
  yield page.wait(1000);
126
116
  // game result
127
117
  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;
7
+ constructor(page: Page, logger?: LoggerInterface);
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, useWaitForSelector?: boolean): Promise<void>;
11
+ click(selector: string, domDirect?: 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, useWaitForSelector?: boolean): Promise<void>;
33
+ click(selector: string, domDirect?: 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.2",
3
+ "version": "0.1.4",
4
4
  "description": "Lotto module",
5
5
  "publishConfig": {
6
6
  "access": "public",