Commit d0ed6939 authored by vabr's avatar vabr Committed by Commit bot

[Password manager Python tests] Re-arrange tests

This CL changes the structure of the tests. Instead of many repetitions, there are now just 3 types of tests: failed login, successful login, and autofill test.

The Environment and WebsiteTest classes are also being refactored for clarity.

BUG=369521

Review URL: https://codereview.chromium.org/1026833003

Cr-Commit-Position: refs/heads/master@{#322574}
parent 9973bb23
...@@ -75,10 +75,6 @@ class NewWebsiteTest(WebsiteTest): ...@@ -75,10 +75,6 @@ class NewWebsiteTest(WebsiteTest):
self.FillPasswordInto("Password CSS selector") self.FillPasswordInto("Password CSS selector")
self.Submit("Password CSS selector") self.Submit("Password CSS selector")
def Logout(self):
# Add logout steps for the website, for example:
self.Click("Logout button CSS selector")
Then, to create the new test, you need just to add: Then, to create the new test, you need just to add:
environment.AddWebsiteTest(NewWebsiteTest("website name")) environment.AddWebsiteTest(NewWebsiteTest("website name"))
...@@ -149,7 +145,7 @@ manager. When this bug is solved, all the tests that were failing because of ...@@ -149,7 +145,7 @@ manager. When this bug is solved, all the tests that were failing because of
it are going to be moved to working tests. it are going to be moved to working tests.
Other files: Other files:
* websites.xml : a private file where you can find all the passwords. You can * websites.xml: a private file where you can find all the passwords. You can
ask someone to give it to you or just create your own with your personal ask someone to give it to you or just create your own with your personal
accounts. accounts.
<websites> <websites>
......
...@@ -67,12 +67,11 @@ class TestRunner(object): ...@@ -67,12 +67,11 @@ class TestRunner(object):
# TODO(vabr): Ideally we would replace timeout with something allowing # TODO(vabr): Ideally we would replace timeout with something allowing
# calling tests directly inside Python, and working on other platforms. # calling tests directly inside Python, and working on other platforms.
# #
# The website test runs in two passes, each pass has an internal # The website test runs multiple scenarios, each one has an internal
# timeout of 200s for waiting (see |remaining_time_to_wait| and # timeout of 200s for waiting (see |remaining_time_to_wait| and
# Wait() in websitetest.py). Accounting for some more time spent on # Wait() in websitetest.py). Expecting that not every scenario should
# the non-waiting execution, 300 seconds should be the upper bound on # take 200s, the maximum time allocated for all of them is 300s.
# the runtime of one pass, thus 600 seconds for the whole test. self.test_cmd = ["timeout", "300"] + self.test_cmd
self.test_cmd = ["timeout", "600"] + self.test_cmd
self.logger.log(SCRIPT_DEBUG, self.logger.log(SCRIPT_DEBUG,
"TestRunner set up for test %s, command '%s', " "TestRunner set up for test %s, command '%s', "
...@@ -110,21 +109,26 @@ class TestRunner(object): ...@@ -110,21 +109,26 @@ class TestRunner(object):
def _check_if_test_passed(self): def _check_if_test_passed(self):
"""Returns True if and only if the test passed.""" """Returns True if and only if the test passed."""
success = False
if os.path.isfile(self.results_path): if os.path.isfile(self.results_path):
with open(self.results_path, "r") as results: with open(self.results_path, "r") as results:
count = 0 # Count the number of successful tests. # TODO(vabr): Parse the results to make sure all scenarios succeeded
# instead of hard-coding here the number of tests scenarios from
# test.py:main.
NUMBER_OF_TEST_SCENARIOS = 3
passed_scenarios = 0
for line in results: for line in results:
self.failures.append(line) self.failures.append(line)
count += line.count("successful='True'") passed_scenarios += line.count("successful='True'")
success = passed_scenarios == NUMBER_OF_TEST_SCENARIOS
# There is only two tests running for every website: the prompt and if success:
# the normal test. If both of the tests were successful, the tests break
# would be stopped for the current website.
self.logger.log(SCRIPT_DEBUG, "Test run of %s: %s", self.logger.log(
self.test_name, "pass" if count == 2 else "fail") SCRIPT_DEBUG,
if count == 2: "Test run of {0} succeded: {1}".format(self.test_name, success))
return True return success
return False
def _run_test(self): def _run_test(self):
"""Executes the command to run the test.""" """Executes the command to run the test."""
......
...@@ -475,7 +475,7 @@ all_tests = { ...@@ -475,7 +475,7 @@ all_tests = {
} }
def saveResults(environment_tests_results, environment_save_path): def SaveResults(environment_tests_results, environment_save_path):
"""Save the test results in an xml file. """Save the test results in an xml file.
Args: Args:
...@@ -488,17 +488,16 @@ def saveResults(environment_tests_results, environment_save_path): ...@@ -488,17 +488,16 @@ def saveResults(environment_tests_results, environment_save_path):
""" """
if environment_save_path: if environment_save_path:
xml = "<result>" xml = "<result>"
for test_result in environment_tests_results: for (name, test_type, success, failure_log) in environment_tests_results:
xml += ("<test name='%s' successful='%s' type='%s'>%s</test>" xml += (
% (test_result.name, str(test_result.successful), "<test name='{0}' successful='{1}' type='{2}'>{3}</test>".format(
test_result.test_type, test_result.message)) name, success, test_type, failure_log))
xml += "</result>" xml += "</result>"
with open(environment_save_path, "w") as save_file: with open(environment_save_path, "w") as save_file:
save_file.write(xml) save_file.write(xml)
def RunTest(chrome_path, chromedriver_path, profile_path, def RunTest(chrome_path, chromedriver_path, profile_path,
environment_passwords_path, enable_automatic_password_saving, environment_passwords_path, website_test_name, test_type):
website_test_name):
"""Runs the test for the specified website. """Runs the test for the specified website.
Args: Args:
...@@ -506,8 +505,6 @@ def RunTest(chrome_path, chromedriver_path, profile_path, ...@@ -506,8 +505,6 @@ def RunTest(chrome_path, chromedriver_path, profile_path,
chromedriver_path: The chromedriver binary file. chromedriver_path: The chromedriver binary file.
profile_path: The chrome testing profile folder. profile_path: The chrome testing profile folder.
environment_passwords_path: The usernames and passwords file. environment_passwords_path: The usernames and passwords file.
enable_automatic_password_saving: If True, the passwords are going to be
saved without showing the prompt.
website_test_name: Name of the website to test (refer to keys in website_test_name: Name of the website to test (refer to keys in
all_tests above). all_tests above).
...@@ -519,26 +516,22 @@ def RunTest(chrome_path, chromedriver_path, profile_path, ...@@ -519,26 +516,22 @@ def RunTest(chrome_path, chromedriver_path, profile_path,
fails, or if the website name is not known. fails, or if the website name is not known.
""" """
enable_automatic_password_saving = (
test_type == WebsiteTest.TEST_TYPE_SAVE_AND_AUTOFILL)
environment = Environment(chrome_path, chromedriver_path, profile_path, environment = Environment(chrome_path, chromedriver_path, profile_path,
environment_passwords_path, environment_passwords_path,
enable_automatic_password_saving) enable_automatic_password_saving)
# Test which care about the save-password prompt need the prompt
# to be shown. Automatic password saving results in no prompt.
run_prompt_tests = not enable_automatic_password_saving
if website_test_name in all_tests: if website_test_name in all_tests:
environment.AddWebsiteTest(all_tests[website_test_name]) environment.AddWebsiteTest(all_tests[website_test_name])
else: else:
raise Exception("Test name {} is unknown.".format(website_test_name)) raise Exception("Test name {} is unknown.".format(website_test_name))
environment.AllTests(run_prompt_tests) environment.RunTestsOnSites(test_type)
environment.Quit() environment.Quit()
return environment.tests_results return environment.tests_results
# Tests setup. def main():
if __name__ == "__main__":
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Password Manager automated tests help.") description="Password Manager automated tests help.")
...@@ -568,22 +561,19 @@ if __name__ == "__main__": ...@@ -568,22 +561,19 @@ if __name__ == "__main__":
if args.save_path: if args.save_path:
save_path = args.save_path save_path = args.save_path
# Run the test without enable-automatic-password-saving to check whether or tests_results = RunTest(
# not the prompt is shown in the way we expected. args.chrome_path, args.chromedriver_path, args.profile_path,
tests_results = RunTest(args.chrome_path, args.passwords_path, args.test, WebsiteTest.TEST_TYPE_PROMPT_FAIL)
args.chromedriver_path,
args.profile_path, tests_results += RunTest(
args.passwords_path, args.chrome_path, args.chromedriver_path, args.profile_path,
False, args.passwords_path, args.test, WebsiteTest.TEST_TYPE_PROMPT_SUCCESS)
args.test)
tests_results += RunTest(
# Run the test with enable-automatic-password-saving to check whether or not args.chrome_path, args.chromedriver_path, args.profile_path,
# the passwords is stored in the the way we expected. args.passwords_path, args.test, WebsiteTest.TEST_TYPE_SAVE_AND_AUTOFILL)
tests_results += RunTest(args.chrome_path,
args.chromedriver_path, SaveResults(tests_results, save_path)
args.profile_path,
args.passwords_path, if __name__ == "__main__":
True, main()
args.test)
saveResults(tests_results, save_path)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment