How to use assertEquals in appium by using selenium java

I just tries to compare actual result with expected result , using assertEquals, but getting error. I think i have made basic mistake. provide any example for compare text result

WebElement result= (WebElement)driver.findElementsByXPath("//android.widget.TextView[contains(@resource-id,‘formula’)]");
Assert.assertEquals(result, “9”);

result is a webelement object, you should compare result.getText() and “9”

WebElement result= (WebElement) driver.findElementsByXPath("//android.widget.TextView[contains(@resource-id,‘formula’)]");
Assert.assertEquals(result.getText(), “9”);
I have wrote code above but getting following error
java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement

findElementsByXPath returns a List. You probably want:

WebElement result= (WebElement) driver.findElementByXPath("//android.widget.TextView[contains(@resource-id,'formula')]");
Assert.assertEquals(result.getText(), "9");
2 Likes

Thanks for quick reply . I have tried but getting following error
java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement

Post your code please…

findElements return list of WebElements, you are assign it to WebElement …

You should assign it to List list = driver.findElementsByXPath …

package example;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import junit.framework.Assert;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestAppIication { AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “5.1”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
caps.setCapability(MobileCapabilityType.DEVICE_NAME,“vpk”);
//caps.setCapability(“avd”,“AVD_Nexus_4”);// Mention the created AVD name
caps.setCapability(MobileCapabilityType.APP_PACKAGE,“com.android.calculator2”);
caps.setCapability(MobileCapabilityType.APP_ACTIVITY,“com.android.calculator2.Calculator”);
driver = new AndroidDriver (new URL(“http://127.0.0.1:4723/wd/hub”), caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void testExample(){
WebElement five=driver.findElement(By.name(“5”));
five.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name(“4”));
four.click();
WebElement equalTo=driver.findElementByAccessibilityId(“equals”);
equalTo.click();
WebElement result= (WebElement) driver.findElementsByXPath("//android.widget.TextView[contains(@resource-id,‘formula’)]");
Assert.assertEquals(result.getText(), “9”);

}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}

You still have

...findElementsByXPath...

instead of

...findElementByXPath...

notice the ‘s’ on findElements vs findElement

I have made changes but still getting following error

TestNG] Running:
C:\Users\prabu\AppData\Local\Temp\testng-eclipse-4225576\testng-customsuite.xml

FAILED CONFIGURATION: @BeforeClass setUp
org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 209 milliseconds
Build info: version: ‘3.0.0-beta2’, revision: ‘2aa21c1’, time: ‘2016-08-02 15:03:28 -0700’
System info: host: ‘Vaibhav-PC’, ip: ‘192.168.116.2’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_101’
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:683)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:130)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:143)
at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:37)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:160)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:168)
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:69)
at example.TestAppIication.setUp(TestAppIication.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:170)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)

SKIPPED CONFIGURATION: @AfterClass tearDown
SKIPPED: testExample

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@3c5a99da: 14 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@6adca536: 12 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@23223dd8: 8 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 9 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2eee9593: 47 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@34340fab: 39 ms

launch your appium server with parameter

--session-override

or stop and start server before launching your test

getting same error after killing server and restart it again

Its working by using String
String result = driver.findElement(By.className(“android.widget.EditText”)).getText();

Please tell me how to terminate app forcefully and how to view html result in TestNG

Hi telmo,

How to verify List of texts coming post swipe and have to Assert each text post scroll.
had tried this approach.

public void listOnmenuItemTextViewBurgers() {
String[] expected = {“SomeName”, “Double Meat”, " Avocado Bacon Burger"};

	HashSet<String> uniqueList = new HashSet<String>();
	for (int i = 0; i < 3; i++) {
		List<MobileElement> list1 = listBurgersCategory;
		String text1 = null;
		List<String> textlist = new ArrayList<String>();
		for (int j = 0; j < list1.size(); j++) {
			text1 = list1.get(j).getText();
			textlist.add(text1);
		}
		uniqueList.addAll(textlist);
		list1.clear();
		comlib.HorizontalSwipe(driver, 0.40);
	}
	log.stepLog("**********Verify Burgers Category List***********");
	for (String Burger : uniqueList) {
		Assert.assertEquals(Burger.contains("SomeName"), expected[0]);
	}
}

This approach fails since:
FAILED: VerifyAssertListByOrderScreen
junit.framework.AssertionFailedError:
expected: but was: SomeName
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:282)