2

Using: selenium webdriver, rubygems, appium, android and ios devices

The app I am testing has a button which becomes enabled only when connected to a specific wireless network. I'd like to create a script which will check if the button is active or not

  vKioskStatus = element.enabled?
  puts(vKioskStatus)

If it is not active, then I'd like to change wifi networks. Is that possible to automate changing networks on a mobile device?

UPDATE

I'm receiving the following error when trying to use getNetworkConnection. Is there a require I need to add?

<main>': undefined method `getNetworkConnection' for #<Selenium::WebDriver::Driver:0x..fe1a5511e browser=:firefox> (NoMethodError)

Here's my code:

require 'rubygems'
require 'selenium-webdriver'
require 'uri'
require 'appium_lib'

require_relative 'AndroidLib'
cButton = Buttons.new

driver = Selenium::WebDriver.for(:remote, :url => "http://127.0.0.1:4723/wd/hub") # Works for Android
sleep(5)

bob = driver.getNetworkConnection()
puts bob
user3531858
  • 155
  • 5
  • 17
  • Try `driver.get_network_connection()`. See https://github.com/appium/ruby_lib/blob/master/lib/appium_lib/device/device.rb – David Feb 19 '16 at 01:41

6 Answers6

1

you can toggle the wifi connection in Android (not possible in iOS) using the below appium commands,

To enable flight mode :

# Python

def enableFlightMode(self,context):
        driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
        driver.implicitly_wait(10)
        if driver.network_connection == 1:
            self.report_pass("The network connection is disabled in the mobile and flight mode is active.")
        else:
            self.report_fail("The flight mode is not active yet!")

And to disable flight mode:

def enableFlightMode(self,context):
        driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
        driver.implicitly_wait(10)
        if driver.network_connection == 1:
            self.report_pass("The network connection is disabled in the mobile and flight mode is active.")
        else:
            self.report_fail("The flight mode is not active yet!")
siva2503
  • 11
  • 2
  • The "disable" code snippet is just a copy from the "enable" function. How would disableFlightMode work? Or: what options are there in general for the set_network_connection function? – schoenk Mar 21 '23 at 17:23
0

You can achieve this by accessing all available wifi networks using Shell Script. It means you need to write logic of firing ADB/Shell commands through your code.Connecting to wifi using adb shell

On your button click you can fire command to connect to desired wifi network.

Community
  • 1
  • 1
  • I'll have to read up on this a little bit more to make sure I understand it correctly. On my mobile device I have 3 wireless networks which I can connect. Sometimes I want to connect to network1 and in the middle of the test I may want to change to network 2 or 3 then back to network1. – user3531858 Aug 17 '15 at 12:53
0

Yes, it is possible to change the network using Appium. Look here

Gaurav
  • 1,332
  • 11
  • 22
  • I'm not quite getting how this works because I get an error when I'm attempting to execute the code. Is it possible for my selenium rubygem script to change the device's active network connection on the fly? – user3531858 Aug 20 '15 at 11:57
  • I'm new to this so pls excuse all of my questions. I see the getNetworkConnection is set in the java-client binding, but is there one for rubygems? Is there a way to use both java and rubygems languages in a specific script? – user3531858 Aug 21 '15 at 13:29
  • I think should work in Ruby, but your syntax is incorrect, should be `driver.get_network_connection()`. See https://github.com/appium/ruby_lib/blob/master/lib/appium_lib/device/device.rb – David Feb 19 '16 at 01:39
0

I am using below adb commands to turn on & off WiFi/data.And it is working fine.

Turn on wifi - adb shell am start -n io.appium.settings/.Settings -e wifi on

Turn off WiFi - adb shell am start -n io.appium.settings/.Settings -e wifi off

Turn on mobile data - adb shell am start -n io.appium.settings/.Settings -e data on

Turn off mobile data - adb shell am start -n io.appium.settings/.Settings -e data off

Chandrashekhar Swami
  • 1,742
  • 23
  • 39
0

You can get/change network connection settings through AndroidDriver. However, it works only for Android version less than 5.

AppiumDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("..."), caps);
NetworkConnectionSetting networkConnection = new NetworkConnectionSetting(false, true, false);  // airplane mode, wiif, data

networkConnection.setData(true);  // enable mobile data
networkConnection.setWifi(false); // close wifi

((AndroidDriver<WebElement>)driver).setNetworkConnection(networkConnection);
networkConnection = ((AndroidDriver<WebElement>)driver).getNetworkConnection();
Fayez Alrafeea
  • 181
  • 1
  • 2
  • 6
0

This ADB command will certainly switch off your wifi :

adb shell am broadcast -a io.appium.settings.wifi --es setstatus disable

To turn it on use :

adb shell am broadcast -a io.appium.settings.wifi --es setstatus enable

OR

Try this code -

self.driver.open_notifications()
self.driver.find_element_by_xpath('//android.widget.Switch[@content-desc="Airplane mode"]').click()
self.driver.back()

Give me a thumps up if it works for you

Shivam Bharadwaj
  • 1,864
  • 21
  • 23