From f0ae31747bb9e42dc74870eaa0255e66704f2b01 Mon Sep 17 00:00:00 2001 From: teerawatrksp Date: Tue, 6 Aug 2024 00:29:42 +0700 Subject: [PATCH] override webdriver options on webdriverUser with example --- examples/webdriver_ex.py | 20 +++++++++++++++ locust_plugins/users/webdriver.py | 41 ++++++++++++++++--------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/examples/webdriver_ex.py b/examples/webdriver_ex.py index bfc9e7f2..f487f7b3 100644 --- a/examples/webdriver_ex.py +++ b/examples/webdriver_ex.py @@ -18,6 +18,26 @@ class MyUser(WebdriverUser): wait_time = constant(2) + # webdriver client options can be customized by overriding the option_args + option_args = [ + "--disable-translate", + "--disable-extensions", + "--disable-background-networking", + "--safebrowsing-disable-auto-update", + "--disable-sync", + "--metrics-recording-only", + "--disable-default-apps", + "--no-first-run", + "--disable-setuid-sandbox", + "--hide-scrollbars", + "--no-sandbox", + "--no-zygote", + "--autoplay-policy=no-user-gesture-required", + "--disable-notifications", + "--disable-logging", + "--disable-permissions-api", + "--ignore-certificate-errors", + ] if __name__ == "__main__": # wait a bit at the end to make debugging easier diff --git a/locust_plugins/users/webdriver.py b/locust_plugins/users/webdriver.py index cf1c2221..a50fd145 100644 --- a/locust_plugins/users/webdriver.py +++ b/locust_plugins/users/webdriver.py @@ -29,25 +29,7 @@ def __init__(self, user: User): options = Options() if self.user.headless: options.add_argument("--headless") - for arg in [ - "--disable-translate", - "--disable-extensions", - "--disable-background-networking", - "--safebrowsing-disable-auto-update", - "--disable-sync", - "--metrics-recording-only", - "--disable-default-apps", - "--no-first-run", - "--disable-setuid-sandbox", - "--hide-scrollbars", - "--no-sandbox", - "--no-zygote", - "--autoplay-policy=no-user-gesture-required", - "--disable-notifications", - "--disable-logging", - "--disable-permissions-api", - "--ignore-certificate-errors", - ]: + for arg in self.user.option_args: options.add_argument(arg) # hide infobar about automation options.add_experimental_option("excludeSwitches", ["enable-automation"]) @@ -251,8 +233,27 @@ def failure(self, exc): class WebdriverUser(User): abstract = True _first_instance = True - headless = False # overwrite this as needed command_executor = "http://127.0.0.1:4444" + headless = False # overwrite this as needed + option_args = [ # overwrite this as needed + "--disable-translate", + "--disable-extensions", + "--disable-background-networking", + "--safebrowsing-disable-auto-update", + "--disable-sync", + "--metrics-recording-only", + "--disable-default-apps", + "--no-first-run", + "--disable-setuid-sandbox", + "--hide-scrollbars", + "--no-sandbox", + "--no-zygote", + "--autoplay-policy=no-user-gesture-required", + "--disable-notifications", + "--disable-logging", + "--disable-permissions-api", + "--ignore-certificate-errors", + ] def __init__(self, parent): super().__init__(parent)