blob: 40a61dff8007e9922bb7d14a7d65f8149efce941 [file] [log] [blame]
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Provides Appium driver object."""
from __future__ import absolute_import
from appium_util import appium_driver_util
class DriverProvider(object):
"""Provides Appium driver used to run tests.
Attributes:
platform_name: A string representing the platform to run tests against.
channel: A string indicating the channel to run tests against.
app_path: A string representing the app under test full path.
appium_command: list of strings representing appium invocation command
appium_dir_path: A string for Appium server directory full path.
appium_log_folder: A path to store Appium logs. If not set,
Appium log will store in test_suite folder.
appium_process: A handle to the Appium server process.
device: A string representing the device type.
app_id: A string representing the app id.
device_version: A string representing the device version.
device_name: A string representing the device name.
device_id: A string representing the device id.
driver: A webdriver used to run tests.
host_address: The IP address used to run the Appium server.
appium_port: The port used to start Appium server.
use_prebuilt_wda: A bool indicating not to rebuild WebDriverAgent.
use_uiautomator2: A bool indicating to use uiautomator_2.
xcconfig: A string representing the XCode config file for starting WDA.
test_suite: A path to test suite csv file.
device_class: Needs to filter test step based on device class.
app_intent: (Android only) Intent used to start an app.
skip_reset: A boolean to indicate if a full reset should be skipped.
"""
def __init__(self, **kwargs):
"""Inits DriverProvider.
Args:
**kwargs: Arguments to be passed.
"""
app_info = kwargs['app_info']
device_info = kwargs['device_info']
self.platform_name = kwargs['platform_name']
self.channel = kwargs['channel']
self.app_path = kwargs['app_path']
self.appium_command = kwargs['appium_command']
self.appium_dir_path = kwargs['appium_dir_path']
self.device_class = device_info.device_class
self.app_id = app_info.id
self.app_intent = app_info.app_intent
self.device_version = device_info.version
self.device_name = device_info.name
self.device_id = device_info.id
self.host_address = kwargs['host_address']
self.appium_port = kwargs['appium_port']
self.skip_reset = kwargs['skip_reset']
self.use_uiautomator2 = kwargs['use_uiautomator2']
self.use_prebuilt_wda = kwargs['use_prebuilt_wda']
self.xcconfig = kwargs['xcconfig']
self.test_suite = kwargs['test_suite']
self.appium_log_folder = (kwargs['appium_log_folder']
if 'appium_log_folder' in kwargs else '')
self._driver = None
self._appium_process = None
self.flags = kwargs['flags']
self.ResetDriver()
@property
def driver(self):
"""Returns a lazy initialized instance of DriverProvider.
Starts Appium server before creating a new driver.
Returns:
A webdriver.
"""
if not self._driver:
appium_process = appium_driver_util.StartServer(
self.appium_dir_path, self.appium_command, self.host_address,
self.appium_port, self.test_suite, self.appium_log_folder)
self._appium_process = appium_process
self._driver = appium_driver_util.CreateAppiumDriver(
self.platform_name, self.app_path, self.app_id, self.app_intent,
self.device_id, self.device_name, self.device_version,
self.device_class, self.host_address, self.appium_port,
self.skip_reset, self.use_prebuilt_wda, self.use_uiautomator2,
self.xcconfig, self.flags)
return self._driver
def ResetDriver(self):
"""Resets Appium driver."""
self._driver = None
def QuitDriver(self):
"""Quit Appium driver(close session)."""
if self._driver:
self._driver.quit()
def Driver(self):
"""Return Appium driver."""
return self._driver