Appium - open in browser - android

Testing mobile application using appium. When I run to test, my application opens, I can do some action, and then when I login, app takes me to browser to login to the cloud which is OK, but my problem is in the emulator. I have a question that pops-up, Open with Chrome or other app (JUST ONCE / ALWAYS). How can I avoid this dialog?
added below code but this does not help
caps.setCapability("appPackage", "com.android.browser");
I expect that when app switch me to browser then I can paste my credentials and switch back to my application and I can continue my testing

I experienced similar popups too when testing apps with Appium. The only solution that works for me is extracting the xpath (UIAutomatorViewer) and perform a click on the popup. There must be a OK or cancel button somewhere. (I do this for app crash popups, chrome privacy stuff, ...)
I perform something like:
if(element.isDisplayed()){
element.click();
}
//proceed with your test
It is not optimal but it works. Remember if you would test this app manually you would also have to click the popup away, so I think this is OK.

You can trigger KeyEvent to put apps in the background and see the home screen. Then you search for browser icon and click it. Once in browser, you can use the context commands to automate the webview.
After you done with browser, you can resume the native app.

Sometimes there are some testing that makes us out of the application, Appium provides features for us to switch to the application we want. Please try this method :
driver.startActivity(new Activity("com.example", "ActivityName"));
You must know the name of your application bundle and its main activity.
You can read the documentation here.

Related

Can I build an app that interacts with another app that I don't own?

New here. I still didn't decide which technology I'll use to make this app, but the main feature is basically this:
There's app A and app B (I don't own any of these);
My app (C) needs only one role: when I login on app A it'll logout on app B and when I login on app B it'll logout on app A.
Note: Forcing app A or B to close will not logout or in.
Anyone knows a way to develop this? The app is supposed to be available on Android and iOS, I was planning to use nativescript.
Any help will be appreciated! Thanks,
Diogo
Unfortunately not because every app run in its specific sand box for security reasons. The only way to do something like that it is using broadcasts intents but the apps A and B should already be prepared to respond to a intent to log out and send a broadcast when log in. Probably it doesn't happen.
In iOS, this won't happen as every app is running on different sandbox and there is no relation between two sandboxes.
Thank you for all your answers. Very useful. Can I use an app to close and open others? For example, If I open Spotify it automatically closes YouTube music using my app. This way would only have to do with the smartphone or tablet.

Check if web app is added to home screen on Android

I have a web app and on the Android I would like to display an alert describing how to add my app to the home screen. (Add it to "Bookmarks" and then "Add it to home screen" or "Add to shortcut in Home"). Then a icon will be displayed on the screen that opens my app.
But off course I only want this to show if the app is not added to the home screen.
Does anybody know how to do this?
Any input appreciated, thanks.
Yes, you can.
While technically a page open in Chrome browser tab can't directly check whether a home screen shortcut exists, the page's local data (localStorage, IndexedDB) is shared between home screen instance and browser tabs, so this can be used to communicate the existence of the home screen version.
Detect if the app is running from home screen
If it's ran from home screen, save this fact to localStorage
Profit! (by reading this from localStorage in any tab)
When the app is in standalone view (which is possible only when launched from home screen), the CSS media query (display-mode: standalone) will match. In Javascript you can read it using:
matchMedia('(display-mode: standalone)').matches
(BTW: the non-standard iOS equivalent of this is navigator.standalone, but iOS doesn't share state between home screen and Safari, so you're out of luck there).
However, instead of custom instructions I suggest meeting Chrome's criteria for "progressive web app" and let Chrome do the prompting for you.
You can't check a web app if its added to home screen on android. At least for now. (Chrome 67)
But you can tell if the app is running in standalone mode using display-mode media query.
Add this inside your <style> tag.
#media all and (display-mode: standalone) {
body {
background-color: yellow;
}
}
Then in your <script> tag.
if (window.matchMedia('(display-mode: standalone)').matches) {
console.log('display-mode is standalone');
}
Or this
if (window.navigator.standalone === true) {
console.log('display-mode is standalone');
}
You can check more from here.
The short answer is: from a Web Site you can't.
The longer answer is: from a Web site you might be able to get a hint in Chrome.
Chrome on Android two new features 1) Web App Manifest that describes what should be launched from the home screen and how it should look on the homescreen, and 2) Chrome now has an beforeinstallprompt event that will trigger for Web apps that we think are app-like and can be installed to the homescreen.
There are a number of criteria for the onbeforeinstallprompt event to fire which might make it an "ok" heuristic (although I suspect not).
The event only fires if:
The site has a manifest, is on https and has a service worker. (this can be quite a stretch).
The user has engaged with the site multiple times (right now, twice within at least 5 minutes).
The user has not already added your site to the home-screen.
So, in summary it is complex and full of false positives and false negatives. However if all you want to do is detect if you should display a banner to prompt the user to add your web-app to the homescreen then Chrome already has a solution for you.
We also have a full range of samples on our samples site.
I think you can do it. Simply add query string to start_url in manifest.json and in your javascript check if start url is having that query string. If query string is found then yeah app is installed.
first you get the list of apps on the device
List<ApplicationInfo> packs = pm.getInstalledApplications(0);
then you use getLaunchIntentForPackage()
Now that you've got the list of packages installed on your device,
iterate through them and call getLaunchIntentForPackage() on each
item.
If a valid intent is returned, it exists on the Launcher, else if null
is returned, the package does not launch from the Launcher screen.
Note that Home screen shortcuts are a subset of the Launcher apps.

Why doesn't FLAG_ACTIVITY_NO_HISTORY work on device, but works on emulator?

I am trying to integrate with the LinkedIn api within my app. Most of it is working just fine but for 1 issue. This is how it works:
From my apps settings screen, the user clicks on connect to LinkedIn button which brings up the web view where the user has to enter their linkedin credentials. The web view is launched the following way:
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(liToken.getAuthorizationUrl()));
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
The above is actually done via an asynctask class.
After successful auth from linkedin, the web view goes away and the user comes back to my apps settings page. Here the user has 2 options. He can either click on save or can hit the cancel button. Save button saves the settings and then takes them to the main activity, cancel doesn't save anything and then takes them to the main activity.
The problem is, this all works just fine on 4.2.2 emulator, but on a Google LG Nexus4 device running 4.2.2, hitting on either the save or the cancel button brings back the linkedin webview, which is not the desired behavior.
One thing that I wanted to point out was that the emulator uses the default android browser while nexus4 uses chrome? is that the problem here?
Do I need to set some other flag on the intent?
Have you tried cleaning your project and rebuilding? Delete the app from the device and reinstall?

Clear Android Browser History

I am writing an application for a client that will have several devices that are open to customers to look at and play with. They want to be able to clear the browser history on a regular basis so that if a customer opens the browser to an inappropriate web site the next customer to come along will not see this.
I am currently using this to clear the history and searches:
Browser.clearHistory(getContentResolver());
Browser.clearSearches(getContentResolver());
This correctly clears the history. But any windows(tabs) that were open in the browser remain open. How can I tell the browser to close all of these tabs so that the next time someone opens the browser it will load the start page only?
I've noticed that killing the browser with the TaskManager that comes pre-loaded on the device works. Is killing the browser task the only way that I can get it to close any open tabs? If so how can I go about killing/restarting the browser process? I've tried this:
am = (ActivityManager) getApplicationContext().getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses("com.android.browser");
But this method doesn't seem to be doing anything. I do have
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
In my manifest but after I run this and go back to the browser it is still sitting on whatever tabs were left open. What is the proper way to use the permission that is granted by this?
Id rather find some way other than Task Kill to make it work, but at this point I am coming up empty on all fronts.
If your device is rooted, you can use the command below, to clear all data of the application. I don't know if this is applicable for each device, as it depends on the package manager
Runtime.getRuntime().exec("su -c /system/bin/pm clear com.android.browser");
I tried to close the browser via the ActivityManager and it works fine. I tested this on an HTC 2.2 and it works perfect.
Maybe you should also try this:
am.killBackgroundProcesses("com.google.android.browser");
in case the browser on your phone is not at com.android.browser
or even try to finish the activity: com.android.browser.BrowserActivity
Hope this helps!
Much like a computer browser, Android browsers will keep all records that you search onto the device. If you’re worried about browser history or don’t want any records in your Android, you can remove all browser history within minutes. After delete your browser records will delete all cache data where you’re login into Chrome.

Android OS - Stop user from closing app or using any OS functionality

My team is trying to build an Android application for a tablet that will be dedicated for this sole purpose. One of the requirements is that the application is the only thing running on the device (at least from the user's point of view). The user should not be able to close it or use any other functionality from the OS (settings, other apps, etc.).
We have been doing some research and so far have not found anything. Is this we are trying to do even possible ? Does anybody have any idea how we could approach this ? Maybe blocking the buttons ?
Thanks,
It is possible but would be ugly in the long run without a custom built rom (http://xda-developers.com has instructions on how to do this), I wouldn't know where to start code wise - but there are a couple of applications which portray this kind of functionality - TodlerLock is one such app - its designed to stop todlers from from doing anything on the device, whilst the application keeps them entertained. It appears to intercept all button presses and acts as a home application to do this for the home button.
Then there are the programs like estrongs security manager that allows a user to set a password for some or all applications and basically stops the application being run without the password (it appears to intercept the intents and opens the security manager requesting the password first, if it fails it finishes the intent) - this maybe a much easier option to use something like this - you could set a password for most things, including settings and the security application itself, everything bar your application. This will stop the user doing anything you dont want them doing without the password.
You would have to set up the device for the user before hand though, as any home screen intercepter application can be changed but the user - so you would have to set the default, then lock access to the settings so the user can't change this action.
I think the only way of doing this to have your own custom version of Android built only for you. Get the android source, remove what ever you don't want and build it. I am not sure if you would ever want not to close it but you can make sure they can't install anything else on your ROM.
Im not sure about this, but, intercept the home intent and write your own custom home?
It sounds a lot simpler than writing your own custom rom.
This is not possible without OS changes. You cannot override the home button.

Categories

Resources