| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import types | 5 import types |
| 6 | 6 |
| 7 import selenium.common.exceptions | 7 import selenium.common.exceptions |
| 8 from selenium.webdriver.common.action_chains import ActionChains | 8 from selenium.webdriver.common.action_chains import ActionChains |
| 9 | 9 |
| 10 | 10 |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 321 |
| 322 def __init__(self, driver, content_type): | 322 def __init__(self, driver, content_type): |
| 323 content_url = 'chrome://settings/contentExceptions#%s' % content_type | 323 content_url = 'chrome://settings/contentExceptions#%s' % content_type |
| 324 assert content_url == driver.current_url | 324 assert content_url == driver.current_url |
| 325 self._list_elem = driver.find_element_by_xpath( | 325 self._list_elem = driver.find_element_by_xpath( |
| 326 './/*[@id="content-settings-exceptions-area"]' \ | 326 './/*[@id="content-settings-exceptions-area"]' \ |
| 327 '//*[@contenttype="%s"]//list[@role="listbox"]' \ | 327 '//*[@contenttype="%s"]//list[@role="listbox"]' \ |
| 328 '[@class="settings-list"]' % content_type) | 328 '[@class="settings-list"]' % content_type) |
| 329 self._driver = driver | 329 self._driver = driver |
| 330 self._content_type = content_type | 330 self._content_type = content_type |
| 331 try: |
| 332 self._incognito_list_elem = driver.find_element_by_xpath( |
| 333 './/*[@id="content-settings-exceptions-area"]' \ |
| 334 '//*[@contenttype="%s"]//div[not(@hidden)]i' \ |
| 335 '//list[@mode="otr"][@role="listbox"]' \ |
| 336 '[@class="settings-list"]' % content_type) |
| 337 except selenium.common.exceptions.NoSuchElementException: |
| 338 self._incognito_list_elem = None |
| 331 | 339 |
| 332 def _GetExceptionList(self): | 340 def _AssertIncognitoAvailable(self): |
| 333 return DynamicList(self._driver, self._list_elem) | 341 if not self._incognito_list_elem: |
| 342 raise AssertionError( |
| 343 'Incognito settings in "%s" content page not available' |
| 344 % self._content_type) |
| 334 | 345 |
| 335 def _GetPatternList(self): | 346 def _GetExceptionList(self, incognito): |
| 347 if not incognito: |
| 348 list_elem = self._list_elem |
| 349 else: |
| 350 list_elem = self._incognito_list_elem |
| 351 return DynamicList(self._driver, list_elem) |
| 352 |
| 353 def _GetPatternList(self, incognito): |
| 354 if not incognito: |
| 355 list_elem = self._list_elem |
| 356 else: |
| 357 list_elem = self._incognito_list_elem |
| 336 pattern_list = [p.text for p in | 358 pattern_list = [p.text for p in |
| 337 self._list_elem.find_elements_by_xpath( | 359 list_elem.find_elements_by_xpath( |
| 338 './/*[contains(@class, "exception-pattern")]' | 360 './/*[contains(@class, "exception-pattern")]' |
| 339 '//*[@class="static-text"]')] | 361 '//*[@class="static-text"]')] |
| 340 return pattern_list | 362 return pattern_list |
| 341 | 363 |
| 342 def AddNewException(self, pattern, behavior): | 364 def AddNewException(self, pattern, behavior, incognito=False): |
| 343 """Add a new pattern and behavior to the Exceptions page. | 365 """Add a new pattern and behavior to the Exceptions page. |
| 344 | 366 |
| 345 Args: | 367 Args: |
| 346 pattern: Hostname pattern string. | 368 pattern: Hostname pattern string. |
| 347 behavior: Setting for the hostname pattern (Allow, Block, Session Only). | 369 behavior: Setting for the hostname pattern (Allow, Block, Session Only). |
| 370 incognito: Incognito list box. Default to false. |
| 348 | 371 |
| 349 Raises: | 372 Raises: |
| 350 AssertionError when an exception cannot be added on the content page. | 373 AssertionError when an exception cannot be added on the content page. |
| 351 """ | 374 """ |
| 375 if incognito: |
| 376 self._AssertIncognitoAvailable() |
| 377 list_elem = self._incognito_list_elem |
| 378 else: |
| 379 list_elem = self._list_elem |
| 352 # Select behavior first. | 380 # Select behavior first. |
| 353 try: | 381 try: |
| 354 self._list_elem.find_element_by_xpath( | 382 list_elem.find_element_by_xpath( |
| 355 './/*[@class="exception-setting"]' | 383 './/*[@class="exception-setting"]' |
| 356 '[not(@displaymode)]//option[@value="%s"]' | 384 '[not(@displaymode)]//option[@value="%s"]' |
| 357 % behavior).click() | 385 % behavior).click() |
| 358 except selenium.common.exceptions.NoSuchElementException: | 386 except selenium.common.exceptions.NoSuchElementException: |
| 359 raise AssertionError( | 387 raise AssertionError( |
| 360 'Adding new exception not allowed in "%s" content page' | 388 'Adding new exception not allowed in "%s" content page' |
| 361 % self._content_type) | 389 % self._content_type) |
| 362 # Set pattern now. | 390 # Set pattern now. |
| 363 self._GetExceptionList().Add(pattern) | 391 self._GetExceptionList(incognito).Add(pattern) |
| 364 | 392 |
| 365 def DeleteException(self, pattern): | 393 def DeleteException(self, pattern, incognito=False): |
| 366 """Delete the exception for the selected hostname pattern. | 394 """Delete the exception for the selected hostname pattern. |
| 367 | 395 |
| 368 Args: | 396 Args: |
| 369 pattern: Hostname pattern string. | 397 pattern: Hostname pattern string. |
| 398 incognito: Incognito list box. Default to false. |
| 370 """ | 399 """ |
| 371 self._GetExceptionList().Remove(pattern) | 400 if incognito: |
| 401 self._AssertIncognitoAvailable() |
| 402 self._GetExceptionList(incognito).Remove(pattern) |
| 372 | 403 |
| 373 def GetExceptions(self): | 404 def GetExceptions(self, incognito=False): |
| 374 """Returns a dictionary of {pattern: behavior}. | 405 """Returns a dictionary of {pattern: behavior}. |
| 375 | 406 |
| 376 Example: {'file:///*': 'block'} | 407 Example: {'file:///*': 'block'} |
| 408 |
| 409 Args: |
| 410 incognito: Incognito list box. Default to false. |
| 377 """ | 411 """ |
| 378 pattern_list = self._GetPatternList() | 412 if incognito: |
| 379 behavior_list = self._list_elem.find_elements_by_xpath( | 413 self._AssertIncognitoAvailable() |
| 414 list_elem = self._incognito_list_elem |
| 415 else: |
| 416 list_elem = self._list_elem |
| 417 pattern_list = self._GetPatternList(incognito) |
| 418 behavior_list = list_elem.find_elements_by_xpath( |
| 380 './/*[@role="listitem"][@class="deletable-item"]' \ | 419 './/*[@role="listitem"][@class="deletable-item"]' \ |
| 381 '//*[@class="exception-setting"][@displaymode="static"]') | 420 '//*[@class="exception-setting"][@displaymode="static"]') |
| 382 assert len(pattern_list) == len(behavior_list), \ | 421 assert len(pattern_list) == len(behavior_list), \ |
| 383 'Number of patterns does not match the behaviors.' | 422 'Number of patterns does not match the behaviors.' |
| 384 return dict(zip(pattern_list, [b.text.lower() for b in behavior_list])) | 423 return dict(zip(pattern_list, [b.text.lower() for b in behavior_list])) |
| 385 | 424 |
| 386 def GetBehaviorForPattern(self, pattern): | 425 def GetBehaviorForPattern(self, pattern, incognito=False): |
| 387 """Returns the behavior for a given pattern on the Exceptions page. | 426 """Returns the behavior for a given pattern on the Exceptions page. |
| 388 | 427 |
| 389 Args: | 428 Args: |
| 390 pattern: Hostname pattern string. | 429 pattern: Hostname pattern string. |
| 430 incognito: Incognito list box. Default to false. |
| 391 """ | 431 """ |
| 392 assert self.GetExceptions().has_key(pattern), \ | 432 if incognito: |
| 433 self._AssertIncognitoAvailable() |
| 434 assert self.GetExceptions(incognito).has_key(pattern), \ |
| 393 'No displayed host name matches pattern "%s"' % pattern | 435 'No displayed host name matches pattern "%s"' % pattern |
| 394 return self.GetExceptions()[pattern] | 436 return self.GetExceptions(incognito)[pattern] |
| 395 | 437 |
| 396 def SetBehaviorForPattern(self, pattern, behavior): | 438 def SetBehaviorForPattern(self, pattern, behavior, incognito=False): |
| 397 """Set the behavior for the selected pattern on the Exceptions page. | 439 """Set the behavior for the selected pattern on the Exceptions page. |
| 398 | 440 |
| 399 Args: | 441 Args: |
| 400 pattern: Hostname pattern string. | 442 pattern: Hostname pattern string. |
| 401 behavior: Setting for the hostname pattern (Allow, Block, Session Only). | 443 behavior: Setting for the hostname pattern (Allow, Block, Session Only). |
| 444 incognito: Incognito list box. Default to false. |
| 402 | 445 |
| 403 Raises: | 446 Raises: |
| 404 AssertionError when the behavior cannot be changed on the content page. | 447 AssertionError when the behavior cannot be changed on the content page. |
| 405 """ | 448 """ |
| 406 pattern_list = self._GetPatternList() | 449 if incognito: |
| 407 listitem_list = self._list_elem.find_elements_by_xpath( | 450 self._AssertIncognitoAvailable() |
| 451 list_elem = self._incognito_list_elem |
| 452 else: |
| 453 list_elem = self._list_elem |
| 454 pattern_list = self._GetPatternList(incognito) |
| 455 listitem_list = list_elem.find_elements_by_xpath( |
| 408 './/*[@role="listitem"][@class="deletable-item"]') | 456 './/*[@role="listitem"][@class="deletable-item"]') |
| 409 pattern_listitem_dict = dict(zip(pattern_list, listitem_list)) | 457 pattern_listitem_dict = dict(zip(pattern_list, listitem_list)) |
| 410 # Set focus to appropriate listitem. | 458 # Set focus to appropriate listitem. |
| 411 listitem_elem = pattern_listitem_dict[pattern] | 459 listitem_elem = pattern_listitem_dict[pattern] |
| 412 listitem_elem.click() | 460 listitem_elem.click() |
| 413 # Set behavior. | 461 # Set behavior. |
| 414 try: | 462 try: |
| 415 listitem_elem.find_element_by_xpath( | 463 listitem_elem.find_element_by_xpath( |
| 416 './/option[@value="%s"]' % behavior).click() | 464 './/option[@value="%s"]' % behavior).click() |
| 417 except selenium.common.exceptions.ElementNotVisibleException: | 465 except selenium.common.exceptions.ElementNotVisibleException: |
| 418 raise AssertionError( | 466 raise AssertionError( |
| 419 'Changing the behavior is invalid for pattern ' | 467 'Changing the behavior is invalid for pattern ' |
| 420 '"%s" in "%s" content page' % (behavior, self._content_type)) | 468 '"%s" in "%s" content page' % (behavior, self._content_type)) |
| 421 # Send enter key. | 469 # Send enter key. |
| 422 pattern_elem = listitem_elem.find_element_by_tag_name('input') | 470 pattern_elem = listitem_elem.find_element_by_tag_name('input') |
| 423 _FocusField(self._driver, self._list_elem, pattern_elem) | 471 _FocusField(self._driver, self._list_elem, pattern_elem) |
| 424 pattern_elem.send_keys('\n') | 472 pattern_elem.send_keys('\n') |
| OLD | NEW |