Log to Console from an Appium Plugin

85 Views Asked by At

Summary

I am building an Appium Plugin.

While building my plugin I've attempted to call console.log(). I expected the String argument to appear in the Terminal window I start the Appium Server using my plugin in.

Although, amongst the server logs, the String passed to console.log() is missing.

I think I'm missing something simple, since there's nothing written about this recently and it seems like a big deal to be able to develop.

My Code

MyPlugin

import BasePlugin from '@appium/base-plugin';
import type { BaseDriver } from 'appium/driver';
type NextHandler = () => Promise<string>;
export declare function customLogger(message: string): void;
export declare class MyApp extends BasePlugin {
    getPageSource(next: NextHandler, driver: BaseDriver<any>): Promise<import("my-app").Results>;
}
export {};
import BasePlugin from '@appium/base-plugin'
import type { BaseDriver } from 'appium/driver'
import myPlugin from '@my-plugin/appium-engine'
import axios from 'axios'
import * as fs from 'fs';
import * as path from 'path';

type NextHandler = () => Promise<string>

export class MyApp extends BasePlugin {
  public async getPageSource(next: NextHandler, driver: BaseDriver<any>) {
    // this is the API key that we set in the capabilities
    const apiKey = driver.caps.key
    const originalSource = await next()
    console.log("My console log")
    const results = myPlugin(originalSource)

    // this sends data to the server
    const { data } = await axios.post(
      'http://localhost:3001/api/scans',
      { scan: results },
      {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': apiKey
        }
      }
    )

    return results
  }
}

My Client

from appium import webdriver
from appium.options.ios import XCUITestOptions
from appium.webdriver.appium_connection import AppiumConnection

class CustomAppiumConnection(AppiumConnection):
    # Can add your own methods for the custom class
    pass

custom_executor = CustomAppiumConnection(remote_server_addr='http://0.0.0.1:1234')

options = XCUITestOptions().load_capabilities({
    'platformVersion': '16.2',
    'deviceName': 'iPhone 14',
    'udid': '450B76CF-CF54-4DBC-90E5-FC4E567EF52C',
    'key': 'value'
})
driver = webdriver.Remote(custom_executor, options=options)
pageSource = driver.page_source

Starting My Server

appium --use-plugins @my-plugin/appium-plugin --log-level debug

From WebDriver Docs

@property
    def page_source(self) -> str:
        """Gets the source of the current page.

        :Usage:
            ::

                driver.page_source
        """
        return self.execute(Command.GET_PAGE_SOURCE)["value"]
GET_PAGE_SOURCE: str = "getPageSource"

What I've Tried

  • Building the project
  • Writing logs to a file, hence the import * as fs from 'fs';, and so on
2

There are 2 best solutions below

0
On

Using the Appium custom logger, this.logger.debug("My log") worked.

1
On

Have you tried logging in the constructor of your plugin? I think the original getPageSource itself is not being run therefore your console.log() isn't being executed either.