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):
self.FillPasswordInto("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:
environment.AddWebsiteTest(NewWebsiteTest("website name"))
......@@ -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.
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
accounts.
<websites>
......
......@@ -67,12 +67,11 @@ class TestRunner(object):
# TODO(vabr): Ideally we would replace timeout with something allowing
# 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
# Wait() in websitetest.py). Accounting for some more time spent on
# the non-waiting execution, 300 seconds should be the upper bound on
# the runtime of one pass, thus 600 seconds for the whole test.
self.test_cmd = ["timeout", "600"] + self.test_cmd
# Wait() in websitetest.py). Expecting that not every scenario should
# take 200s, the maximum time allocated for all of them is 300s.
self.test_cmd = ["timeout", "300"] + self.test_cmd
self.logger.log(SCRIPT_DEBUG,
"TestRunner set up for test %s, command '%s', "
......@@ -110,21 +109,26 @@ class TestRunner(object):
def _check_if_test_passed(self):
"""Returns True if and only if the test passed."""
success = False
if os.path.isfile(self.results_path):
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:
self.failures.append(line)
count += line.count("successful='True'")
# There is only two tests running for every website: the prompt and
# the normal test. If both of the tests were successful, the tests
# would be stopped for the current website.
self.logger.log(SCRIPT_DEBUG, "Test run of %s: %s",
self.test_name, "pass" if count == 2 else "fail")
if count == 2:
return True
return False
passed_scenarios += line.count("successful='True'")
success = passed_scenarios == NUMBER_OF_TEST_SCENARIOS
if success:
break
self.logger.log(
SCRIPT_DEBUG,
"Test run of {0} succeded: {1}".format(self.test_name, success))
return success
def _run_test(self):
"""Executes the command to run the test."""
......
......@@ -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.
Args:
......@@ -488,17 +488,16 @@ def saveResults(environment_tests_results, environment_save_path):
"""
if environment_save_path:
xml = "<result>"
for test_result in environment_tests_results:
xml += ("<test name='%s' successful='%s' type='%s'>%s</test>"
% (test_result.name, str(test_result.successful),
test_result.test_type, test_result.message))
for (name, test_type, success, failure_log) in environment_tests_results:
xml += (
"<test name='{0}' successful='{1}' type='{2}'>{3}</test>".format(
name, success, test_type, failure_log))
xml += "</result>"
with open(environment_save_path, "w") as save_file:
save_file.write(xml)
def RunTest(chrome_path, chromedriver_path, profile_path,
environment_passwords_path, enable_automatic_password_saving,
website_test_name):
environment_passwords_path, website_test_name, test_type):
"""Runs the test for the specified website.
Args:
......@@ -506,8 +505,6 @@ def RunTest(chrome_path, chromedriver_path, profile_path,
chromedriver_path: The chromedriver binary file.
profile_path: The chrome testing profile folder.
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
all_tests above).
......@@ -519,26 +516,22 @@ def RunTest(chrome_path, chromedriver_path, profile_path,
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_passwords_path,
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:
environment.AddWebsiteTest(all_tests[website_test_name])
else:
raise Exception("Test name {} is unknown.".format(website_test_name))
environment.AllTests(run_prompt_tests)
environment.RunTestsOnSites(test_type)
environment.Quit()
return environment.tests_results
# Tests setup.
if __name__ == "__main__":
def main():
parser = argparse.ArgumentParser(
description="Password Manager automated tests help.")
......@@ -568,22 +561,19 @@ if __name__ == "__main__":
if args.save_path:
save_path = args.save_path
# Run the test without enable-automatic-password-saving to check whether or
# not the prompt is shown in the way we expected.
tests_results = RunTest(args.chrome_path,
args.chromedriver_path,
args.profile_path,
args.passwords_path,
False,
args.test)
# Run the test with enable-automatic-password-saving to check whether or not
# the passwords is stored in the the way we expected.
tests_results += RunTest(args.chrome_path,
args.chromedriver_path,
args.profile_path,
args.passwords_path,
True,
args.test)
saveResults(tests_results, save_path)
tests_results = RunTest(
args.chrome_path, args.chromedriver_path, args.profile_path,
args.passwords_path, args.test, WebsiteTest.TEST_TYPE_PROMPT_FAIL)
tests_results += RunTest(
args.chrome_path, args.chromedriver_path, args.profile_path,
args.passwords_path, args.test, WebsiteTest.TEST_TYPE_PROMPT_SUCCESS)
tests_results += RunTest(
args.chrome_path, args.chromedriver_path, args.profile_path,
args.passwords_path, args.test, WebsiteTest.TEST_TYPE_SAVE_AND_AUTOFILL)
SaveResults(tests_results, save_path)
if __name__ == "__main__":
main()
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