-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .commands import (SetResolution, SetPosition) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" This file aims to demonstrate how to write custom commands in OpenWPM | ||
Steps to have a custom command run as part of a CommandSequence | ||
1. Create a class that derives from BaseCommand | ||
2. Implement the execute method | ||
3. Append it to the CommandSequence | ||
4. Execute the CommandSequence | ||
""" | ||
import logging | ||
|
||
from tkinter import ttk | ||
|
||
from openwpm.commands.types import BaseCommand | ||
from openwpm.config import BrowserParams, ManagerParams | ||
from openwpm.socket_interface import ClientSocket | ||
|
||
from selenium.webdriver import Firefox | ||
|
||
|
||
def get_screen_resolution(driver): | ||
return driver.execute_script("return [screen.width, screen.height];") | ||
|
||
|
||
class SetResolution(BaseCommand): | ||
""" Sets the browser window resolution """ | ||
def __init__(self, width, height) -> None: | ||
self.logger = logging.getLogger("openwpm") | ||
self.width = width | ||
self.height = height | ||
|
||
def __repr__(self) -> str: | ||
return "SetResolution" | ||
|
||
def execute( | ||
self, | ||
driver: Firefox, | ||
browser_params: BrowserParams, | ||
manager_params: ManagerParams, | ||
extension_socket: ClientSocket, | ||
): | ||
|
||
self.logger.info(f"Setting window resolution to {self.width} x {self.height} ") | ||
driver.set_window_size(self.width, self.height) | ||
|
||
resolution = get_screen_resolution(driver) | ||
if resolution[0] <= self.width or resolution[1] <= self.height: | ||
self.logger.warn( | ||
f"Browser window resolution ({self.width} x {self.height}) exceeds " + | ||
f"screen resolution ({resolution[0]} x {resolution[1]})") | ||
|
||
|
||
|
||
class SetPosition(BaseCommand): | ||
""" Sets the browser window position """ | ||
def __init__(self, x, y) -> None: | ||
self.logger = logging.getLogger("openwpm") | ||
self.x = x | ||
self.y = y | ||
|
||
def __repr__(self) -> str: | ||
return "SetPosition" | ||
|
||
def execute( | ||
self, | ||
driver: Firefox, | ||
browser_params: BrowserParams, | ||
manager_params: ManagerParams, | ||
extension_socket: ClientSocket, | ||
): | ||
|
||
driver.set_window_position(self.x, self.y, windowHandle='current') |