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 'maestro.platform' for javascript to determine platform #1747

Merged
merged 1 commit into from
May 20, 2024

Conversation

Fishbowler
Copy link
Contributor

Proposed Changes

Add a platform property to the maestro object in JavaScript, so as to determine iOS or Android at runtime.

output.welcomeMessage = "Your app is running on " + maestro.platform

Why though?

Previously, an app that requested a few permissions would have lots of this:

- tapOn: "Enable Location"
- runFlow:
    when:
      platform: 'Android'
    commands:
      - assertVisible: "Allow access to this device's location?"
      - tapOn: "While using the app"
- runFlow:
    when:
        platform: 'iOS'
    commands:
        - assertVisible: "This app uses your location to show you information about your local environment."
        - tapOn: "Allow While Using App"
- assertVisible:
    id: "location_permission_true"

I wanted to set the strings in JavaScript, but this would require prepending the flow with something like:

- runFlow:
    when:
      platform: 'Android'
    commands:
      - runScript: 
          file: setPermissionsVars.js
          env:
            MOBILE_OS: 'Android'
- runFlow:
    when:
      platform: 'iOS'
    commands:
      - runScript: 
          file: setPermissionsVars.js
          env:
            MOBILE_OS: 'iOS'

After this change:

- runScript: setPermissionsVars.js
- tapOn: "Enable Location"
- assertVisible: ${output.locationPermissionAlert}
- tapOn: ${output.locationPermissionButton}
- assertVisible:
    id: "location_permission_true"

Testing

I've tested this with a more trivial example:

flow.yml:

appId: org.wikimedia.wikipedia #iOS
#appId: org.wikipedia #Android
jsEngine: graaljs
---
- launchApp
- runScript: 'setSearchTerm.js'
- tapOn: "Search Wikipedia"
- inputText: ${output.searchTerm}

- runFlow:
    when:
      platform: 'Android'
    commands:
      - assertVisible: Robot
- runFlow:
    when:
      platform: 'iOS'
    commands:
      - assertVisible: Fruit.*

setSearchTerm.js:

output.searchTerm = 'blackberries'
if (maestro.platform == 'android'){
    output.searchTerm = 'robots'
}
if (maestro.platform == 'ios'){
    output.searchTerm = 'apples'
}

Tested on iOS and Android, in Rhino & Graal.

@axelniklasson axelniklasson self-assigned this May 17, 2024
@axelniklasson axelniklasson self-requested a review May 17, 2024 14:39
@axelniklasson
Copy link
Collaborator

Thanks for raising this @Fishbowler, I'll review it next week. Please do also raise a PR to https://github.com/mobile-dev-inc/maestro-docs updating this page with information about the platform object and a code example. Thanks!

@Fishbowler
Copy link
Contributor Author

Done. Potentially a bit of overkill on the docs, but we can deal with that in the review of that PR :D

Copy link
Collaborator

@axelniklasson axelniklasson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @Fishbowler, we really appreciate the contribution 🎉

@axelniklasson axelniklasson merged commit bad91c1 into mobile-dev-inc:main May 20, 2024
1 check passed
@Fishbowler Fishbowler deleted the javascript-platform branch June 2, 2024 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants