Appium Webdriver Import Issue - java

Building a basic Appium test in Java for Android.
When I run the code, it gives me an exception error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
driver cannot be resolved
driver cannot be resolved
driver cannot be resolved to a variable
at tests.AppiumTest.main(AppiumTest.java:50)
I have triple checked my jar files, all of them appear to be included and I'm not missing any but when I hover over the driver text, the import Webdriver option does not appear.
Code below:
package tests;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class AppiumTest {
public static void main(String[] args) {
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "My Phone");
caps.setCapability("udid", "Redacted"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.1.1");
caps.setCapability("appPackage", "com.android.vending");
caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
caps.setCapability("noReset", "true");
// Instagram: com.instagram.android/com.instagram.android.activity.MainTabActivity
// Facebook: com.facebook.katana/com.facebook.katana.LoginActivity
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
//Added 5 seconds wait so that the app loads completely before starting with element identification
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Find Google Play element using ID property and click on it
driver.findElement(By.id("com.android.vending:id/search_box_idle_text")).click();
//Find 'Google Play Store' element and set the value Google
driver.findElement(By.id("com.android.vending:id/search_box_text_input")).sendKeys("Google");
//Press Enter key from Keyboard using any of the below methods
((AndroidDriver<MobileElement>) driver).pressKeyCode(66);
}
}
Screenshots of imported jar files:
1
2

First of all You didn't show where AndroidDriver is declared.
Second thing is that I don't see in Your dependancies Appium, You just have selenium imported.
appium.io
You should have something like this in your pom, if using maven:
https://mvnrepository.com/artifact/io.appium/java-client/6.0.0
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.0.0</version>
</dependency>
My dependancy:
Hope this helps...

Related

Exception in thread "main" java.lang.NoClassDefFoundError (Java, Appium, Android Studio)

I keep getting an error on runtime when I'm executing from Eclipse IDE. I added the class paths for the commons-lang3, java-client and selenium webdriver for java jar packages. What am I missing?
My code:
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class InstallAppAndroidEmulator {
public static void main(String[] args) {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, 9.0);
dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
dc.setCapability(MobileCapabilityType.APP, "C:\\Users\\Some_Name\\Desktop\\apk files\\app-test.apk");
URL url = null;
try {
url = new URL("http://localhost:4723/wd/hub");
AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(url, dc);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Files:
Error message:
Solved by adding missing selenium jars from lib folder (unless using a standalone library)
Solved by adding missing selenium jars from lib folder as well (not needed if using a standalone library)

'class' or 'interface' expected

I am trying to start automating testing using Appium. I'm getting 'class' or 'interface' expected on my desired capabilities.
The code I'm using is below:
package tests;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class AppiumTest {
public static void main(String[] args) {
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "My Phone");
caps.setCapability("udid", "ZY224Gs7NG"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.1.1");
caps.setCapability("appPackage", "com.android.vending");
caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
caps.setCapability("noReset", "true");
//Instantiate Appium Driver
try {
AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
}
}
Instead of the above code in AppiumTest class use the code
cap.setCapability(MobileCapabilityType.PLATFORM_NAME,MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
Always use device name as "Android device" and you don't need PlatformVersion line in your code and no need to give device id as well so also remove this line "caps.setCapability("udid", "ZY224Gs7NG");"
The other code looks great it should work with these changes.

Not able to locate element in chrome by using appium java

I am learning appium and trying to perform a basic google search operation in appium java. The code I have written is:
package com.MavenTest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class StartChrome {
#Test
public void test1() throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "My Phone");
caps.setCapability("udid", "790dc03c"); // Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "5.1.1");
caps.setCapability("browserName", "Chrome");
caps.setCapability("noReset", true);
// Create object of AndroidDriver class and pass the url and capability that we
// System.setProperty("webdriver.chrome.driver",
// "D:\\workspace\\AppiumTest\\driver\\chromedriver.exe");
AppiumDriver<MobileElement> driver = null;
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
// Open URL in Chrome Browser
driver.get("http://www.google.com");
System.out.println("Title " + driver.getTitle());
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.name("Search")));
driver.findElementByName("q").sendKeys("google");
Thread.sleep(2000);
driver.findElementByName("Gogle Search").click();
driver.quit();
}
}
error I am getting when trying to sendkeys is:
java.lang.ClassCastException:
org.openqa.selenium.remote.RemoteWebElement cannot be cast to
io.appium.java_client.MobileElement
MobileElement is derived from RemoteWebElement.
If you write code like this:
driver.findElementByName("q").sendKeys("google");
You are trying to cast a RemoteWebElement to one of its subclasses MobileElement.
java.lang.ClassCastException Issue comes if you directly access the method like below:
driver.findElementByName("q").sendKeys("google");
WorkAround: You have to cast to the MobileElement like below:
MobileElement find = (MobileElement) driver.findElementByName("q").sendKeys("google");
I faced the same issue, upgrading Appium Client library fixes it.
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.4</version>
</dependency>

Looking for solution to driver.scrollTo("Views")

I am beginner to Apium and java, i was looking for a solution to driver.scrollTo("Views"), i tried the below code , while running i was getting the following error:
A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity) (WARNING: The server did not provide any stacktrace information)
package Android;
import static org.junit.Assert.assertNotNull;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScrollingToText {
AndroidDriver driver;
#BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "0123456789ABCDEF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "5.0");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.hmh.api");
capabilities.setCapability("appActivity","com.hmh.ApiDemos");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void findScrollable() throws InterruptedException {
//Scroll till element which contains "Views" text If It Is not visible on screen.
// driver.scrollTo("Views");
driver.findElementByAccessibilityId("Views").click();
WebElement radioGroup = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"+ ".resourceId(\"android:id/list\")).scrollIntoView("
+ "new UiSelector().text(\"Radio Group\"));");
assertNotNull(radioGroup.getLocation());
// Click on Views/.
driver.findElement(By.name("Views")).click();
System.out.println("Scrolling has been started to find text -> Tabs.");
// Scroll till element which contains Tabs text.
// driver.scrollTo("Tabs");
driver.findElementByAccessibilityId("Tabs").click();
System.out.println("Tabs text has been found and now clicking on It.");
// Click on Tabs.
driver.findElement(By.name("Tabs")).click();
}
#AfterTest
public void End() {
driver.quit();
}
}
(Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)
Above error come when you don't have application installed on your device.
Please install it first and then run your test script it should run.
Hope these help you.

Appium to test Android Hybrid Mobile Application

I want to automate test a basic Hybrid Mobile Application build on top of Cordova running in Android. I used Apppium for that. I followed the tutorial video to get started. I downloaded and Added Selenum, selendroid and java client .jar files to the build path of the Application.
Below is my code,
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class AppiumTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
URL serverAddress = new URL("http://0.0.0.0:4723/wd/hub");
WebDriver driver = new AndroidDriver<MobileElement>(serverAddress, getDesiredCapabilities());
try{
Set<String> contextNames = ((AppiumDriver<MobileElement>) driver).getContextHandles();
for (final String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW_0")) {
Thread.sleep(3000);
driver.switchTo().window("WEBVIEW_0");
}
}
//Change color to Red
driver.findElement(By.cssSelector("p.recieved")).click();
Thread.sleep(2000);
//Change color to Red
driver.findElement(By.cssSelector("recieved")).click();
Thread.sleep(2000);
driver.get("http//appium.io/");
Thread.sleep(2000);
}
finally {
driver.quit();
}
System.out.println("Driver "+driver);
}
public static DesiredCapabilities getDesiredCapabilities() {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(MobileCapabilityType.AUTOMATION_NAME, "selendroid");
capability.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capability.setCapability("platformVersion", "4.4.4");
capability.setCapability("deviceName", "Android-Dev");
capability.setCapability("app",
"C:/Joseph/Appium/HybridProject/AppiumTest/platforms/android/build/outputs/apk/android-debug.apk");
capability.setCapability("appPackage", "com.joseph.appiumtest");
capability.setCapability("appActivity", ".MainActivity");
return capability;
}
}
I can able to get the Capabilities and Contexts. On switching the window (driver.switchTo().window("WEBVIEW_0")), I am getting error like Exception in thread "main" org.openqa.selenium.WebDriverException: CATCH_ALL: java.lang.VerifyError: io/selendroid/server/model/SelendroidWebDriver
Versions :
Android : 4.4.4
Appium : 1.6.3
Selenium : 3.0.1
Selendroid: 0.17.0
After lots of tries, got the Automation testing in Hybrid Mobile Application using Appium worked.
Basically in Capabilities, its not necessary to set the package name and activity name. Instead get the file path of the Application package name(.apk).
File app= new File("project/platforms/android/build/outputs/apk/android-debug.apk");
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android-Dev");
capabilities.setCapability("platformVersion", "4.4.4");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "MobilePlatform.ANDROID");
capabilities.setCapability("app", app.getAbsolutePath());
Then do not explicitly switch the window to WEBVIEW. Get the context and set the dynamic context of Webview particular for the Application.
try {
Set<String> contextNames = ((AppiumDriver) driver).getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")) {
((AppiumDriver<MobileElement>) driver).context(contextName);
}
}
}
catch(Exception e){
e.printStackTrace();
}
And finally with the driver set, do the action.
driver.findElement(By.xpath("//*[#id='login']")).click();

Categories