def __init__(self): self._cache = ApplicationCache() self._timeout_in_secs = float(5)
class _ApplicationManagementKeywords(KeywordGroup): def __init__(self): self._cache = ApplicationCache() self._timeout_in_secs = float(5) # Public, open and close def close_application(self): """Closes the current application.""" self._debug('Closing application with session id %s' % self._current_application().session_id) self._cache.close() def close_all_applications(self): """Closes all open applications. This keyword is meant to be used in test or suite teardown to make sure all the applications are closed before the test execution finishes. After this keyword, the application indices returned by `Open Application` are reset and start from `1`. """ self._debug('Closing all applications') self._cache.close_all() def open_application(self, remote_url, alias=None, **kwargs): """Opens a new application to given Appium server. Capabilities of appium server, Android and iOS, Please check http://appium.io/slate/en/master/?python#appium-server-capabilities | *Option* | *Man.* | *Description* | | remote_url | Yes | Appium server url | | alias | no | alias | Examples: | Open Application | http://localhost:4723/wd/hub | alias=Myapp1 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | Open Application | http://localhost:4723/wd/hub | platformName=Android | platformVersion=4.2.2 | deviceName=192.168.56.101:5555 | app=${CURDIR}/demoapp/OrangeDemoApp.apk | appPackage=com.netease.qa.orangedemo | appActivity=MainActivity | """ desired_caps = kwargs application = webdriver.Remote(str(remote_url), desired_caps) self._debug('Opened application with session id %s' % application.session_id) return self._cache.register(application, alias) def switch_application(self, index_or_alias): """Switches the active application by index or alias. `index_or_alias` is either application index (an integer) or alias (a string). Index is got as the return value of `Open Application`. This keyword returns the index of the previous active application, which can be used to switch back to that application later. Example: | ${appium1}= | Open Application | http://localhost:4723/wd/hub | alias=MyApp1 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | ${appium2}= | Open Application | http://localhost:4755/wd/hub | alias=MyApp2 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | Click Element | sendHello | # Executed on appium running at localhost:4755 | | Switch Application | ${appium1} | # Switch using index | | Click Element | ackHello | # Executed on appium running at localhost:4723 | | Switch Application | MyApp2 | # Switch using alias | | Page Should Contain Text | ackHello Received | # Executed on appium running at localhost:4755 | """ old_index = self._cache.current_index if index_or_alias is None: self._cache.close() else: self._cache.switch(index_or_alias) return old_index def reset_application(self): """ Reset application """ driver = self._current_application() driver.reset() def remove_application(self, application_id): """ Removes the application that is identified with an application id Example: | Remove Application | com.netease.qa.orangedemo | """ driver = self._current_application() driver.remove_app(application_id) def get_appium_timeout(self): """Gets the timeout in seconds that is used by various keywords. See `Set Appium Timeout` for an explanation.""" return robot.utils.secs_to_timestr(self._timeout_in_secs) def set_appium_timeout(self, seconds): """Sets the timeout in seconds used by various keywords. There are several `Wait ...` keywords that take timeout as an argument. All of these timeout arguments are optional. The timeout used by all of them can be set globally using this keyword. The previous timeout value is returned by this keyword and can be used to set the old value back later. The default timeout is 5 seconds, but it can be altered in `importing`. Example: | ${orig timeout} = | Set Appium Timeout | 15 seconds | | Open page that loads slowly | | Set Appium Timeout | ${orig timeout} | """ old_timeout = self.get_appium_timeout() self._timeout_in_secs = robot.utils.timestr_to_secs(seconds) return old_timeout def get_source(self): """Returns the entire source of the current page.""" return self._current_application().page_source def log_source(self, loglevel='INFO'): """Logs and returns the entire html source of the current page or frame. The `loglevel` argument defines the used log level. Valid log levels are `WARN`, `INFO` (default), `DEBUG`, `TRACE` and `NONE` (no logging). """ source = self._current_application().page_source self._log(source, loglevel.upper()) return source def go_back(self): """Goes one step backward in the browser history.""" self._current_application().back() def lock(self): """ Lock the device """ self._current_application().lock() def background_app(self, seconds=5): """ Puts the application in the background on the device for a certain duration. """ self._current_application().background_app(seconds) def shake(self): """ Shake the device """ self._current_application().shake() def portrait(self): """ Set the device orientation to PORTRAIT """ self._rotate('PORTRAIT') def landscape(self): """ Set the device orientation to LANDSCAPE """ self._rotate('LANDSCAPE') def get_current_context(self): """Get current context.""" return self._current_application().current_context def get_contexts(self): """Get available contexts.""" print self._current_application().contexts return self._current_application().contexts def switch_to_context(self, context_name): """Switch to a new context""" self._current_application().switch_to.context(context_name) def go_to_url(self, url): """ Opens URL in default web browser. Example: | Open Application | http://localhost:4755/wd/hub | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | browserName=Safari | | Go To URL | http://m.webapp.com | """ self._current_application().get(url) # Private def _current_application(self): if not self._cache.current: raise RuntimeError('No application is open') return self._cache.current def _get_platform(self): try: platformName = self._current_application( ).desired_capabilities['desired']['platformName'] except Exception, e: raise Exception, e return platformName.lower()
class _ApplicationManagementKeywords(KeywordGroup): def __init__(self): self._cache = ApplicationCache() self._timeout_in_secs = float(5) # Public, open and close def close_application(self): """Closes the current application.""" self._debug('Closing application with session id %s' % self._current_application().session_id) self._cache.close() def close_all_applications(self): """Closes all open applications. This keyword is meant to be used in test or suite teardown to make sure all the applications are closed before the test execution finishes. After this keyword, the application indices returned by `Open Application` are reset and start from `1`. """ self._debug('Closing all applications') self._cache.close_all() def open_application(self, remote_url, alias=None, **kwargs): """Opens a new application to given Appium server. Capabilities of appium server, Android and iOS, Please check http://appium.io/slate/en/master/?python#appium-server-capabilities | *Option* | *Man.* | *Description* | | remote_url | Yes | Appium server url | | alias | no | alias | Examples: | Open Application | http://localhost:4723/wd/hub | alias=Myapp1 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | Open Application | http://localhost:4723/wd/hub | platformName=Android | platformVersion=4.2.2 | deviceName=192.168.56.101:5555 | app=${CURDIR}/demoapp/OrangeDemoApp.apk | appPackage=com.netease.qa.orangedemo | appActivity=MainActivity | """ desired_caps = kwargs application = webdriver.Remote(str(remote_url), desired_caps) self._debug('Opened application with session id %s' % application.session_id) return self._cache.register(application, alias) def switch_application(self, index_or_alias): """Switches the active application by index or alias. `index_or_alias` is either application index (an integer) or alias (a string). Index is got as the return value of `Open Application`. This keyword returns the index of the previous active application, which can be used to switch back to that application later. Example: | ${appium1}= | Open Application | http://localhost:4723/wd/hub | alias=MyApp1 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | ${appium2}= | Open Application | http://localhost:4755/wd/hub | alias=MyApp2 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app | | Click Element | sendHello | # Executed on appium running at localhost:4755 | | Switch Application | ${appium1} | # Switch using index | | Click Element | ackHello | # Executed on appium running at localhost:4723 | | Switch Application | MyApp2 | # Switch using alias | | Page Should Contain Text | ackHello Received | # Executed on appium running at localhost:4755 | """ old_index = self._cache.current_index if index_or_alias is None: self._cache.close() else: self._cache.switch(index_or_alias) return old_index def reset_application(self): """ Reset application """ driver = self._current_application() driver.reset() def remove_application(self, application_id): """ Removes the application that is identified with an application id Example: | Remove Application | com.netease.qa.orangedemo | """ driver = self._current_application() driver.remove_app(application_id) def get_appium_timeout(self): """Gets the timeout in seconds that is used by various keywords. See `Set Appium Timeout` for an explanation.""" return robot.utils.secs_to_timestr(self._timeout_in_secs) def set_appium_timeout(self, seconds): """Sets the timeout in seconds used by various keywords. There are several `Wait ...` keywords that take timeout as an argument. All of these timeout arguments are optional. The timeout used by all of them can be set globally using this keyword. The previous timeout value is returned by this keyword and can be used to set the old value back later. The default timeout is 5 seconds, but it can be altered in `importing`. Example: | ${orig timeout} = | Set Appium Timeout | 15 seconds | | Open page that loads slowly | | Set Appium Timeout | ${orig timeout} | """ old_timeout = self.get_appium_timeout() self._timeout_in_secs = robot.utils.timestr_to_secs(seconds) return old_timeout def get_source(self): """Returns the entire source of the current page.""" return self._current_application().page_source def log_source(self, loglevel='INFO'): """Logs and returns the entire html source of the current page or frame. The `loglevel` argument defines the used log level. Valid log levels are `WARN`, `INFO` (default), `DEBUG`, `TRACE` and `NONE` (no logging). """ source = self._current_application().page_source self._log(source, loglevel.upper()) return source def go_back(self): """Goes one step backward in the browser history.""" self._current_application().back() def lock(self): """ Lock the device """ self._current_application().lock() def background_app(self, seconds=5): """ Puts the application in the background on the device for a certain duration. """ self._current_application().background_app(seconds) def shake(self): """ Shake the device """ self._current_application().shake() def portrait(self): """ Set the device orientation to PORTRAIT """ self._rotate('PORTRAIT') def landscape(self): """ Set the device orientation to LANDSCAPE """ self._rotate('LANDSCAPE') def get_current_context(self): """Get current context.""" return self._current_application().current_context def get_contexts(self): """Get available contexts.""" print self._current_application().contexts return self._current_application().contexts def switch_to_context(self, context_name): """Switch to a new context""" self._current_application().switch_to.context(context_name) def go_to_url(self, url): """ Opens URL in default web browser. Example: | Open Application | http://localhost:4755/wd/hub | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | browserName=Safari | | Go To URL | http://m.webapp.com | """ self._current_application().get(url) # Private def _current_application(self): if not self._cache.current: raise RuntimeError('No application is open') return self._cache.current def _get_platform(self): try: platformName = self._current_application().desired_capabilities['desired']['platformName'] except Exception, e: raise Exception, e return platformName.lower()