I don't have much experience on android JUnit test. Here I'm doing a simple test application for JUnit.The application contains two activity when the button pressed in the first activity Intent use to start the second activity. But I'm getting two failures as junit.framework.AssertionFailedError. This is the code that I have tried:
public void testLayout(){
buttonId=com.example.androidunittest.R.id.button;
assertNotNull(activity.findViewById(buttonId));
Button view=(Button) activity.findViewById(buttonId);
assertEquals("Incorrect label of the button","Start",view.getText());
}
public void testIntentTriggerViaOnClick(){
buttonId=com.example.androidunittest.R.id.button;
Button view=(Button) activity.findViewById(buttonId);
assertNotNull("Button not allowed to be null",view);
view.performClick();
Intent triggeredIntent=getStartedActivityIntent();
assertNotNull("Intent was null",triggeredIntent);
String data=triggeredIntent.getExtras().getString("URL");
assertEquals("Incorrect data passed via the intent","http://www.google.com", data);
}
The failure line are
assertNotNull("Button not allowed to be null",view); and assertNotNull(activity.findViewById(buttonId)); according to the JUnit test panel result.
Could anyone please explain me what is happening here? Also how to fix the error in the application code?
I have another question, how to run all test class once in eclipse?
What I did is as per this answer , right click on the project -> Run as-> Android JUnit test. But only active class was gave the result.
Any help would be appreciated.
Related
How do I make my Text-To-Speech stop speaking? Right now, my TTS will start speaking when I launch my application, but I'm trying to make it stop speaking when a button on my Main Activity is pressed.
I have tried creating an onStop() method and using .stop, but to no avail.
May I please get some help on this issue?
You could do something along the lines of this ...
stopSpeakingButton.setOnClickListener(new
View.OnClickListener() {
String toSpeak = " ";
textToSpeech.speak(
toSpeaks,TextToSpeech.QUEUE_FLUSH,null);
});
As you gave us no code this a concept , not the most beautiful solution but will work if you adjust for your use case, the idea is to make it read string with onny a space in it!
Tried the login use case with appium for android native app. But button click not working. But I am getting all test passed.Tried with mobile driver also.
#BeforeClass
public static void setUp() throws MalformedURLException
{
DesiredCapabilities capabilities=new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME","Chrome");
capabilities.setCapability("VERSION","4.3");
capabilities.setCapability("deviceName","SGH-T999L");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage","org.odk.collect.android");
capabilities.setCapability("appActivity","com.fieldforce.android.activities.LoginActivity");
webDriver=new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
// webDriver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
}
#Test
public void testLogin() throws Exception
{
// webDriver.switchTo().window("NATIVE_APP");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement userName= webDriver.findElement(By.id("txt_username"));
userName.sendKeys("733894");
WebElement password= webDriver.findElement(By.id("txt_password"));
password.sendKeys("Pass#123");
WebElement login_button= webDriver.findElement(By.id("org.odk.collect.android:id/btn_login"));
wait.until(ExpectedConditions.visibilityOf(login_button));
login_button.click();
}
#AfterClass
public static void tearDown()
{
webDriver.quit();
}
Of course the test will pass, as you are just trying to click on Login Button, it doesn't matter for your test case whether it should pass or fail unless you add some Assert after clicking on login button.
Try adding some wait after click on login
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
And then try asserting something in #test for method testLogin like add below two statement in the end, and make them compatible with locator you get after login
Actualtext = driver.findElement(By.xpath("locator to verify after login")).getText();
Assert.assertEquals(Actualtext, "assert Text");
I got the same issue.
Please make sure your submit button(UI) is not covered by the phone keyboard.
If it covers, Solution :
hide the keyboard before run the click [.click()] function.
keyboard hide code is here How to dismiss the keyboard in appium using Java?
Reason:
Coz Appium clicks the button using x&y UI coordinates and if the keyboard covers the submit button it clicks on the keyboard not on the button.
In my case I've also tried similar button clicking with no result. I've realized that I wasn't clicking the right element.
To verify what I was clicking I've printed element's Text and TagName:
List<MobileElement> views = driver.findElements(By.className ("android.widget.TextView"));
System.out.println("1: "+views.get(2).getText());
System.out.println("2: "+views.get(2).getTagName());
views.get(2).click();
Output was:
1: OK
2: android.widget.TextView
My classNames I got through UI Automator Viewer, Android/Sdk/tools/bin/uiautomatorviewer application.
Although, before list's getting you could temporarily use Thread.sleep(5000); with your own values to check assumption about slow element loading and replace it with proper time waiting method if required in case of time problem.
Sometimes a button could have multilayer structure and it is required to get the right layer you could click. In my case there were two layers.
Would suggest using
WebElement login_button= webDriver.findElement(By.id("btn_login")); //as used for other WebElement
Also i believe a general login page shall ideally be having the login visible, if that not be the case, you might want to perform a scroll down on the page instead of
wait.until(ExpectedConditions.visibilityOf(login_button));
PS - If you are receiving any error/exception post this. Please add to the question and let know.
As your keyboard hiding the Login button, so hide the keyboard by,
driver.hidekeyboard();
I want to click on the app tutorial OK button which is displayed only for first time,
In robotium this statement does this
if(solo.searchText("OK")){
solo.clickOnText("OK");
}else{
//Do other things
}
If OK text is not displayed test should not fail it should continue, but in espresso test is failed when second time app is run because that time app tutorial is not displayed.
In espresso
onView(withText("OK")).check(matches(isDisplayed())).perform(click());
asssertion fails here, I want boolean return value for OK text so that if OK button is not displayed test should continue.
Try this
public void checkVisibileText(#StringRes int message) {
onView(withText(message)).check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
}
I'm new to Robotium as well as Android. I have written some code to test an activity. and that activity is opening a dialogue box. and i'm not able to identify it. I tried solo.getCurrentActivity(), not workes. I tried solo.searchText(). But test is failing.
write code like this
if (solo.waitForActivity("HomePhone", 20000)) {
if(solo.waitForText("xxx*",1,10000)){
if(solo.waitForText("ok*",1,5000)){
solo.clickOnText("ok*");
}else{
solo.goBack();
}
}
wait for text is best method for your purpose
I am writing a test case class for one of the activity in my application.
That Activity class contains license checking for the application in android market and also displays the splash screen for 3 seconds. Here I would like to test that activity is displaying the splash screen and checking the license using Robotium instrumentation in Android.
So please tell me how to do this.
To test that your splash screen is shown, you can try this, if you have set up robotium in your setup method, and named it solo:
public void testSplash() {
assertNotNull(solo.getCurrentActivity().findViewById( "the id of the splash" ));
}
public void testLicense() {
String licence = "my licence";
assertEquals(licence, (MyActivity) solo.getCurrentActivity().getLicence());
}