Running a website automation test on your own iPhone with Selenium

Shawn Lobo
3 min readJun 30, 2021

--

Ever since smartphones came into the market, the number of mobile visits on websites have sky rocketed. This also means websites need to be optimised specifically to cater to mobile audience.

Why run a test on a physical device over simulator or mobile emulation?

A physical device is a true 1:1 comparison of what a user would see when visiting your website. Testing on a physical device will allow detecting defects and bugs during early stages of development that would otherwise have been found when someone with a device notices it.

Prerequisites for testing on a Physical iOS device:

  1. iPhone
  2. Mac
  3. Lightning to USB cable
  4. An IDE of your choice

Preparing the iPhone

  1. Head to Settings > Safari > Advanced
  2. Toggle Remote Automation to ON

Preparing the Mac

  1. Enable the safaridriver

Open the terminal and run the below command:

% safaridriver --enable

2. Connect the iPhone to the Mac with the lightning cable

Click on Trust to pair the device to the Mac.

Note: In case you do not see this alert, it is possible that the device is already paired with the Mac.

3. Fetch the iPhone UDID and name.

Open Finder > Your iPhone

Click on the subtext below Your iPhone to reveal the UDID. This will be required later.

Putting it all together

For this article I’ll be using Python 3 to simply open a webpage to keep it short.

  1. Install Python on your Mac
  2. Install the selenium language binding
    % pip3 install selenium

3. Build the test script

Issues I have come across:

  1. Remote Automation is turned off
Message: Could not create a session: Some devices were found, but could not be used:
- Remote Automation is turned off (turn it on via Settings > Safari > Advanced > Remote Automation)

Simply toggle it back on via Settings > Safari > Advanced > Remote Automation

Next, open Safari and keep it open prior to the test. This sometimes seems to get the tests to start

2. The Safari instance is already paired with another WebDriver session.

selenium.common.exceptions.SessionNotCreatedException: Message: Could not create a session: The Safari instance is already paired with another WebDriver session.

Ensure that the previous instance is stopped.

That’s all for my first article. I plan on adding some more articles on automation setups that I use on a daily basis.

--

--

Shawn Lobo
Shawn Lobo

Written by Shawn Lobo

Web app developer with love for circuits, Internet of Things, engines and ☕️ who codes with his 🐱 .

No responses yet