Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell to PatrolIntegrationTester, or ability to execute code on testing machine #2386

Open
Sevastyan opened this issue Oct 30, 2024 · 6 comments
Assignees
Labels
feature New feature request package: patrol Related to the patrol package (native automation, test bundling)

Comments

@Sevastyan
Copy link

Use case

I am writing patrol test to test my Flutter app on Android emulator on my machine. I want to be able to run terminal commands on my host machine during the patrol test (inside the PatrolTesterCallback).

This feature request is indeed related to the problem I am having, however the feature will serve a much wider variety of cases than described shortly.

So, the problem is that I cannot trigger Android's flow for handling URLs of Instant Apps. When testing manually, I usually fire this terminal command:

adb shell 'am start -a android.intent.action.VIEW -d "https://..." com.my.applicationId'

this brings up this system UI:

And then, when testing manually, I click "OPEN APP", which opens Android Instant App, from which I download my full app and get to testing further.

The adb command above is not the only one I wish to be able to run from within the patrol test. I also want to generate the URL during the test, and for this I have a nifty CLI command on my machine (it contacts my server to get me the URL).

To sum up, I want to start my patrol test by running a few terminal commands (executing them on my host machine), and then start using patrol's finders to assert what is shown on the emulator's screen.


What I have tried:

There is already a native openUrl method provided by patrol. However, it never opens the Android's UI as in the picture above - it simply opens the browser (not even my app, which actually handles the URL nicely). openUrl will only open my app, if the URL schema is a custom schema that my app supports. I mean, my app support both "https://" and "mycustomapp" shemas. But with patrol I am not able to test "https" schema, as it always opens the browser. This, obviously, makes it impossible to test the real user flow - user clicking on the URL, user making a decision to continue viewing the URL in the Instant App, user interacting with Instant App, user downloading the full app, and user interacting with the full app.

Proposal

void main() {
  patrolTest(
    'My test.',
    ($) async {
      final result = $.host.run('command', ['option', 'value']); // akin to Process.run on the host
      // use result...
    },
  );
}
@jBorkowska jBorkowska self-assigned this Oct 31, 2024
@jBorkowska jBorkowska added feature New feature request package: patrol Related to the patrol package (native automation, test bundling) labels Oct 31, 2024
@bartekpacia
Copy link
Contributor

this was implemented in #630 but then removed

@seftoner
Copy link

Hello @bartekpacia, since it was removed, is there any workaround to achieve the desired action?

@kikuchy
Copy link

kikuchy commented Feb 4, 2025

I am also stuck in a similar situation. Is there any workaround?

@Sevastyan
Copy link
Author

@kikuchy One way around this, that I have thought about, but not actually implemented, is to create a separate dedicated app just to test the Instant App invocation. I assume this is your case, and you are not in need to run some random code on the host machine before a test.

@kikuchy
Copy link

kikuchy commented Feb 4, 2025

@Sevastyan Thank you for response! In my case, it's ok to run test only on Android and throwing Intent, it's looks like to be enough to use Process.run() in test case code.

@seftoner
Copy link

seftoner commented Feb 12, 2025

I needed to run a CLI command on the host machine (Mac OS) during an E2E test using Patrol. The tests run on an Android device, but during the test, I had to set up the server environment and verify if the client responded correctly.

Since Process.run() executes commands on the Android device, I needed a way to run commands on the host (Mac OS).

My workaround

I created a simple Python web service that wraps the CLI tool, allowing me to execute commands remotely via HTTP from my tests.

Example Scenario

Scenario: Logged out on server side
    Given Home Assistant access is configured  # NEED TO RUN REMOTE COMMAND
    And I have successfully logged in  
    And the application is running in the foreground  
    And I see {K.home.page} page  
    ...

Calling the Tool from Code

// Create long-lived token command
final createToken = [
      'raw',
      'ws',
      'auth/long_lived_access_token',
      '--json={"lifespan":3650,"client_name":"$_clientName"}'
    ].join(' ');

final result = await _cli.execute(createToken);

// Execute implementation
Future<Either<CommandError, CommandResult>> execute(String command) async {
    try {
      final response = await http.post(
        Uri.parse(_cliServerUrl),
        headers: {'Content-Type': 'application/json'},
        body: jsonEncode({
          'command': command,
          'token': _token,
        }),
      );

      final data = jsonDecode(response.body);

      return Right(
        CommandResult(
          stdout: data['stdout'],
          stderr: data['stderr'],
          exitCode: data['exit_code'],
        ),
      );
    } catch (e) {
      return Left(CommandError('Failed to execute command', e));
    }
}

This setup allows executing CLI commands remotely, similar to a lightweight remote terminal over HTTP. It currently wraps a single tool, but it could be extended to support more general remote execution. I used this approach for my project https://github.com/seftoner/hommie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request package: patrol Related to the patrol package (native automation, test bundling)
Projects
None yet
Development

No branches or pull requests

6 participants