OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 """The screenshot generation Environment class. | |
6 | |
7 It holds the ScreenshotGenerator instances and executes them. | |
8 """ | |
9 | |
10 import glob | |
11 import json | |
12 import os | |
13 import pyautogui | |
14 import shutil | |
15 import time | |
16 | |
17 from selenium import webdriver | |
18 from selenium.webdriver.chrome.options import Options | |
19 | |
20 class Environment: | |
21 """Sets up the Environment. """ | |
22 | |
23 def __init__(self, chrome_path, chromedriver_path, profile_path, output_path, | |
24 safebrowsing_path, language, is_rtl_language): | |
25 """Creates a new Environment, starts Chromedriver. | |
26 | |
27 Args: | |
28 chrome_path: The chrome binary file. | |
29 chromedriver_path: The chromedriver binary file. | |
30 profile_path: The chrome testing profile folder. | |
31 output_path: Path into which screenshots are written. | |
32 safebrowsing_path: Path to a Chrome profile that contains a SafeBrowsing | |
33 database. | |
34 language: Language used by Chrome. | |
35 is_rtl_language: Whether the language is a right-to-left language. | |
36 """ | |
37 self.chrome_path = chrome_path | |
38 self.chromedriver_path = chromedriver_path | |
39 self.profile_path = profile_path | |
40 self.output_path = output_path | |
41 self.safebrowsing_path = safebrowsing_path | |
42 self.language = language | |
43 self.is_rtl_language = is_rtl_language | |
44 self.screenshot_generators = [] | |
45 self.downloads_path = os.path.join(self.profile_path, "Downloads") | |
46 self.downloads_path = os.path.join(self.profile_path, "Downloads") | |
47 self.screenshot_generators = [] | |
48 # These are initialized in Start(): | |
49 self.driver = None | |
50 self.website_window = None | |
51 | |
52 def Start(self): | |
53 """Starts Chromedriver.""" | |
54 # Cleaning the chrome testing profile folder. | |
55 if os.path.exists(self.profile_path): | |
56 shutil.rmtree(self.profile_path) | |
57 os.mkdir(self.profile_path) | |
58 os.mkdir(os.path.join(self.profile_path, "Default")) | |
59 | |
60 # Copy Safe Browsing database from an existing profile for re-use. | |
61 if self.safebrowsing_path: | |
62 for f in glob.glob(os.path.join(self.safebrowsing_path, | |
63 "Safe Browsing*")): | |
64 shutil.copy(f, self.profile_path) | |
65 | |
66 # Initialize Default/Preferences. | |
67 preferences = json.dumps({ | |
68 "browser": {"custom_chrome_frame": True}, | |
69 "download": {"default_directory": self.downloads_path}, | |
70 "safebrowsing": {"enabled": True}, | |
71 "translate": {"enabled": True}, | |
72 "profile": {"default_content_setting_values": {"geolocation": 1}}, | |
73 "search": {"suggest_enabled": True}, | |
74 }) | |
75 with open(os.path.join(self.profile_path, "Default", "Preferences"), | |
76 "w") as f: | |
77 f.write(preferences) | |
78 | |
79 # Initialize Local State. | |
80 preferences = json.dumps({ | |
81 "user_experience_metrics": {"reporting_enabled": True}, | |
82 }) | |
83 with open(os.path.join(self.profile_path, "Local State"), "w") as f: | |
84 f.write(preferences) | |
85 | |
86 # Set OS environment so that Chrome starts int he right language. | |
87 os.environ['LANGUAGE'] = self.language | |
88 | |
89 options = Options() | |
90 options.binary_location = self.chrome_path | |
91 options.add_argument("user-data-dir=%s" % self.profile_path) | |
92 | |
93 self.driver = webdriver.Chrome(self.chromedriver_path, 0, options) | |
94 | |
95 # Always run at fixed window size to simplify screenshot creation. | |
96 self.driver.set_window_position(0, 0) | |
97 self.driver.set_window_size(800, 800) | |
98 | |
99 self.website_window = self.driver.current_window_handle | |
100 | |
101 def HideChromeDriverIcon(self): | |
102 if self.is_rtl_language: | |
103 pyautogui.click(x = 57, y = 62) | |
104 else: | |
105 pyautogui.click(x = 746, y = 62) | |
melandory
2016/05/30 13:51:49
nit: pyautogui.click( x = 57 if self.is_rtl_langua
| |
106 time.sleep(1) | |
107 if self.is_rtl_language: | |
108 pyautogui.click(x = 135, y = 176) | |
109 else: | |
110 pyautogui.click(x = 817, y = 176) | |
111 pyautogui.moveTo(x = 1000, y = 1000) | |
112 | |
113 | |
114 def AddScreenshotGenerator(self, screenshot_generator): | |
115 """Adds a screenshot_generator to the execution Environment. | |
116 | |
117 Args: | |
118 screenshot_generator: The ScreenshotGenerator instance to be added. | |
119 """ | |
120 | |
121 screenshot_generator.environment = self | |
122 self.screenshot_generators.append(screenshot_generator) | |
123 | |
124 | |
125 def GenerateScreenshots(self): | |
126 """Generates the screenshots.""" | |
127 | |
128 for generator in self.screenshot_generators: | |
129 self.Start() | |
130 self.HideChromeDriverIcon() | |
131 generator.driver = self.driver | |
132 generator.Execute() | |
133 self.Quit() | |
134 | |
135 | |
136 def Quit(self): | |
137 """Shuts down the driver.""" | |
138 | |
139 self.driver.quit() | |
OLD | NEW |