Skip to content

feat: add two new mobile flags#13887

Closed
wswebcreation wants to merge 11 commits intomainfrom
ws/add-native-context-flag
Closed

feat: add two new mobile flags#13887
wswebcreation wants to merge 11 commits intomainfrom
ws/add-native-context-flag

Conversation

@wswebcreation
Copy link
Member

@wswebcreation wswebcreation commented Nov 17, 2024

This PR adds new session flags called isNativeContext and mobileContext. These flags will tell you if

  • the mobile is in the native context or not
  • what the context is

It also reverts the extra WebDriver-call that was added to the implicitWait adnd use the isNativeContext flag

They will automatically change if the context changes

Given this config:

// wdio.conf.js
export const config = {
    // ...
    capabilities: [
      {
        platformName: 'iOS',
        app: 'net.company.SafariLauncher',
        udid: '123123123123abc',
        deviceName: 'iPhone',
        // ...
      }
    ],
    // ...
}

You can access these flags like all other (mobile) flags in your test like so:

console.log(driver.isMobile)        // outputs: true
console.log(driver.isNativeContext) // outputs: true
console.log(driver.mobileContext)   // outputs: NATIVE_APP

Because it's attached to the contextManager it will also update the status if you do this based on the below config

// wdio.conf.js
export const config = {
    // ...
    capabilities: [
      {
        platform name: 'iOS',
        browserName: 'safari',
        deviceName: 'iPhone',
        // ...
      }
    ],
    // ...
}

```js
//.....
console.log(driver.isNativeContext)   // outputs: false
console.log(driver.mobileContext)     // outputs: WEBVIEW_8919

await driver.switchContext('NATIVE_APP')
console.log(driver.isNativeContext)   // outputs: true
console.log(driver.mobileContext)     // outputs: NATIVE_APP
    
await driver.switchContext('WEBVIEW_8919')
console.log(driver.isNativeContext)   // outputs: false
console.log(driver.mobileContext)     // outputs: WEBVIEW_8919

Proposed changes

Types of changes

  • Polish (an improvement for an existing feature)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (improvements to the project's docs)
  • Specification changes (updates to WebDriver command specifications)
  • Internal updates (everything related to internal scripts, governance documentation and CI files)

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Backport Request

//: # (The current main branch is the development branch for WebdriverIO v9. If your change should be released to the current major version of WebdriverIO (v8), please raise another PR with the same changes against the v8 branch.)

  • This change is solely for v9 and doesn't need to be back-ported
  • Back-ported PR at #XXXXX

Further comments

Reviewers: @webdriverio/project-committers

@wswebcreation
Copy link
Member Author

wswebcreation commented Nov 17, 2024

@christian-bromann

I'm not sure if you like my way to handle the way to be able to overwrite the session-flag, but it was the easiest way without too much code impact and , imho, the least impact.

If you agree with this I can revert the changes for the scrollIntoView and reduce the extra webdriver call => done

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 17, 2024

Open in Stackblitz

eslint-plugin-wdio

pnpm add https://pkg.pr.new/webdriverio/webdriverio/eslint-plugin-wdio@13887

@wdio/allure-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/allure-reporter@13887

@wdio/appium-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/appium-service@13887

@wdio/browser-runner

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/browser-runner@13887

@wdio/browserstack-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/browserstack-service@13887

@wdio/cli

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/cli@13887

@wdio/concise-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/concise-reporter@13887

@wdio/config

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/config@13887

@wdio/cucumber-framework

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/cucumber-framework@13887

@wdio/dot-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/dot-reporter@13887

@wdio/firefox-profile-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/firefox-profile-service@13887

@wdio/globals

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/globals@13887

@wdio/jasmine-framework

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/jasmine-framework@13887

@wdio/json-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/json-reporter@13887

@wdio/junit-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/junit-reporter@13887

@wdio/lighthouse-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/lighthouse-service@13887

@wdio/local-runner

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/local-runner@13887

@wdio/logger

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/logger@13887

@wdio/mocha-framework

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/mocha-framework@13887

@wdio/protocols

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/protocols@13887

@wdio/repl

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/repl@13887

@wdio/reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/reporter@13887

@wdio/runner

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/runner@13887

@wdio/sauce-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/sauce-service@13887

@wdio/shared-store-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/shared-store-service@13887

@wdio/smoke-test-cjs-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-cjs-service@13887

@wdio/smoke-test-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-reporter@13887

@wdio/smoke-test-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-service@13887

@wdio/spec-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/spec-reporter@13887

@wdio/static-server-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/static-server-service@13887

@wdio/sumologic-reporter

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/sumologic-reporter@13887

@wdio/testingbot-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/testingbot-service@13887

@wdio/types

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/types@13887

@wdio/utils

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/utils@13887

@wdio/webdriver-mock-service

pnpm add https://pkg.pr.new/webdriverio/webdriverio/@wdio/webdriver-mock-service@13887

webdriver

pnpm add https://pkg.pr.new/webdriverio/webdriverio/webdriver@13887

webdriverio

pnpm add https://pkg.pr.new/webdriverio/webdriverio@13887

commit: c0cfc6e

The mobileContext will provide the context in which the app is, this is depending on browser/app
@wswebcreation wswebcreation marked this pull request as draft November 17, 2024 10:23
this.#currentContext = context
if (this.#browser.isMobile) {
this.#browser.isNativeContext = context ? context === 'NATIVE_APP' : this.#browser.isNativeContext
this.#browser.mobileContext = context || undefined
Copy link
Member Author

Choose a reason for hiding this comment

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

@christian-bromann ,

Not sure why I'm getting an error Property 'mobileContext' does not exist on type 'Browser'. here. I've added it to the types, but might be missing something

- simplified `mobileContext`
- fixed setting the property in the monad
- updated the tests
- only set the context for mobile
@wswebcreation wswebcreation marked this pull request as ready for review November 17, 2024 12:49
@wswebcreation wswebcreation added the PR: New Feature 🚀 PRs that contain new features label Nov 17, 2024
@wswebcreation wswebcreation changed the title feat: add flag isNativeContext feat: add two new mobile flags Nov 19, 2024
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

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

Some comments. My main request would be to move this into the webdriverio package and handle this flag as a getter returning a dynamic value based on the context manager. See #13896 where I do the same for the isBidi flag.

| `isMobile` | `Boolean` | Indicates a mobile session. See more under [Mobile Flags](#mobile-flags). |
| `isIOS` | `Boolean` | Indicates an iOS session. See more under [Mobile Flags](#mobile-flags). |
| `isAndroid` | `Boolean` | Indicates an Android session. See more under [Mobile Flags](#mobile-flags). |
| `isNativeContext` | `Boolean` | Indicates if the mobile is in the `NATIVE_APP` context. See more under [Mobile Flags](#mobile-flags). |
Copy link
Member

Choose a reason for hiding this comment

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

Can we add isBidi here as we forgot about it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, will do

Comment on lines +276 to +277
isNativeContext: { value: isNativeContext },
mobileContext: { value: mobileContext },
Copy link
Member

Choose a reason for hiding this comment

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

We should define these environment flags in the webdriverio package instead. This will allow use to manage this value dynamically through the context manager, e.g.

Suggested change
isNativeContext: { value: isNativeContext },
mobileContext: { value: mobileContext },
isNativeContext: { get: function () {
const context = getContextManager(this)
return context.isNativeContext
} },

We should avoid having both flags being writeable as we don't want users mess around with it. I think we can achieve this easily with proposed approach. I suggest adding these flags here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, will fix this

Comment on lines +75 to +85
/**
* We need to be able to overwrite some properties in case it changes during the session
*/
const writableProperties = ['isNativeContext', 'mobileContext']
writableProperties.forEach((property) => {
propertiesObject[property] = {
value: propertiesObject[property]?.value,
writable: true,
configurable: true,
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Then we can remove this.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

// We can only determine it for Android and Chrome, for all other, including iOS, we return undefined
isAndroid(capabilities) && capabilities?.browserName?.toLowerCase() === 'chrome' ? 'CHROMIUM' :
undefined
}
Copy link
Member

Choose a reason for hiding this comment

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

And move this into the ContextManager class to compute the initial value.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@wswebcreation
Copy link
Member Author

Thanks for the feedback @christian-bromann , will work on it and fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 PRs that contain new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants