Survey Disable Wifi on iOS Simulator for App Automation

Ian Chen
iCHEF
Published in
4 min readMar 27, 2020
In the end, we will do something like this. LOL😂

To achieve the test case like “[Fail],iPad has no Wifi,Showing Error and cannot-operator the customer info” in automation, We need to find a way to disable the WiFi on the simulator. Due to the limitation of the simulator, it can just only accomplish by turn off the Wifi on the Mac, So far.

The following is the method that we have tried to accomplish this task, We achieve this by using Charles Proxy’s breakpoints and Charles Web Interface which sending the API to Charles inside the automation to simular disable Wifi.

🦄 Using Charles Proxy 🦄

  1. Following the steps to enable the proxy on the simulator. Make sure you can get any request sending from the simulator.

2. Also, enable the Charles web interface “Proxy → Web Interface Settings”. So we can request a command to control Charles. (Like Enable the Breakpoints, Start Recording etc..)

Proxy → Web Interface Settings

3. Here is the command that Charles web interface support.

Available commands

Throttling commands/throttling/deactivate
/throttling/activate: Enable the last used throttling preset.
/throttling/activate?preset=YOUR_PRESET: Enable throttling with one of these specific:
56+kbps+Modem: 56 kbps Modem
256+kbps+ISDN%2FDSL: 256 kbps ISDN/DSL
512+kbps+ISDN%2FDSL: 512 kbps ISDN/DSL
2+Mbps+ADSL: 2 Mbps ADSL
8+Mbps+ADSL2: 8 Mbps ADSL2
16+Mbps+ADSL2%2B: 16 Mbps ADSL2+
32+Mbps+VDSL: 32 Mbps VDSL
32+Mbps+Fibre: 32 Mbps Fibre
100+Mbps+Fibre: 100 Mbps Fibre
3G: 3G
4G: 4g
Recording commands/recording/start
/recording/stop
Tools commands/tools/breakpoints/enable
/tools/breakpoints/disable
/tools/no-caching/enable
/tools/no-caching/disable
/tools/block-cookies/enable
/tools/block-cookies/disable
/tools/map-remote/enable
/tools/map-remote/disable
/tools/map-local/enable
/tools/map-local/disable
/tools/rewrite/enable
/tools/rewrite/disable
/tools/black-list/enable
/tools/black-list/disable
/tools/white-list/enable
/tools/white-list/disable
/tools/dns-spoofing/enable
/tools/dns-spoofing/disable
/tools/auto-save/enable
/tools/auto-save/disable
/tools/client-process/enable
/tools/client-process/disable

with your proxy IP and Port we can try to send the command through the terminal and curl.

curl -v -x http://your_proxy_ip:your_proxy_port http://control.charles/YOUR_COMMAND

You might see something like this, which means you can send an API request inside the App Automation to control Charles start or stop Breakpoints.

curl -v -x http://192.169.3.122:8888 http://control.charles/recording/stop

4. Let’s write something in your automation project to send the command to Charles.

import Foundationprotocol WiFiCharlesAPI {

}
enum charlesCommand: String {
case recordingStart = “/recording/start”
case recordingStop = “/recording/stop”
case breakpointsEnable = “/tools/breakpoints/enable”
case breakpointsDisable = “/tools/breakpoints/disable”
case quit = “/quit”
}
extension WiFiCharlesAPI {
static func sendCharlesAPI(command: charlesCommand) {
let stringURL = charlesWebConsoleURL + command.rawValue

let url = URL(string: stringURL)
var request = URLRequest(url: url!)
request.httpMethod = “GET”

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let semaphore = DispatchSemaphore(value: 0)

session.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(“error:\(error!)\n”)
return
}
let stringData = String(data: data, encoding: String.Encoding.utf8) ?? “”
print(“dataString:\(stringData)”)
semaphore.signal()
}.resume()
semaphore.wait()

}
}

It’s a Protocol Extension so that you can hook to any page class.

5. Add Breakpoints Setting inside the Charles Charles Proxy → Breakpoints setting.

https://* means to block all request and response

Then we can simular the disable Wifi situation in the simulator.

6. After all, calling the API function inside the test case.

// test Proxy
func testCaseC123() {
XCTContext.runActivity(named: “”, block: {_ in
APIPage.sendCharlesAPI(command: .breakpointsEnable)
/**
Your test action
*/
APIPage.sendCharlesAPI(command: .breakpointsDisable)
})
}

Conclusion

It’s much like a workaround for iOS simulator defects. In our product, there are many functions required internet. But it’s sad that we cannot write some test cases in CI (App automation) to protect them. By using Charles Proxy inside the CI, now we can make sure every release those functions will be functioning normally.

https://dev.to/mmazzarolo/charles-proxy-automation-1884

https://www.detroitlabs.com/blog/2018/05/01/how-to-set-up-charles-proxy-for-an-ios-simulator/

--

--