| 123 | |
| 124 | @beartype |
| 125 | def setup(self, config_file: Path | None = None) -> None: |
| 126 | self.context_manager = sync_playwright() |
| 127 | self.playwright = self.context_manager.__enter__() |
| 128 | self.browser = self.playwright.chromium.launch( |
| 129 | headless=self.headless, slow_mo=self.slow_mo, |
| 130 | timeout=300000, |
| 131 | ) |
| 132 | |
| 133 | if config_file: |
| 134 | with open(config_file, "r") as f: |
| 135 | instance_config = json.load(f) |
| 136 | else: |
| 137 | instance_config = {} |
| 138 | |
| 139 | storage_state = instance_config.get("storage_state", None) |
| 140 | start_url = instance_config.get("start_url", None) |
| 141 | geolocation = instance_config.get("geolocation", None) |
| 142 | |
| 143 | self.context = self.browser.new_context( |
| 144 | viewport=self.viewport_size, |
| 145 | storage_state=storage_state, |
| 146 | geolocation=geolocation, |
| 147 | device_scale_factor=1, |
| 148 | ) |
| 149 | if self.save_trace_enabled: |
| 150 | self.context.tracing.start(screenshots=True, snapshots=True) |
| 151 | if start_url: |
| 152 | start_urls = start_url.split(" |AND| ") |
| 153 | for url in start_urls: |
| 154 | page = self.context.new_page() |
| 155 | page.on("request", lambda request: print(">>", request.method, request.url)) |
| 156 | page.on("response", lambda response: print("<<", response.status, response.url)) |
| 157 | client = page.context.new_cdp_session( |
| 158 | page |
| 159 | ) # talk to chrome devtools |
| 160 | if self.text_observation_type == "accessibility_tree": |
| 161 | client.send("Accessibility.enable") |
| 162 | page.client = client # type: ignore # TODO[shuyanzh], fix this hackey client |
| 163 | page.goto(url, timeout=300000) |
| 164 | print('*** GOTO DONE ***') |
| 165 | # set the first page as the current page |
| 166 | self.page = self.context.pages[0] |
| 167 | self.page.bring_to_front() |
| 168 | else: |
| 169 | self.page = self.context.new_page() |
| 170 | client = self.page.context.new_cdp_session(self.page) |
| 171 | if self.text_observation_type == "accessibility_tree": |
| 172 | client.send("Accessibility.enable") |
| 173 | self.page.client = client # type: ignore |
| 174 | |
| 175 | @beartype |
| 176 | def get_page_client(self, page: Page) -> CDPSession: |