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