Chromium Code Reviews| Index: tools/privacy_whitepaper/environment.py |
| diff --git a/tools/privacy_whitepaper/environment.py b/tools/privacy_whitepaper/environment.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d89dc195a5d8575807bb75c0111c4a6c52077eb8 |
| --- /dev/null |
| +++ b/tools/privacy_whitepaper/environment.py |
| @@ -0,0 +1,139 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""The screenshot generation Environment class. |
| + |
| +It holds the ScreenshotGenerator instances and executes them. |
| +""" |
| + |
| +import glob |
| +import json |
| +import os |
| +import pyautogui |
| +import shutil |
| +import time |
| + |
| +from selenium import webdriver |
| +from selenium.webdriver.chrome.options import Options |
| + |
| +class Environment: |
| + """Sets up the Environment. """ |
| + |
| + def __init__(self, chrome_path, chromedriver_path, profile_path, output_path, |
| + safebrowsing_path, language, is_rtl_language): |
| + """Creates a new Environment, starts Chromedriver. |
| + |
| + Args: |
| + chrome_path: The chrome binary file. |
| + chromedriver_path: The chromedriver binary file. |
| + profile_path: The chrome testing profile folder. |
| + output_path: Path into which screenshots are written. |
| + safebrowsing_path: Path to a Chrome profile that contains a SafeBrowsing |
| + database. |
| + language: Language used by Chrome. |
| + is_rtl_language: Whether the language is a right-to-left language. |
| + """ |
| + self.chrome_path = chrome_path |
| + self.chromedriver_path = chromedriver_path |
| + self.profile_path = profile_path |
| + self.output_path = output_path |
| + self.safebrowsing_path = safebrowsing_path |
| + self.language = language |
| + self.is_rtl_language = is_rtl_language |
| + self.screenshot_generators = [] |
| + self.downloads_path = os.path.join(self.profile_path, "Downloads") |
| + self.downloads_path = os.path.join(self.profile_path, "Downloads") |
| + self.screenshot_generators = [] |
| + # These are initialized in Start(): |
| + self.driver = None |
| + self.website_window = None |
| + |
| + def Start(self): |
| + """Starts Chromedriver.""" |
| + # Cleaning the chrome testing profile folder. |
| + if os.path.exists(self.profile_path): |
| + shutil.rmtree(self.profile_path) |
| + os.mkdir(self.profile_path) |
| + os.mkdir(os.path.join(self.profile_path, "Default")) |
| + |
| + # Copy Safe Browsing database from an existing profile for re-use. |
| + if self.safebrowsing_path: |
| + for f in glob.glob(os.path.join(self.safebrowsing_path, |
| + "Safe Browsing*")): |
| + shutil.copy(f, self.profile_path) |
| + |
| + # Initialize Default/Preferences. |
| + preferences = json.dumps({ |
| + "browser": {"custom_chrome_frame": True}, |
| + "download": {"default_directory": self.downloads_path}, |
| + "safebrowsing": {"enabled": True}, |
| + "translate": {"enabled": True}, |
| + "profile": {"default_content_setting_values": {"geolocation": 1}}, |
| + "search": {"suggest_enabled": True}, |
| + }) |
| + with open(os.path.join(self.profile_path, "Default", "Preferences"), |
| + "w") as f: |
| + f.write(preferences) |
| + |
| + # Initialize Local State. |
| + preferences = json.dumps({ |
| + "user_experience_metrics": {"reporting_enabled": True}, |
| + }) |
| + with open(os.path.join(self.profile_path, "Local State"), "w") as f: |
| + f.write(preferences) |
| + |
| + # Set OS environment so that Chrome starts int he right language. |
| + os.environ['LANGUAGE'] = self.language |
| + |
| + options = Options() |
| + options.binary_location = self.chrome_path |
| + options.add_argument("user-data-dir=%s" % self.profile_path) |
| + |
| + self.driver = webdriver.Chrome(self.chromedriver_path, 0, options) |
| + |
| + # Always run at fixed window size to simplify screenshot creation. |
| + self.driver.set_window_position(0, 0) |
| + self.driver.set_window_size(800, 800) |
| + |
| + self.website_window = self.driver.current_window_handle |
| + |
| + def HideChromeDriverIcon(self): |
| + if self.is_rtl_language: |
| + pyautogui.click(x = 57, y = 62) |
| + else: |
| + pyautogui.click(x = 746, y = 62) |
|
melandory
2016/05/30 13:51:49
nit: pyautogui.click( x = 57 if self.is_rtl_langua
|
| + time.sleep(1) |
| + if self.is_rtl_language: |
| + pyautogui.click(x = 135, y = 176) |
| + else: |
| + pyautogui.click(x = 817, y = 176) |
| + pyautogui.moveTo(x = 1000, y = 1000) |
| + |
| + |
| + def AddScreenshotGenerator(self, screenshot_generator): |
| + """Adds a screenshot_generator to the execution Environment. |
| + |
| + Args: |
| + screenshot_generator: The ScreenshotGenerator instance to be added. |
| + """ |
| + |
| + screenshot_generator.environment = self |
| + self.screenshot_generators.append(screenshot_generator) |
| + |
| + |
| + def GenerateScreenshots(self): |
| + """Generates the screenshots.""" |
| + |
| + for generator in self.screenshot_generators: |
| + self.Start() |
| + self.HideChromeDriverIcon() |
| + generator.driver = self.driver |
| + generator.Execute() |
| + self.Quit() |
| + |
| + |
| + def Quit(self): |
| + """Shuts down the driver.""" |
| + |
| + self.driver.quit() |