Load a Config file on an Android app using Appium with Java - java

I have been searching for a long time to find a way to import a config file on an Android app using Appium with Java. I am still learning on this and I hope that I find some help here.
Here is the case:
I have an android app that I started testing but before that I have to import a configuration file into this app in order to become active.
public class sipphone extends base {
public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
AndroidDriver<AndroidElement> driver=capabilities();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
File configImport= new File("src");
File file= new File(configImport, "testconfig.config");
driver.findElementById("android:id/button1").click();
driver.findElementById("android:id/btn_later").click();
try{
//import file from src directory
}
catch (Exception e) {
return;
}
}
}
How can I load the file inside the try block and then into the Android App?

Please check
https://appium.github.io/java-client/io/appium/java_client/android/PushesFiles.html#pushFile
Search on http://discuss.appium.io/ or https://github.com/appium/appium on how to use it

Related

"Image not valid, but TextSearch is switched off!" error - Sikuli on Mac

I am trying to automate a desktop application on Mac using Sikuli and Eclipse.
Source code:
import org.sikuli.script.FindFailed;
import org.sikuli.script.ImagePath;
import org.sikuli.script.Screen;
public class TextEditorExample {
public static void main(String[] args) throws FindFailed {
// TODO Auto-generated method stub
Screen s=new Screen();
System.out.println(ImagePath.getBundlePath());
s.click("spotlight_icon.png");
s.find("spotlight.png");
s.type("spotlight.png","finder");
s.click("applications.png");
s.click("texteditor_icon.png");
s.find("texteditor.png");
s.type("texteditor.png","Sikuli Example");
}
}
But I'm getting the following error :
[error] Image: Image not valid, but TextSearch is switched off!
[error] RunTimeAPI: Wait: Abort: unknown
[error] RunTimeAPI: ImageMissing: spotlight_icon.png
Path of sikuli script:
/Users/adamin/Desktop/Automation/SikuliExample/src/TextEditorExample.java
Path of Images:
/Users/adamin/Desktop/Automation/SikuliExample/src/spotlight_icon.png
/Users/adamin/Desktop/Automation/SikuliExample/src/spotlight.png
/Users/adamin/Desktop/Automation/SikuliExample/src/applications.png
/Users/adamin/Desktop/Automation/SikuliExample/src/texteditor_icon.png
/Users/adamin/Desktop/Automation/SikuliExample/src/texteditor.png
Can anybody help me in solving this issue?
The imagepath is set by default to your project root folder and will only look for patterns there. Just set the bundle path manually to wherever your files are:
ImagePath.setBundlePath("fullpath");
Alternatively, place your files to whatever folder that is returned by:
System.out.println(ImagePath.getBundlePath());
Use Pattern.
Pattern pattern = new Pattern(path+"spotlight_icon.png");
Screen s=new Screen();
try {
s.click(pattern);
} catch (FindFailed e) {
e.printStackTrace();
}
This Error most probably comes when image is not loadable, Meanwhile, use this approach
try{
String path = "path of your image";
Pattern target = new Pattern(path);
Screen scr = new Screen();
scr.click(target);
}
catch(Exception e)
{
e.printStackTrace();
}
Y

Java-simple-serial-connector trows exception UnsatisfiedLinkError

I downloaded the sources from here. This is the code with wich I am trying to open com port 13.
I saw in the device menager that com port13 is present.
import jssc.*;
public class Main {
static boolean s = false;
public static void main(String[] args) {
// TODO Auto-generated method stub
SerialPort ser = new SerialPort("COM1");
try {
s = ser.openPort();
} catch (SerialPortException e) {
// TODO Auto-generated catch block
System.out.println("Riko Stana neshto");
//e.printStackTrace();
}
//System.out.println("Riko" + ser.getPortName() );
}
}
When ran in Eclipse this appears in the console:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jssc.SerialNativeInterface.openPort(Ljava/lang/String;Z)J
at jssc.SerialNativeInterface.openPort(Native Method)
at jssc.SerialPort.openPort(SerialPort.java:158)
at Main.main(Main.java:9)
The linbrary consist of java files and two dll file. I linked the java files to my eclipse project, but not sure how they are linked to the dll files. Should I do something in order to link the .java files to the .dll files? Could it be the cause of my problem? Any help is very much appreciated.
Download the JSSC zip here: https://code.google.com/archive/p/java-simple-serial-connector/downloads
Extract jssc.jar in your project folder.
Add the "jssc.jar" to your classpath as a .jar library.
Optional: Extract javadoc/jssc-2.7.0-javadoc.jar and javadoc/jssc-2.7.0-src.jar in your project folder and add them as javadoc and sources respectively.
Problem solved.
Edit - pitcure:

loading jar at runtime and using its class

i have one problem in which i need some help.
Problem statement:
I'm using one jar to generate reports in excel sheet format. This jar is required only if the user wants to generate report in excel format. Other formats of report available are html and txt which don't require this jar.
The current user generates the reports in html format so he says, why should I download this jar and export it in the classpath when I don't need report in the excel format.
Now the problem is if this jar is removed, this build will fail/as all the imports to the classes which are being used will give error. Class.forName can load the class at run-time and doesn't give me error but with this I will not be able to use the method of that class since I cannot have the reference of the class.
Is there any way out or this is not possible?
Did you try to compile it with the jar as a dependency for the compile.
Then at runtime, you will have a part where you check if the jar is needed and if so you can dynamically get the jar and load it like so (Code does not work like this of course ;) ):
import java.lang.reflect.Method;
import java.net.URLClassLoader;
Method addURL = null;
try {
addURL = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[]{URL.class});
} catch (Exception e1) {
//Log error
}
addURL.setAccessible(true);
//Maybe download the file or check if file exist else give out error and end processing
File yourJar = new File(filePath+"/"+fileName+".jar");
//Replace Your.Main.Class with your main class
addURL.invoke(Your.Main.Class.class
.getClassLoader(), yourJar.toURI().toURL());
// Your class should now be loaded and no more ClassNotFound exception should occur when it is accessed, but not if it is accessed before!
The problem is that you are hard wiring your dependencies. So your code needs to do some imports for the third party libs. What you need is to loosely couple the third party libs so that the core of you application does not need to import anything related to 3rd party libs. Use an interface which defines a method or the set of methods needed to generate reports in any format. Make this interface part of your core application. Format specific implementation goes then in separate modules which are dependent on your core application and on the 3rd party libs. Use a factory in the core application to load the specific implementation at runtime using refelction. If a format is requested from which the relevant module jars are not present in the classpath, a ClassNotFoundException will be thrown, catch it and handle accordingly.
Here a sample structure for your application
Core application
class ReportData {
}
interface ReportGenerator {
byte[] generate(ReportData data);
}
class ReportGeneratorFactory {
public ReportGenerator getInstance(String format)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
ReportGenerator reportGenerator = null;
if("txt".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.TxtReportGenerator").newInstance();
} else if("html".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.HtmlReportGenerator").newInstance();
} else if("xl".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.XlReportGenerator").newInstance();
} else {
throw new UnsupportedOperationException(
String.format("Unsupport format %s", format));
}
return reportGenerator;
}
}
Txt / Html Export (Could be part of the core application if no 3rd party lib are needed)
class TxtReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
class HtmlReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
Module (own jar) for XL report (depends on your core application and on the 3rd party lib)
class XlReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
Usage:
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
byte[] report = new ReportGeneratorFactory()
.getInstance("xl")
.generate(new ReportData());
}

Extracting Text From JPG

I've tried this code and added the needed jar files but still I'm getting an error message like Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302'.
Is there a complete tutorial how to extract text and what things should be done to address the error? Any help is appreciated...
import net.sourceforge.tess4j.*;
import java.io.File;
public class ExtractTxtFromImg {
public static void main(String[] args) {
File imgFile = new File("C:\\Documents and Settings\\rueca\\Desktop\\sampleImg.jpg");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(imgFile);
System.out.println(result);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
In addition to adding the jars, you also need to add the natives. You can do so with Djava.library.path="C:\[absolute path to dir containing *.dll files and such]"
Note that you need to provide the directory, not the file itself.

Android: Can't find my package folder to put the database file in it?

I am new in using File Explorer in eclipse as well as Android.
I have created a database using SQLite and added few data. The database is named dbtaxi.
My package name is com.syariati.finalProject
Then I saved this device database onto my desktop to help me check its content using SQLite Manager, firefox addons. Then I have added some data using SQLite Manager, which is possible is causing the problem.
My requirement is to replace database in my package (com.syariati.finalProject/Databases/dbtaxi) with dbtaxi on my Desktop. But, I can't find the path com.syariati.finalProject.
Need help! Thank you.
Your db is in a private folder "data/data/com.syariati.finalProject/databases/dbtaxi". You cannot access that folder without rooting the phone.
Another option is to copy the db to that folder programatically.
import java.io.*;
import java.nio.channels.*;
public class FileUtils{
public static void copyFile(File in, File out)
throws IOException
{
FileChannel inChannel = new
FileInputStream(in).getChannel();
FileChannel outChannel = new
FileOutputStream(out).getChannel();
try {
inChannel.transferTo(0, inChannel.size(),
outChannel);
}
catch (IOException e) {
throw e;
}
finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
public static void main(String args[]) throws IOException{
FileUtils.copyFile(new File(args[0]),new File(args[1]));
}
}
It's in "/data/data/com.syariati.finalProject/databases".
/data/data/com.syariati.finalProject/databases
.
.
.
.

Categories

Resources