local-api-client-python icon indicating copy to clipboard operation
local-api-client-python copied to clipboard

Automate Mobile Profiles

Open maxbaum8 opened this issue 4 years ago • 2 comments

Just fooling around with the mobile profiles, I was wondering if there is any way to connect and control my mobile device with selenium automation.

When I try connecting to the profile id selenium throws the error: "selenium.common.exceptions.SessionNotCreatedException: Message: This profile is using the External Spoofing Engine, so Selenium connection is not allowed.'

Is there any work around you are aware of or other language which makes automation of the mobile device through Kameleo achievable?

maxbaum8 avatar Oct 05 '21 14:10 maxbaum8

Nevermind, I think I have found the solution. Guess I'd have to use a library like appium.

Another question though, don't think this is setup but I think it would be useful for me.

When starting up the mobile connection with the apk android application, I cannot edit the port number which is different (the allowance of the manual entries would be ideal as it can be automated by appium). Ideally I would like to be able to get this variable from the http api requests or somewhere else and be able to automatically setup phones / android vms to programatically be passed the port number and IP address to connect to the spoofer on. Because right now I was only able to connect via the QR code.

maxbaum8 avatar Oct 06 '21 12:10 maxbaum8

As we understand you would like to automate browsing on your mobile device with Kameleo. Currently, it is not possible to control your mobile device's browser with Kameleo. But this is a feature we will support soon.

As you said a workaround can be reached with Appium. You just need to know what port number you should enter for the connection.

If you would like to automate your Mobile profiles here is a workaround: once you call the StartProfile endpoint you will receive the following response

{
  "persistenceState": "unsaved",
  "lifetimeState": "running",
  "externalSpoofingEnginePort": 34664
}

The externalSpoofingEnginePort's value is what you are looking for. It will be provided only for mobile profiles, or for desktop profiles with external launcher. You can get it from GET /profiles/{guid}/status endpoint as well. See the documentation on SwaggerHub.

If you don't use appium, you can automate any other browser with Selenium or Puppeteer, while they will "emulate" a mobile browser thanks to Kameleo. For this, please read the article about the External Spoofing Engine. Basically this is working as a proxy, so you can set it up as an HTTP proxy for your custom browser, and this will apply a spoofing on the browser and it will look like a mobile device.

This "custom browser", can be a browser that is started by Puppeteer, so you can easily automate it with Puppeteer. See this example for some orientation.

const createProfileRequest = BuilderForCreateProfile
	.forBaseProfile(chromeBaseProfileList[0].id)
	.setRecommendedDefaults()
	.setLauncher('external')
	.build();

const profile = await client.createProfile({ body: createProfileRequest });

// Start the profile
const result = await client.startProfile(profile.id);

// Run Puppeteer
const browser = await puppeteer.launch({
	headless: false,
	defaultViewport: null,
	args: [ `--proxy-server=http://127.0.0.1:${result.externalSpoofingEnginePort}` ]
});

const page = await browser.newPage();
const url = 'https://help.kameleo.io/hc/en-us';

try {
	await page.goto(url)
	await new Promise((r) => setTimeout(r, 10000));
	await browser.close();
} catch (e) {
	console.log(e);
}

kameleo-team avatar Oct 06 '21 14:10 kameleo-team

Automation of mobile profiles was introduced since our latest reply. Please follow this article.

Just a quick example:

  1. search for mobile base profiles:
base_profile_list = client.search_base_profiles(
  device_type='mobile',
  os_family='ios',
  browser_product='safari',
  language='en-us'
)
  1. start profile with Chroma:
create_profile_request = BuilderForCreateProfile \
  .for_base_profile(base_profile_list[0].id) \
  .set_recommended_defaults() \
  .set_launcher('chromium') \
  .build()
profile = client.create_profile(body=create_profile_request)
  1. set up touch events properly
client.start_profile_with_options(profile.id, body={
  'additionalOptions': [
    {
      'key': 'disableTouchEmulation',
      'value': True,
    },
  ],
})

Then you are ready to automate with each of the frameworks (Selenium, Puppeteer, Playwright)

kameleo-team avatar Oct 24 '23 07:10 kameleo-team