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.
Related
I have just setup the appium environment and facing the below problem.
It is impossible to create a new session because 'createSession' which takes HttpClient, InputStream and long was not found or it is not accessible
I have read some solutions but they didn't work.
Please can someone check it and suggest what i might be missing.I need to pass through this.
package appiumMobileAPPTestPackage;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class StartApplication {
private static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "\\Apps\\APKs");
File app = new File(appDir, "ApiDemos.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 6000);
capabilities.setCapability( AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true);
capabilities.setCapability("appium-version", "1.13.0");
capabilities.setCapability("deviceName", "SM-J700F");
capabilities.setCapability("udid", "5203ac07f4a8939d"); //Give Device ID of your mobile phone
capabilities.setCapability("platformVersion", "6.0.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
capabilities.setCapability("noReset", "true");
driver = new AndroidDriver(new URL("https://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
driver.quit();
}
}
And below exception I am shown. I have changed the mobile device and tried multiple solutions but to no help as of now. Also somebody explain this the exception chain why this has occurred and how did that start.
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...
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>
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();
Performing automation test on android emulator using appium. Browser in emulator is not opening when it is automated via code. I have copied my code below kindly look into it and help me out. Thanks in advance
package report;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class emulator {
WebDriver driver;
public void setUp() throws MalformedURLException
{
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,"browser");
capabilities.setCapability(CapabilityType.VERSION,"4.4");
capabilities.setCapability(CapabilityType.PLATFORM,"windows");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("devices","Android");
capabilities.setCapability("avd","nexus");
capabilities.setCapability("deviceName","");
capabilities.setCapability("appPackage", "com.android.browser");
capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity");
driver=new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
public void cal(){
driver.get("http://www.google.com");
}
public static void main(String[] args) throws MalformedURLException
{
emulator a=new emulator();
a.setUp();
a.cal();
}
}
The lock screen will deactivate the other script of our program. So I tried open the emulator manually and disable the lock screen (Settings -> Security -> None). Then close the emulator. Now open the emulator automatically and run the script.
public void setUp(int p) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "browser");
capabilities.setCapability(CapabilityType.VERSION, "");
capabilities.setCapability(CapabilityType.PLATFORM, "windows");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("udid", "emulator-" + p);
capabilities.setCapability("devices", "Android");
capabilities.setCapability("avd", "Nexus7");
capabilities.setCapability("deviceName", "");
capabilities.setCapability("appPackage", "com.android.browser");
capabilities.setCapability("appActivity", "com.android.browser.BrowserActivity");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:" + this.port + "/wd/hub"),
capabilities);
}
When working on mobile browser we need not to pass all the above desiredcapabilities like apppackage and appactivitiy. You can check the details of required capabilities # http://qaautomationcafe.blogspot.in/2015/09/mobile-web-automation-using-appium.html