def __init__( self, token: str = None, projectname: str = None, jobname: str = None, disable_reports: bool = False, ): if Generic.__instance is not None: raise SdkException("A driver session already exists") LoggingHelper.configure_logging() self._token = token if token is not None else ConfigHelper.get_developer_token( ) agent_status_response: AgentStatusResponse = AgentClient.get_agent_version( self._token) if version.parse(agent_status_response.tag) < version.parse( Generic.MIN_GENERIC_DRIVER_SUPPORTED_VERSION): raise AgentConnectException( f"Your current Agent version {agent_status_response.tag} does not support the Generic driver. " f"Please upgrade your Agent to the latest version and try again" ) else: logging.info( f"Current Agent version {agent_status_response.tag} does support Generic driver" ) self.session_id = None if disable_reports: # Setting the project and job name to empty strings will cause the Agent to not initialize a report self._projectname = "" self._jobname = "" else: self._projectname = (projectname if projectname is not None else ReportHelper.infer_project_name()) self._jobname = (jobname if jobname is not None else ReportHelper.infer_job_name()) reportsettings = ReportSettings(self._projectname, self._jobname) capabilities = {"platformName": "ANY"} self._agent_client: AgentClient = AgentClient( token=self._token, capabilities=capabilities, report_settings=reportsettings, ) self._agent_session: AgentSession = self._agent_client.agent_session self.command_executor = GenericCommandExecutor( agent_client=self._agent_client) Generic.__instance = self
def test_get_agent_status_response_with_error_http_status_code_raises_agentconnectexception( mocked_agent_address, ): # Mock the response returned by the Agent when retrieving the address responses.add(responses.GET, "http://localhost:9876/api/status", status=500) with pytest.raises(AgentConnectException) as ace: AgentClient.get_agent_version(token="1234") assert (str(ace.value) == "Agent returned HTTP 500 when trying to retrieve Agent status")
def test_get_agent_status_no_response_raises_sdkexception( mocked_agent_address): # Mock the response returned by the Agent when retrieving the address responses.add(responses.GET, "http://localhost:9876/api/status", status=200) with pytest.raises(SdkException) as sdke: AgentClient.get_agent_version(token="1234") assert str( sdke.value ) == "Could not parse Agent status response: no JSON response body present"
def test_get_agent_status_response_without_tag_element_raises_sdkexception( mocked_agent_address, ): # Mock the response returned by the Agent when retrieving the address responses.add( responses.GET, "http://localhost:9876/api/status", json={"key": "value"}, status=200, ) with pytest.raises(SdkException) as sdke: AgentClient.get_agent_version(token="1234") assert str( sdke.value ) == "Could not parse Agent status response: element 'tag' not found in JSON response body"
def __init__( self, capabilities: dict, token: str, projectname: str, jobname: str, disable_reports: bool, ): if BaseDriver.__instance is not None: raise SdkException("A driver session already exists") LoggingHelper.configure_logging() if token is not None: logging.info(f"Token used as specified in constructor: {token}") self._token = token if token is not None else ConfigHelper.get_developer_token( ) if disable_reports: # Setting the project and job name to empty strings will cause the Agent to not initialize a report self._projectname = "" self._jobname = "" else: self._projectname = (projectname if projectname is not None else ReportHelper.infer_project_name()) self._jobname = (jobname if jobname is not None else ReportHelper.infer_job_name()) self._agent_client: AgentClient = AgentClient( token=self._token, capabilities=capabilities, reportsettings=ReportSettings(self._projectname, self._jobname), ) self._agent_session: AgentSession = self._agent_client.agent_session self.w3c = True if self._agent_session.dialect == "W3C" else False # Create a custom command executor to enable: # - automatic logging capabilities # - customized reporting settings self.command_executor = CustomCommandExecutor( agent_client=self._agent_client, remote_server_addr=self._agent_session.remote_address, ) self.command_executor.disable_reports = disable_reports RemoteWebDriver.__init__( self, command_executor=self.command_executor, desired_capabilities=self._agent_session.capabilities, ) BaseDriver.__instance = self
def __init__( self, desired_capabilities: dict = None, token: str = None, project_name: str = None, job_name: str = None, disable_reports: bool = False, ): if Remote.__instance is not None: raise SdkException("A driver session already exists") LoggingHelper.configure_logging() self._desired_capabilities = desired_capabilities self._token = token if token is not None else ConfigHelper.get_developer_token( ) if disable_reports: # Setting the project and job name to empty strings will cause the Agent to not initialize a report self._project_name = "" self._job_name = "" else: self._project_name = (project_name if project_name is not None else ReportHelper.infer_project_name()) self._job_name = (job_name if job_name is not None else ReportHelper.infer_job_name()) report_settings = ReportSettings(self._project_name, self._job_name) self._agent_client: AgentClient = AgentClient( token=self._token, capabilities=self._desired_capabilities, report_settings=report_settings, ) self._agent_session: AgentSession = self._agent_client.agent_session self.w3c = True if self._agent_session.dialect == "W3C" else False AppiumWebDriver.__init__( self, command_executor=self._agent_session.remote_address, desired_capabilities=self._desired_capabilities, ) self.command_executor = CustomAppiumCommandExecutor( agent_client=self._agent_client, remote_server_addr=self._agent_session.remote_address, ) # this ensures that mobile-specific commands are also available for our command executor self._addCommands() Remote.__instance = self
def test_get_agent_status_response_with_tag_element_creates_agentstatusresponse( mocked_agent_address, ): # Mock the response returned by the Agent when retrieving the address responses.add( responses.GET, "http://localhost:9876/api/status", json={"tag": "1.2.3"}, status=200, ) agent_status_response: AgentStatusResponse = AgentClient.get_agent_version( token="1234") assert agent_status_response.tag == "1.2.3"
def test_action_proxy_payload_is_correctly_formatted(): # Arrange - Create a custom proxy descriptor proxy_descriptor = ProxyDescriptor("my_guid", "my_classname") proxy_descriptor.by = By.CSS_SELECTOR proxy_descriptor.by_value = "my_css_selector" proxy_descriptor.parameters = {"key": "value"} # Arrange - Create an action proxy object action_proxy = ActionProxy() action_proxy.proxydescriptor = proxy_descriptor # Act - Create the payload that will be sent to the Agent to execute this action payload = AgentClient._create_action_proxy_payload(action_proxy) # Assert - Check that the payload contains the expected keys and associated values assert payload["by"] == {"cssSelector": "my_css_selector"} assert payload["className"] == "my_classname" assert payload["guid"] == "my_guid" assert payload["parameters"] == {"key": "value"}
def __init__( self, desired_capabilities: dict = None, token: str = None, project_name: str = None, job_name: str = None, disable_reports: bool = False, ): if Remote.__instance is not None: raise SdkException("A driver session already exists") LoggingHelper.configure_logging() self._desired_capabilities = desired_capabilities self._token = token if token is not None else ConfigHelper.get_developer_token( ) if disable_reports: # Setting the project and job name to empty strings will cause the Agent to not initialize a report self._project_name = "" self._job_name = "" else: self._project_name = (project_name if project_name is not None else ReportHelper.infer_project_name()) if job_name: self._job_name = job_name else: self._job_name = ReportHelper.infer_job_name() # Can update job name at runtime if not specified. os.environ[ EnvironmentVariable.TP_UPDATE_JOB_NAME.value] = "True" report_settings = ReportSettings(self._project_name, self._job_name) self._agent_client: AgentClient = AgentClient( token=self._token, capabilities=self._desired_capabilities, report_settings=report_settings, ) self._agent_session: AgentSession = self._agent_client.agent_session self.w3c = True if self._agent_session.dialect == "W3C" else False AppiumWebDriver.__init__( self, command_executor=self._agent_session.remote_address, desired_capabilities=self._desired_capabilities, ) self.command_executor = CustomAppiumCommandExecutor( agent_client=self._agent_client, remote_server_addr=self._agent_session.remote_address, ) # this ensures that mobile-specific commands are also available for our command executor self._addCommands() # Disable automatic command and test reports if Behave reporting is enabled. if os.getenv("TP_DISABLE_AUTO_REPORTING") == "True": self.command_executor.disable_command_reports = True self.command_executor.disable_auto_test_reports = True Remote.__instance = self
def __init__( self, capabilities: dict, token: str, project_name: str, job_name: str, disable_reports: bool, report_type: ReportType, ): if BaseDriver.__instance is not None: raise SdkException("A driver session already exists") LoggingHelper.configure_logging() if token is not None: logging.info(f"Token used as specified in constructor: {token}") self._token = token if token is not None else ConfigHelper.get_developer_token( ) if disable_reports: # Setting the project and job name to empty strings will cause the Agent to not initialize a report self._project_name = "" self._job_name = "" else: self._project_name = project_name if project_name is not None else ReportHelper.infer_project_name( ) if job_name: self._job_name = job_name else: self._job_name = ReportHelper.infer_job_name() # Can update job name at runtime if not specified. os.environ[ EnvironmentVariable.TP_UPDATE_JOB_NAME.value] = "True" self._agent_client: AgentClient = AgentClient( token=self._token, capabilities=capabilities, report_settings=ReportSettings(self._project_name, self._job_name, report_type), ) self._agent_session: AgentSession = self._agent_client.agent_session self.w3c = True if self._agent_session.dialect == "W3C" else False # Create a custom command executor to enable: # - automatic logging capabilities # - customized reporting settings self.command_executor = CustomCommandExecutor( agent_client=self._agent_client, remote_server_addr=self._agent_session.remote_address, ) self.command_executor.disable_reports = disable_reports # Disable automatic command and test reports if Behave reporting is enabled. if os.getenv("TP_DISABLE_AUTO_REPORTING") == "True": self.command_executor.disable_command_reports = True self.command_executor.disable_auto_test_reports = True RemoteWebDriver.__init__( self, command_executor=self.command_executor, desired_capabilities=self._agent_session.capabilities, ) BaseDriver.__instance = self