Unable to toggle on and off location service using Appium - appium

I need to enable and disable location service in Android before my test. I have tried the following solutions but nothing worked at all.
Scenario : 1
ADB commands
adb shell settings put secure location_providers_allowed +network,gps
adb shell settings put secure location_providers_allowed -network,gps
These commands working at times but not all the times so my test fails lot of times for disabling.
Scenario : 2
toggleLocationService() Method
public static void enableAndDisableLocation(AppiumDriver<WebElement> driver) {
((AndroidDriver<WebElement>) driver).openNotifications();
((AndroidDriver<WebElement>) driver).toggleLocationServices();
}
Are there any other solution to enable and disable Location service in Android using Appium. Also I want to know the status of the Location before enabling and disabling.
I am using Java client 5.0.4
Testing in Android Nougat.

To Enable:
adb shell settings put secure location_providers_allowed +gps
adb shell settings put secure location_providers_allowed +network
To Disable:
adb shell settings put secure location_providers_allowed -gps
adb shell settings put secure location_providers_allowed -network

Related

How do I make sure ports 8200..8299 for UIAutmator2 are free'd up after killing Appium?

I'm running Appium 24/7 on a Windows machine to perform automated tests. I'm running these tests via AVD on an emulator with Android 10. For my test, I give the correct Capabilities and runs functionally great. But after a few hours/days I get the follow error:
UnknownError: An unknown server-side error occurred while processing the command. Original error:*Cannot find any free port in range 8200..8299}. Please set the available port number by providing the systemPort capability or double check the processes that are locking ports within this range and terminate these which are not needed anymore at getResponseForW3CError at asyncHandler.
When my code is executed, I make sure the finally in try executes the AppiumDriverLocalService.stop(); method to kill Appium. My feeling says this does somehow not free up any ports for UIAutomator2 (which you can configure, but I have not configured, through systemPort capability.
} finally {
driver.closeApp();
service.stopServer();
System.exit(0);
}
Why does Appium not free up any ports for UIAutomator2 in the range 8000-8299 after stopping the service?
Android Debug Bridge version 1.0.40
Appium version 1.20.2
Android 10
PS I know of the adb command adb -s $UDID forward --remove-all but does this solve the real issue, I don't want to execute a Java Process each time
Update - Fix 2-3
I have fixed the issue by changing the finally clause to execute the following methods in order:
} finally {
AndroidDriver.quit();
AppiumDriverLocalService.stopServer();
In order to free up the port 8200..8299, AndroidDriver must be quit as UIAutomator2 is linked to this. The AppiumDriverLocalService will kill/forward/free this port and UIAutomator2, after this I can freely stop the server:
[ADB] Removing forwarded port socket connection: 8203
This issue is a good example of your problem. As you can see, Appium team is closing it as a Not A Bug.
Reason is obvious: Appium is not responsible for managing ports, it relies that your system has some free in required (by Appium code) range.
systemPort is used to connect to appium-uiautomator2-server, by default is 8200. Basically Appium selects one port from 8200 to 8299 for appium-uiautomator2-server. However, when you run tests in parallel, you must adjust the port to avoid conflicts. If you are not, you should be safe in 99% cases with the default.
Restart the system (assuming it will clear ports) and run tests. Check Appium server logs for any errors on closing session. It could be that issue is related to ADB setup or permission issue, and UI2 server is not stopped properly.
If so, port remains in use. And it is not Appium to deal with cleanup. I'm not sure that adb command you mentioned is a good 100% guaranteed way to do it.
I would just add a shell script that is executed before Appium server start to clean ports in 8200-8299 range and forget this problem.
killall -9 node - to kill all appium running internally and at root level
and adb kill-server - to kill the abd server by running this it resolved the above issue for me , hope may help someone

Send command line argument/flag like '--relaxed-security' to saucelabs while initiating an appium session

I want to be able to execute certain adb shell commands on real devices in saucelabs.
I learned from https://wiki.saucelabs.com/display/DOCS/2019/02/13/Enhance+your+Appium+tests+on+emulators+with+ADB+commands that I can make use of '--relaxed-security' flag while initiating an appium session on saucelabs to execute adb shell commands.
While it is easier to pass command line arguments while starting appium on local machines, I'm not sure how to include the flag while connecting to saucelabs.
This is how I'm initiating a session on saucelabs :
desired_capabilities = {
"deviceName": "xxx",
"platformName": "xx",
"platformVersion": "xx",
"bundleId": "xxx",
"automationName": "uiautomator2",
"testobject_api_key" : "xxx"
}
driver = webdriver.Remote("https://us1-manual.app.testobject.com/wd/hub", desired_capabilities)
I assume it is enabled by default, you can double check your project to have Instrumentation option enabled.
Since it is a server flag, you can't pass it from client side and I don't see a way to do it via capability.
Try to run adb command and if it won't work - ask their support.
This has not been enabled in the Real Device Cloud of Sauce Labs so it will not work in this case
Update 20210825:
We've discussed the feature "ADB support for Android Real Devices" with our Product Team and they will investigate this from a technical perspective to see if this will be feasible. I'll keep you posted

How to run automation scripts on remote Appium server using public IP?

I have deployed automation script in remote ubuntu-server and i want to execute those script on my local system where appium server is running.I have googled for the answer but all the solutions work only if both the device are connected to same wifi network
Please suggest me the solution to run the script in my local system where Appium server exist.
Where is the device - is It connected at ubuntu-Server?
Where is appium server running?
Where are the test scripts?
I have tried in this way and it worked.
Test scripts are available in System-1
Appium Server & Devices are available in another system. Where devices are connected through a physical cable.
With this approach, I am able to run the scripts remotely from my machine.
Observations: We can try out through accessing devices through WIFI (adb wifi google it). But if wifi fluctuates then appium session will be terminated. To avoid all these go with the physical usb connection.

Unable to see the device list when running the adb devices on cmd

getting below exception when opened the genymotion emulator.
Note : Set the path of sdk in genymotion. and adb version is also latest.and also unable to kill the process adb on task manager. three adb processes are running.
adb server is out of date. killing...
cannot bind 'tcp:5037' ADB server didn't ACK
*failed to start daemon* error:
You just need to do following task :
1) Open genymotion settings
2) Specify sdk path for the adb manually
3) adb kill-server
4) adb start-server
Solution was if we are running command prompt by admin rights, then it is working fine,Thanks for your valuable inputs.

React native not running on localhost

React native stopped working, whenever I run it now (no matter which project) I get the error could not connect to development server |
ensure the following
node server is running and available on the same network - run npm start from react native root.
Node server url is correctly set in appdelegate
URL : http://localhost:8081/index.ios.bundle? platform=ios&dev=false
Please ensure you have done the following
1) run server using
npm start
2) if you are using actual device , make sure you are connected to same network
3) for android: run the following command and try again
adb reverse tcp:8081 tcp:8081
I was face same problem while running in physical device.
I did as below:
Open terminal and check your local ip address.
Shake device(android) click on dev settings
Click "Debug server host & port for device" under debugging
type local ip address and port number.
note: cross check in browser ip along with port number.
I usually just run npm start in my terminal. (OSX) I assume it would be the same on Windows
npm start
To debug on a real device:
On iOS - open the file AppDelegate.m and change localhost to the IP address of your computer. Shake the device to open the development menu with the option to start debugging.
let jsCodeLocation = NSURL(string: "http://{{YOUR IP ADRESS}}:8081/index.bundle?platform=ios")
On Android, you can open dev menu on the device and select Dev Settings, then update Debug server host for device setting to the IP address of your computer.

Resources