Switch between Android Driver and iOS Driver in a single test (Appium) - android

I'm just trying to get a single test for both of Mobile Operative System
I'm using Cucumber as test framework for example that feature:
#test
Scenario Outline: Test app
When Start Test "<Platform>"
Then Do the test
Examples:
| Platform |
| Android |
| IOS |
Calls to my Test.java:
public class Test {
public <typeofvariable> driver;
#When("Start Test {string}")
public Start_test (String platform){
if (platform.equals("Android"))
{
driver = new InitAndroidDriver();
}
else { driver = new InitIOSDriver();}
}
#When("Do the test")
public Do_the_test{
driver.context("NATIVE_APP");
}
}
But I don't know what type of variable needs to be "driver" to accept AndroidDriver and also IOSDriver, I tried putting AppiumDriver in but It not works for me, because in the "Do the test" section I need to switch between context and AppiumDriver dont seems to have that function.
Appreciate the help.

I found the answer by myself, it's a little bit tricky, but you would want it to implement that!
public class Test {
public AppiumDriver driver;
#When("Start Test {string}")
public Start_test (String platform){
if (platform.equals("Android"))
{
driver = new InitAndroidDriver();
}
else { driver = new InitIOSDriver();}
}
#When("Do the test")
public Do_the_test{
switchContext(driver,"NATIVE_APP",platform);
//Some test here
switchContext(driver,"WEBVIEW",platform);
}
public static void switchContext (AppiumDriver driver, String context, String platform) throws InterruptedException {
Thread.sleep(2000);
int context_position = 0;
if (context.equals("NATIVE_APP")) {context_position = 0;} else {context_position = 1;}
if (platform.equals("Android"))
{
Set<String> contextNames = ((AndroidDriver)driver).getContextHandles();
for (String contextName : contextNames) {
System.out.println("Contextos disponibles Android: " + contextName);
}
System.out.println("Contexto seleccionado: " + contextNames.toArray()[context_position]);
((AndroidDriver)driver).context((String) contextNames.toArray()[context_position]);
}
else {
Set<String> contextNames = ((IOSDriver)driver).getContextHandles();
for (String contextName : contextNames) {
System.out.println("Contextos disponibles IOS: " + contextName);
}
System.out.println("Contexto seleccionado: " + contextNames.toArray()[context_position]);
((IOSDriver)driver).context((String) contextNames.toArray()[context_position]);
}
}
}

Related

Programmatically Auto Accept Incoming Bluetooth Files

I am trying to find a way to have a tablet basically auto-accept/give permission to accept an incoming Bluetooth Share file transferred from a laptop to my Nexus Android device. Since the Android bluetooth system itself does not support this feature, I was wondering if this could be done programmatically using an Android application that listened for the notification and accepted it for me.
MUCH EASIER WAY
If you have a rooted device and use XPosed Framework, your goal can be achieved much easier.
You Need not implement your own bluetooth server nor kill the original BT service, which are very bothering!!!
xposed tutorial link.
Try this code.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppManager", lpparam.classLoader, "isWhitelisted", String.class,new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
param.setResult(true); /* you can compare the sender address(String) with your computer and determine if you return true or just allow the original method to be called after this returns.*/
}
});
}
}
I tested and it works fine:)
Links
Dropbox link of the auto accepting app
Dropbox link of the project files (zip)
Xposed apk site
Towelroot site to root your phone
Background(Original answer)
As I commented above, you bay be able to, and I tried and succeeded in blocking (though not receiving) with this code.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import java.io.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppService", lpparam.classLoader, "startSocketListener", new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
param.setResult(null);
}
});
}
}
The code above hooks the method startListenerSocket() of com.android.bluetooth.BluetoothOppService and prevents the original method from being called by the line param.setResult(null);
Refer to here to see the full code of com.android.bluetooth.BluetoothOppService.java and you will understand the operation.
And the code you can start from is shown below.
import android.util.*;
import de.robv.android.xposed.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial implements IXposedHookLoadPackage
{
private String TAG="TUTORIAL";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.bluetooth"))
{
Log.i(TAG,"Not: "+lpparam.packageName);
return;
}
Log.i(TAG,"Yes "+lpparam.packageName);
findAndHookMethod("com.android.bluetooth.opp.BluetoothOppObexServerSession", lpparam.classLoader, "onPut", new XC_MethodHook() {
#Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.v(TAG,"HOOK DONE");
Class c=param.thisObject.getClass();
}
});
}
}
This code hooks the onPut method of com.android.bluetooth. BluetoothOppObexServerSession linked here. I either am newbie to xposed framework but I hope my answer helped.
I had the same issues you asked and partially solved the problem by implementing my custom OBEX server and manually / programmatically(with ps|grep and su kill pid) killing the native BluetoothOppService. But I will either try the idea of hooking and directly executing my code.
And to help you customize OBEX server session I post my implementation below.
#Override
public int onPut(Operation op)
{
if (D)
{
Log.d(TAG, "onPut " + op.toString());
}
HeaderSet request;
String name, mimeType;
Long length;
String extension=null;// type;
int obexResponse = ResponseCodes.OBEX_HTTP_OK;
String destination;
if (mTransport instanceof BluetoothObexTransport)
{
destination = ((BluetoothObexTransport) mTransport).getRemoteAddress();
}
else
{
destination = "FF:FF:FF:00:00:00";
}
boolean isWhitelisted =IsWhitelisted(destination);
try
{
boolean preReject = false;
request = op.getReceivedHeader();
if (V)
{
// Constants.logHeader(request);
}
name = (String) request.getHeader(HeaderSet.NAME);
length = (Long) request.getHeader(HeaderSet.LENGTH);
mimeType = (String) request.getHeader(HeaderSet.TYPE);
if (length == 0)
{
if (D)
{
Log.w(TAG, "length is 0, reject the transfer");
}
preReject = true;
obexResponse = ResponseCodes.OBEX_HTTP_LENGTH_REQUIRED;
}
if (name == null || name.isEmpty())
{
if (D)
{
Log.w(TAG, "name is null or empty, reject the transfer");
}
preReject = true;
obexResponse = ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
int dotIndex = name.lastIndexOf(".");
if (dotIndex > 0)
{
extension = name.substring(dotIndex + 1).toLowerCase();
}
// Reject policy: anything outside the "white list" plus unspecified
// MIME Types. Also reject everything in the "black list".
// if (!preReject && (mimeType == null || (!isWhitelisted && !Constants.mimeTypeMatches(
// mimeType, Constants.ACCEPTABLE_SHARE_INBOUND_TYPES))
// || Constants.mimeTypeMatches(mimeType,
// Constants.UNACCEPTABLE_SHARE_INBOUND_TYPES))) {
// if (D) {
// Log.w(TAG, "mimeType is null or in unacceptable list, reject the transfer");
// }
// preReject = true;
// obexResponse = ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE;
// }
if (preReject && obexResponse != ResponseCodes.OBEX_HTTP_OK)
{
// some bad implemented client won't send disconnect
return obexResponse;
}
}
catch (IOException e)
{
Log.e(TAG, "get getReceivedHeaders error " + e);
return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
}
int status = receiveFile(destination, name, extension, length, op);
/*
* TODO map status to obex response code
*/
if (status != BluetoothShare.STATUS_SUCCESS)
{
obexResponse = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
Log.d(TAG, "MIME TYPE)" + mimeType);
return obexResponse;
}
I just removed some rejecting codes from the original one.
Also to look at my full code please refer to my git repository.
I also thank the contributors to the android project!

Email not sending in unity android?

I have looked many source and question but I am still not clear to how this code of sending email through unity is not working in android build. It works fine in windows build but doesn't work in android. Can someone help me. Here is the code -
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class EmailUnity
{
public static string SenderEmail;
public static string SenderPassword;
public static string SmtpClient;
public static int SmtpPort;
public static void SendEmail(string to, string subject, string body, bool isHtml, string[] attachmentPaths,
Action<object, AsyncCompletedEventArgs> callback = null)
{
try
{
SmtpClient emailServer = new SmtpClient(SmtpClient, SmtpPort);
emailServer.EnableSsl = true;
emailServer.Credentials = (ICredentialsByHost) new NetworkCredential(SenderEmail, SenderPassword);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
MailMessage message = new MailMessage(SenderEmail, to);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = isHtml;
foreach (string path in attachmentPaths)
{
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
message.Attachments.Add(new Attachment(path));
}
}
if (callback == null)
{
callback = SampleCallback;
}
emailServer.SendCompleted += new SendCompletedEventHandler(callback);
emailServer.SendAsync(message, "");
Debug.Log("Email sending");
}
catch (Exception ex)
{
Debug.Log("Error: " + ex.Message);
callback("", new AsyncCompletedEventArgs(ex, true, "Exception occured"));
}
}
private static void SampleCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled || e.Error != null)
{
Debug.Log("Error: " + e.Error.Message);
}
else
{
Debug.Log("Email sent");
}
}
}
These are the things to try if SmtpClient is not working on Android.
Go to File --> Build Settings... --> Select Android. Now, click on Player Settings.
1.On the Internet Access, change it from Auto to Require.
2.Make sure that API Compatible Level is set to .NET 2.0 not .NET 2.0 Subset.
3.Make sure that Stripping Level is set to Disabled.
4.Go to Player settings --> Android and change Internet Access from Auto to Require

Appium Android Multiple Test case, How does it work?

I am trying to use Appium to automate a test case on my app.
I managed to run a simple script, but I do NOT understand the logic of of the multiple testcases running process like android life-cycle.
What is the cycle for a testcase?
Because when I run the code below it does not run in order of: firstTest, secondTest, thirdTest...
How do we tell the testCase what to run first and in what order ? thanks
public class LoginTest {
AndroidDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.VERSION, "5.0.2");
capabilities.setCapability(CapabilityType.PLATFORM, "Android");
capabilities.setCapability("app-package", "com.myapp"); //Replace with your app's package
capabilities.setCapability("app-activity", ".myapp"); //Replace with app's Activity
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void firstTest() throws InterruptedException
{
List<WebElement> textFieldsList = driver.findElements(By.className("android.widget.EditText"));
int size = textFieldsList.size();
textFieldsList.get(0).sendKeys("test#test.com");
textFieldsList.get(1).sendKeys("12345");
Thread.sleep(1000);
WebElement btnLogin=driver.findElement(By.name("Login"));
String login = btnLogin.getText();
Assert.assertTrue(login.contains("Login"));
System.out.println(login);
btnLogin.click();
Thread.sleep(1000);
}
#Test
public void secondTest() throws InterruptedException {
WebElement btnHome=driver.findElement(By.name("Home"));
String login_1 = btnHome.getText();
Assert.assertTrue(login_1.contains("Home"));
System.out.println(login_1);
btnHome.click();
Thread.sleep(1000);
}
#Test
public void thirdTest() throws InterruptedException {
WebElement btnSecond=driver.findElement(By.name("Second"));
String login_2 = btnSecond.getText();
Assert.assertTrue(login_2.contains("Second"));
System.out.println(login_2);
btnSecond.click();
Thread.sleep(1000);
}
#AfterClass
public void tearDown() {
driver.quit();
}
Thank you
Well the answer to this depends on the Test framework you are using to for your Test.
If you are using Junit for your tests, then you might not be able to prioritise them in a user defined order.
On the other end using if you are using TestNG framework, adding parameter to Test annotation would solve your problem. e.g.
#Test(groups = {"checklist1"}, priority = 1, testName = "firstTest", description = "My First Test")
Though I would suggest, you go through and follow this.
You can use dependsOnMethods with #Test annotation to make that flow:
#Test(dependsOnMethods = "methodName")

Pusher for Android Implementation

I'm trying to implement a pusher service in my Android app, doesn't have access to the server just copying from an iOS app previous implementation. Everything works fine in connection process but when subscribe to a private channel the authentication fails with:
"com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: https://authorization_url"
The implementation goes like this:
HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);
pusher.connect(new com.pusher.client.connection.ConnectionEventListener() {
#Override
public void onConnectionStateChange(ConnectionStateChange change) {
if (change.getCurrentState() == ConnectionState.CONNECTED) {
Channel channel = pusher.subscribePrivate(PUSH_CHANNEL, new PrivateChannelEventListener() {
#Override
public void onAuthenticationFailure(String s, Exception e) {
Log.w("PUSHER", "Channel subscription authorization failed");
}
#Override
public void onSubscriptionSucceeded(String s) {
Log.w("PUSHER", "Channel subscription authorization succeeded");
}
#Override
public void onEvent(String s, String s2, String s3) {
Log.w("PUSHER", "An event with name " + s2 + " was delivered!!");
}
}, "my-event");
}
}
#Override
public void onError(String message, String code, Exception e) {
Log.w("PUSHER", "There was a problem connecting with code " + code + " and message " + message);
}
}, ConnectionState.ALL);
UPDATE
I'm sure that the problem is with the authentication, there is a function call in iOS version that set some headers to the channel subscription or something like that:
(void)pusher:(PTPusher *)pusher willAuthorizeChannel:(PTPusherChannel *)channel withRequest:(NSMutableURLRequest *)request;
{
[request addAuthorizationHeadersForUser:self.credentials.user];
}
Im trying to figure out where to add the headers in android, try to add it to the authorizer but nothing change:
authorizer.setHeaders(addMapAuthorizationHeaders());
Any idea of what is the equivalent in Android of that iOS function: willAuthorizeChannel??
Ok solved, it was what I thought, the HttpAuthorizer needed a set of headers that you can set directly when creating it like:
HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
authorizer.setHeaders(MY_AUTH_HEADERS); //a HashMap with the headers
PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);
And with that works fine, in case somebody have a similar problem.
EDIT:
this is how to set the authorization headers. It's a Map set to "Key" "Value" pair for example:
public static HashMap<String, String> getMapAuthorizationHeaders() {
try {
HashMap<String, String> authHeader = new HashMap<>();
authHeader.put("HeaderKey1", "HeaderValue1");
authHeader.put("HeaderKey2", "HeaderValue2");
return authHeader;
} catch (Exception e) {
return null;
}
}
So the pusher config will be like:
authorizer.setHeaders(getMapAuthorizationHeaders());
I've been struggling with this as well... the solution is simple.
First check this out: https://github.com/pusher/pusher-websocket-java/blob/master/src/main/java/com/pusher/client/util/HttpAuthorizer.java
Then implement the abstract interface Authorizer and override the authorize method with your own code and that's it, you get the same thing as on the iOS.
Some snippet to get you started (with a custom constructor):
CustomSocketHttpAuthorizer authorizer = new CustomSocketHttpAuthorizer(ServerComm.API_MAIN_LINK + ServerComm.API_LINK_PUSHER_AUTH, pusherServerAuthTimeStamp, MessageActivity.this);
PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true);;
clientPusher = new Pusher(ServerComm.PUSHER_CLIENT_KEY, options);
clientPusher.connect(new ConnectionEventListener() .....

How to access database using phone gap

I am new to android phonegap. i am storing and retrieving data using native application. i dont know how to display the retrieved data from native to phonegap(HTML)page.
can anyone pls guide me how to access sqlite with phonegap.?
Thanks in advance.
You need to first create a Android plugin for Phonegap through which you will be able to access the native code and hence the native DB like this
public class SqlitePlugin extends Plugin {
private static final String TAG = "SqlitePlugin";
private static final String CREATE_DB_ACTION = "createDatabase";
private static final String SHOW_DB_VALUES_ACTION = "showValues";
#Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.i(TAG, "Plugin Called");
PluginResult result = null;
if (CREATE_DB_ACTION.equals(action)) {
Log.d(TAG, "CREATE_DB_ACTION");
DB _db = new DB(ctx);
_db.insertValues();
}
else if (SHOW_DB_VALUES_ACTION.equals(action)) {
Log.d(TAG, "SHOW_DB_VALUES_ACTION");
JSONObject DBInfo = null;
try {
DBInfo = getDBValuesListing();
} catch (JSONException e) {
e.printStackTrace();
}
result = new PluginResult(Status.OK, DBInfo);
}
else {
result = new PluginResult(Status.INVALID_ACTION);
Log.d(TAG, "Invalid action : " + action + " passed");
}
return result;
}
}
After that Create a sqlite.js file like this
function SqlitePlugin() {
};
SqlitePlugin.prototype.createDatabase = function(successCallback, failCallback) {
return PhoneGap.exec(successCallback, failCallback, "SqlitePlugin",
"createDatabase", [ null ]);
};
SqlitePlugin.prototype.showValues = function(params, successCallback, failCallback) {
return PhoneGap.exec(successCallback, failCallback, 'SqlitePlugin', 'showValues',
[ params ]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("SqlitePlugin", new SqlitePlugin());
});
Import this sqlite.js in your page(index.html) and then finally use the plugin like this
function showValues() {
window.plugins.SqlitePlugin.showValues('showValues',
showValuesSuccessCallBack, showValuesFailCallBack);
}
function showValuesSuccessCallBack(e) {
if (e.Rows.length > 0) {
alert("Success");
for (i = 0; i < e.Rows.length; i++) {
alert("Id = " + e.Rows[i].id);
alert("Number = " + e.Rows[i].number);
}
} else {
alert("No values in Database");
}
}
function showValuesFailCallBack(f) {
alert("Failure");
}
Let me know if this worked out for you
Write an phonegap plugin to pass the data from native side to html (js)
http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android
Well, you probably should use the HTML5 functions to store and retrieve data from a sqlite DB. However, if you are set on doing it with native code you should look at our implementation which was used for older Android devices that don't support sqlite.
https://github.com/cordova/cordova-android/blob/master/framework/assets/js/storage.js
https://github.com/cordova/cordova-android/blob/master/framework/src/com/phonegap/Storage.java

Categories

Resources