summaryrefslogtreecommitdiffstats
path: root/chrome/test/pyautolib
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/test/pyautolib')
-rw-r--r--chrome/test/pyautolib/pyauto.py74
-rw-r--r--chrome/test/pyautolib/pyautolib.cc74
-rw-r--r--chrome/test/pyautolib/pyautolib.h54
-rw-r--r--chrome/test/pyautolib/pyautolib.i63
4 files changed, 265 insertions, 0 deletions
diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py
new file mode 100644
index 0000000..66abdc9
--- /dev/null
+++ b/chrome/test/pyautolib/pyauto.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python
+
+# Copyright (c) 2010 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.
+
+"""PyAuto: Python Interface to Chromium's Automation Proxy.
+
+PyAuto uses swig to expose Automation Proxy interfaces to Python.
+For complete documentation on the functionality available,
+run pydoc on this file.
+
+Ref: http://dev.chromium.org/developers/pyauto
+"""
+
+import os
+import sys
+import unittest
+
+
+def _LocateBinDirs():
+ script_dir = os.path.dirname(__file__)
+ chrome_src = os.path.join(script_dir, os.pardir, os.pardir, os.pardir)
+
+ # TODO(nirnimesh): Expand this to include win/linux build dirs
+ # crbug.com/32285
+ bin_dirs = [ os.path.join(chrome_src, 'xcodebuild', 'Debug'),
+ os.path.join(chrome_src, 'xcodebuild', 'Release'),
+ ]
+ for d in bin_dirs:
+ sys.path.append(os.path.normpath(d))
+
+_LocateBinDirs()
+
+try:
+ from pyautolib import PyUITestSuite
+except ImportError:
+ print >>sys.stderr, "Could not locate built libraries. Did you build?"
+ raise
+
+
+class PyUITest(PyUITestSuite, unittest.TestCase):
+ """Base class for UI Test Cases in Python.
+
+ A browser is created before executing each test, and is destroyed after
+ each test irrespective of whether the test passed or failed.
+
+ You should derive from this class and create methods with 'test' prefix,
+ and use methods inherited from PyUITestSuite (the C++ side).
+
+ Example:
+
+ class MyTest(PyUITest):
+
+ def testNavigation(self):
+ self.NavigateToURL("http://www.google.com")
+ self.assertTrue("Google" == self.GetActiveTabTitle())
+ """
+
+ def __init__(self, methodName='runTest'):
+ PyUITestSuite.__init__(self, sys.argv)
+ unittest.TestCase.__init__(self, methodName)
+
+ def __del__(self):
+ PyUITestSuite.__del__(self)
+
+ def run(self, result=None):
+ """The main run method.
+
+ We override this method to make calls to the setup steps in PyUITestSuite.
+ """
+ self.SetUp() # Open a browser window
+ unittest.TestCase.run(self, result)
+ self.TearDown() # Destroy the browser window
diff --git a/chrome/test/pyautolib/pyautolib.cc b/chrome/test/pyautolib/pyautolib.cc
new file mode 100644
index 0000000..a891e8e
--- /dev/null
+++ b/chrome/test/pyautolib/pyautolib.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2010 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.
+
+#include "base/scoped_ptr.h"
+#include "chrome/test/automation/tab_proxy.h"
+#include "chrome/test/pyautolib/pyautolib.h"
+#include "googleurl/src/gurl.h"
+
+PyUITestSuite::PyUITestSuite(int argc, char** argv)
+ : UITestSuite(argc, argv),
+ UITestBase() {
+ UITestSuite::Initialize();
+}
+
+PyUITestSuite::~PyUITestSuite() {
+ UITestSuite::Shutdown();
+}
+
+void PyUITestSuite::SetUp() {
+ UITestBase::SetUp();
+}
+
+void PyUITestSuite::TearDown() {
+ UITestBase::TearDown();
+}
+
+void PyUITestSuite::NavigateToURL(const char* url_string) {
+ GURL url(url_string);
+ UITestBase::NavigateToURL(url);
+}
+
+void PyUITestSuite::SetShelfVisible(bool is_visible) {
+ scoped_refptr<BrowserProxy> browser_proxy = automation()->GetBrowserWindow(0);
+ ASSERT_TRUE(browser_proxy.get());
+ EXPECT_TRUE(browser_proxy->SetShelfVisible(is_visible));
+}
+
+bool PyUITestSuite::IsShelfVisible() {
+ scoped_refptr<BrowserProxy> browser_proxy = automation()->GetBrowserWindow(0);
+ EXPECT_TRUE(browser_proxy.get());
+ if (!browser_proxy.get())
+ return false;
+ bool visible = false;
+ EXPECT_TRUE(browser_proxy->IsShelfVisible(&visible));
+ return visible;
+}
+
+std::wstring PyUITestSuite::GetActiveTabTitle() {
+ std::wstring title;
+ scoped_refptr<TabProxy> tab_proxy(GetActiveTab());
+ EXPECT_TRUE(tab_proxy.get());
+ if (!tab_proxy.get())
+ return title;
+ EXPECT_TRUE(tab_proxy->GetTabTitle(&title));
+ return title;
+}
+
+void PyUITestSuite::OpenFindInPage() {
+ scoped_refptr<BrowserProxy> browser_proxy = automation()->GetBrowserWindow(0);
+ ASSERT_TRUE(browser_proxy.get());
+ EXPECT_TRUE(browser_proxy->OpenFindInPage());
+}
+
+bool PyUITestSuite::IsFindInPageVisible() {
+ scoped_refptr<BrowserProxy> browser_proxy = automation()->GetBrowserWindow(0);
+ EXPECT_TRUE(browser_proxy.get());
+ if (!browser_proxy.get())
+ return false;
+ bool is_visible;
+ EXPECT_TRUE(browser_proxy->IsFindWindowFullyVisible(&is_visible));
+ return is_visible;
+}
+
diff --git a/chrome/test/pyautolib/pyautolib.h b/chrome/test/pyautolib/pyautolib.h
new file mode 100644
index 0000000..04fe7a8
--- /dev/null
+++ b/chrome/test/pyautolib/pyautolib.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2010 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.
+//
+// This file declares the C++ side of PyAuto, the python interface to
+// Chromium automation. It access Chromium's internals using Automation Proxy.
+
+#ifndef CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_
+#define CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_
+
+#include "base/scoped_nsautorelease_pool.h"
+#include "chrome/test/ui/ui_test.h"
+#include "chrome/test/ui/ui_test_suite.h"
+
+// TODO(nirnimesh): separate out the UITestSuite and UITestBase parts
+// crbug.com/32292
+
+// The primary class that interfaces with Automation Proxy.
+// This class is accessed from python using swig.
+class PyUITestSuite : public UITestSuite, public UITestBase {
+ public:
+ // Only public methods are accessible from swig.
+ PyUITestSuite(int argc, char** argv);
+ ~PyUITestSuite();
+
+ // SetUp,TearDown is redeclared as public to make it accessible from swig.
+ virtual void SetUp();
+ virtual void TearDown();
+
+ void NavigateToURL(const char* url_string);
+
+ // Get the title of the active tab. Empty string in case of error.
+ std::wstring GetActiveTabTitle();
+
+ // BrowserProxy methods
+
+ // Shows or hides the download shelf.
+ void SetShelfVisible(bool is_visible);
+
+ // Determines the visibility of the download shelf
+ bool IsShelfVisible();
+
+ // Open the Find box
+ void OpenFindInPage();
+
+ // Determines the visibility of the Find box
+ bool IsFindInPageVisible();
+
+ private:
+ base::ScopedNSAutoreleasePool pool_;
+};
+
+#endif // CHROME_TEST_PYAUTOLIB_PYAUTOLIB_H_
+
diff --git a/chrome/test/pyautolib/pyautolib.i b/chrome/test/pyautolib/pyautolib.i
new file mode 100644
index 0000000..83854c0
--- /dev/null
+++ b/chrome/test/pyautolib/pyautolib.i
@@ -0,0 +1,63 @@
+// Copyright (c) 2010 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.
+//
+// Swig Interface for PyAuto.
+// PyAuto makes the Automation Proxy interface available in Python
+//
+// Running swig as:
+// swig -python -c++ chrome/test/pyautolib/pyautolib.i
+// would generate pyautolib.py, pyautolib_wrap.cxx
+
+%module pyautolib
+
+%include "std_string.i"
+%include "std_wstring.i"
+
+%{
+#include "chrome/test/pyautolib/pyautolib.h"
+%}
+
+// Make functions using (int argc, char** argv) usable as (sys.argv) from python
+%typemap(in) (int argc, char **argv) {
+ int i;
+ if (!PyList_Check($input)) {
+ PyErr_SetString(PyExc_ValueError, "Expecting a list");
+ return NULL;
+ }
+ $1 = PyList_Size($input);
+ $2 = (char **) malloc(($1+1)*sizeof(char *));
+ for (i = 0; i < $1; i++) {
+ PyObject *s = PyList_GetItem($input,i);
+ if (!PyString_Check(s)) {
+ free($2);
+ PyErr_SetString(PyExc_ValueError, "List items must be strings");
+ return NULL;
+ }
+ $2[i] = PyString_AsString(s);
+ }
+ $2[i] = 0;
+}
+
+%typemap(freearg) (int argc, char **argv) {
+ if ($2) free($2);
+}
+
+
+// TODO(nirnimesh): Fill in the autodoc constructs - crbug.com/32287
+class PyUITestSuite {
+ public:
+ PyUITestSuite(int argc, char** argv);
+
+ virtual void SetUp();
+ virtual void TearDown();
+
+ void NavigateToURL(const char* url_string);
+ std::wstring GetActiveTabTitle();
+
+ // BrowserProxy methods
+ void SetShelfVisible(bool is_visible);
+ bool IsShelfVisible();
+ void OpenFindInPage();
+ bool IsFindInPageVisible();
+};