Launching app and executing some test cases with Robotium - android-emulator

I am new to Robotium and tried to execute following code to launch an app and perform some functions.
An example would be, launch messaging app on android emulator and send a text message "Hi" to a user "test".
package com.example.android.test;
import com.example.android.NewUserActivity;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class NewUserActivityTest extends ActivityInstrumentationTestCase2<NewUserActivity> {
private Solo solo;
public NewUserActivityTest() {
super("com.example.android", NewUserActivity.class);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Override
public void tearDown() throws Exception {
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
public void sms() throws Exception{
assertTrue(solo.searchText("Messaging"));
solo.clickOnText("Messaging");
assertTrue(solo.searchText("New message"));
solo.clickOnButton("New message");
solo.enterText(0, "Test");
solo.enterText(1, "Hi");
}
}
With this code, Eclipse runs the test cases but I don't see it on emulator. I understand the package here is a dummy one, I want to know If I am doing it wrong?

Test methods that you want to be executed must have the prefix "test", e.g. "testSms".

Related

After run and close i cannot resize my gui anymore

I have been out for a while, not that i am a good programmer.
But when i made an application window in eclipse (new--- other---WindowBuilder--swing designer--application window)
this is the code it generates
import javax.swing.JFrame;
public class AddTournamentToRankingGUI {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddTournamentToRankingGUI window = new AddTournamentToRankingGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AddTournamentToRankingGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 680, 814);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I tried to resize in eclipse, no problem. then i hit run and hit the close button and tried to resize the GUI in eclipse again.
This is where i cannot resize the window. only after a restart i can resize it in eclipse until i run the app again.
I have tried to add code to complete exit the application, but same result.
This is the code i used
import javax.swing.JOptionPane;
import javax.swing.JFrame;
/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame,
"Are you sure you want to close this window?", "Close Window?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
Am i missing something here?
I am stupid... i thought the move cursor was the resize cursor, so that is why it did not work obviously..

PubNub stop publishing

In using PubNub when I do a publish why does it start a continuous loop?
Programs should end once the function is done.
But after the publish message is sent, the publish program continues to run, like it is waiting for something else.
Here is my code
import java.sql.Timestamp;
import java.util.Date;
import com.pubnub.api.*;
import org.json.*;
public class UserRegister {
public static void main(String[] args) throws InterruptedException {
Pubnub pubnub_pub = new Pubnub("pub-c-3192165c-...", "sub-c-7debcf5c-...");
Callback callback = new Callback() {
public void successCallback(String channel, Object response) {
System.out.println(response.toString());
}
public void errorCallback(String channel, PubnubError error) {
System.out.println(error.toString());
}
};
String encMessage="";
JSONObject message = new JSONObject();
try {
System.out.println("user reg");
message.put("CMD", "USER_REGISTER");
message.put("EMAIL", "jabali2#jabali.in");
message.put("PASSWORD", "1123");
message.put("TIMESTAMP", new Timestamp(new Date().getTime()));
encMessage = new MyEncrypt().encrypt(message.toString());
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pubnub_pub.publish("jabali_channel_101", encMessage, callback);
}
}
Shouldnt the program stop once it finishes publishing?
Its not getting stuck in a loop somewhere either. I can see the output in the PubNub console and in my subscriber. Also I can write what ever I want after the publish statement and it continues like a normal program.
Except for the fact that it never ends on its own
Can any one explain what is going on?
You can use:
pubnub_pub.stop();
This will terminate the thread itself after publishing.

Selenium test wont launch Firefox (java with Netbeans)

I have Selenium IDE installed on Firefox, I ran a simple test on it and I exported the test cases to Netbeans under Java/JUNIT4/WebDriver. When I put the code in Netbeans and try to run it, It doesn't launch firefox. I've another simple program that will launch Firefox and go to google and search for cheese but when I try to export a test that I've ran using Selenium IDE, I can't get it to run. I'm not getting any errors and I get "successful build" when I run it, just nothing happens. Here's my code. Thanks
> Blockquotepackage firstpackage;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
//import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;
public class FirstPackage {
private WebDriver driver;
private String baseUrl;
//private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private boolean acceptNextAlert;
public static void main(String args[]){}
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.get("http://google.com");
baseUrl = "https://www.google.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// WebDriver driver = new FirefoxDriver();
System.out.println(driver.getTitle());
}
#Test
public void testGoogleSearch() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("Google");
driver.findElement(By.id("gbqfb")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alert.getText();
} finally {
acceptNextAlert = true;
}
}
}
// TODO code application logic h
> Blockquote
This problem is likely due to incompatible versions of Firefox and Selenium Firefox WebDriver.
My guess is that your program that works (the one that goes to Google and searches for cheese) has a different version of Selenium in its path than the one that NetBeans ends up using for your imported tests from the IDE.
For more information on how to deal with the version compatibility issue, see my answer to this question.
I just ran your code on my machine and it worked as expected. Make sure you're using correct jar files and are correctly mapped in your project.

error when i run my RMI project

what do i have to do when I have this error:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException
I added the path of the bin in JDK in the properties of "MY computer": this one "C:\Program Files\Java\jdk1.6.0_19\bin"
and I entered to run-cmd-
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\src\countrmi
start rmiregistry
and i run the server, so this error appear
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException
Thank u
Consider looking at the Cajo project. It wraps RMI so you don't have to worry about starting rmi registries and the such.
See the example below from one of the Cajo wiki pages
Duck.java
public interface Duck {
boolean looks();
boolean walks();
boolean talks();
}
DuckServer.java
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;
public class DuckServer implements Duck {
public boolean looks() {
System.out.println("hi there!");
return true;
}
public boolean walks() {
System.out.println("waddle waddle");
return true;
}
public boolean talks() {
System.out.println("quack quack!");
return true;
}
public static void main(String args[]) throws Exception { // simple unit test
Remote.config(null, 1198, null, 0); // use cajo port 1198
ItemServer.bind(new DuckServer(), "Donald");
System.out.println("duck server running");
}
}
DuckClient.java
import gnu.cajo.utils.extra.TransparentItemProxy;
public class DuckClient { // try out DuckServer
public static void main(String args[]) throws Exception {
Duck duck = (Duck)TransparentItemProxy.getItem(
"//serverHost:1198/Donald",
new Class[] { Duck.class }
);
System.out.println("looks like = " + duck.looks());
System.out.println("walks like = " + duck.walks());
System.out.println("talks like = " + duck.talks());
}
}
The time has passed, but maybe this helps someone. When you put
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\src\countrmi
you are setting de path to sources files, but yo have to set this to the classes files, in this way
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\build\classes
and start rmiregistry, of course
at least, it worked fine for me.

Not recognising selenium commands in other than Test class and throwing error

I am using Selenium+JUnit+Eclipse
I have 3 classes in 3 packages. Test class as A(in default Package), Activity class as B(In activity package), Repository class as C(in objectRepository package).
If i do all activities in class A then its working fine. But if I separate the activities in class B and calling classB methods in classA then its throwing java.lang.NullPointerException error...
Code for ClassA.java
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
import org.junit.Test;
import activityPkg.ClassB;
public class ClassA extends TestCase {
ClassB objB = new ClassB();
public void setUp() throws Exception
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://www.google.com");
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();}
#Test
public void testA() throws Exception
{
selenium.open("/");
try
{
Thread.sleep(5000);
String result = objB.MethodB();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Code for ClassB.java
package activityPkg;
import com.thoughtworks.selenium.Selenium;
public class RegressionTools {
Selenium selenium;
ObjectRepository objRep = new ObjectRepository();
public String MethodB() throws Exception
{
String value=null;
try
{
selenium.start();
if(selenium.isElementPresent("//input[#name='btnG' and #value='Google Search']"))
{
System.out.println("Element is present");
value = pass;
}
else
{
System.out.println("Element is not present");
value = Fail;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return value;
}
}
But everytime its stopping from IF condition of MethodB and coming to catch block.
Why it's not even entering into IF or ELSE condition.
Did I miss anything there?
you don't have any selenium instance in your class B. I guess the exception you have is NullPointerException
You start your selenium instance in your first class, and got no reference pointing to it in your class B
Good practice is to have a SeleniumFixture utility which holds a reference to the started instance and manage the lifecycle of the selenium server during the test suite