0

I am trying to automate Web app on iOS Safari for e2e test.
Even though following code worked for emulator in iOS 17 beta,
it seems not working on Real Device(iPhone 12, iOS 16.6).

code for test

const fs = require("fs");
const { Builder, By, Key, until } = require("selenium-webdriver");
const edge = require("selenium-webdriver/edge");
const ie = require("selenium-webdriver/ie");
const chrome = require("selenium-webdriver/chrome");
const safari = require("selenium-webdriver/safari");
const os = require("os");
const path = require("path");
const PNG = require("pngjs").PNG;
const { NodeSSH, Config } = require("node-ssh");
const { execSync } = require("child_process");
const webdriver = require("selenium-webdriver")
const { Preferences, Type, Level } = require("selenium-webdriver/lib/logging")
const wdio = require('webdriverio')
describe("test0", () => {

  const getDriver = async () => {

    const options = new safari.Options()
    const capabilities = webdriver.Capabilities.safari()
    capabilities.setPlatform('ios')
    capabilities.set('safari:deviceType', 'iphone')
    capabilities.set('safari:useSimulator', false)

    const driver = new Builder().forBrowser('safari').withCapabilities(capabilities).setSafariOptions(options).build()

    return driver
  }

  let driver
  beforeAll(async () => {
    driver = await getDriver()
  }, 10000000)
  
afterAll(async () => {
    await driver.quit();
  });

  test("test", async () => {
      await driver.get("https://google.com");
  }, 1000000);
});

Following error will happen when testing through real device while device is listed on Device and Simulators opened by Xcode > Window > Device and Simulators.

 SessionNotCreatedError: Could not create a session: Some devices were found, but could not be used:
    - iPhone (xxx-xxxxxxx): Remote Automation is turned off (turn it on via Settings > Safari > Advanced > Remote Automation)
    - [unknown wireless device]: device is not paired

      at Object.throwDecodedError (../node_modules/selenium-webdriver/lib/error.js:524:15)
      at parseHttpResponse (../node_modules/selenium-webdriver/lib/http.js:601:13)
      at Executor.execute (../node_modules/selenium-webdriver/lib/http.js:529:28)

What I tried

I turned on Safari > Advanced > Web Inspector, Safari > Advanced > Remote Automation, Developper > Enable UI Automation on iOS setting.

I enabled safaridriver running safaridriver --enable.

Version

Xcode: Xcode 15 beta. iOS: iOS 17 beta. MacOS: Ventura 13.5.

Issei Morita
  • 23
  • 1
  • 5

1 Answers1

0

I solved this problem by spawning safaridriver manually, like following code.

const fs=require("fs")
const { Builder, By, Key, until, WebDriver }=require("selenium-webdriver")
const edge=require("selenium-webdriver/edge")
const ie=require("selenium-webdriver/ie")
const chrome=require("selenium-webdriver/chrome")
const safari=require("selenium-webdriver/safari")
const os=require("os")
const path=require("path")
const PNG=require("pngjs").PNG
const { NodeSSH, Config }=require("node-ssh")
const { execSync, spawn }=require("child_process")
const webdriver=require("selenium-webdriver")
const { Preferences, Type, Level }=require("selenium-webdriver/lib/logging")
const { Executor, HttpClient }=require('selenium-webdriver/http')
const wdio=require('webdriverio')

describe("test0", () => {

  const getDriver=async () => {
    const serverOpts={
      port: 4723,
      hostname: 'localhost',
    }
    const url=`http://${serverOpts.hostname}:${serverOpts.port}`
    const safariDriver=spawn('safaridriver', ['-p', `${serverOpts.port}`, '--diagnose'], { shell: true, env: process.env, detached: false })

    safariDriver.stdout.pipe(process.stdout)
    safariDriver.stderr.pipe(process.stderr)

    await new Promise((resolve) => {
      safariDriver.on('spawn', () => {
        setTimeout(() => {
          resolve()
        }, 3000)
      })
    })

    const executor=
      new Executor(
        new HttpClient(url),
        null,
        null
      )


    return WebDriver.createSession(executor, {
      platformName: 'iOS',
      browserName: 'safari',
      'appium:automationName': 'Safari',
      'safari:deviceType': 'iphone',
      'safari:useSimulator': false,
    }, () => { safariDriver.kill() })
  }

  let driver
  beforeAll(async () => {
    driver = await getDriver()
  }, 10000000)
  
afterAll(async () => {
    await driver.quit();
  });

  test("test", async () => {
      await driver.get("https://google.com");
  }, 1000000);
});
Issei Morita
  • 23
  • 1
  • 5