appium test findElement button click doesnt work when it should - java

I'm trying to run a test in eclipse using appium for android. While the test starts and the application launches it throws an error on the first command while trying to press a button. I'm pretty sure that the id for the button is correct since running the same code with selendroid worked. The error at failure trace shows java.lang.NullPointerException. Here is my code:
package thePack;
import static org.junit.Assert.*;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class theTest {
static AppiumDriver driver;
#Before
public void setUp() throws MalformedURLException, InterruptedException, Exception
{
WebDriver dr;
File app = new File("C:\\development\\src\\main\\resources\\app.apk");
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "Vodafone Smart 4G");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appium-version", "1.2.1");
capabilities.setCapability("appPackage", "mypackage.mine.net");
capabilities.setCapability("appActivity", "mypackage.mine.net.activities.mainActivity");
dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) ;
}
#Test
public void login() throws Exception
{
Thread.sleep(0500);
driver.findElement(By.id("english")).click();
Thread.sleep(0500);
}
Any ideas? Thanks!

the line:
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
indicates that your test will be performed on the browser, and this should open the browser when start your test. Thus your elements will never be found

Figured out the issue. For me the problem was the Appium version itself. I was running the 1.3.4.1 and using an Android 4.2.2 device. As soon as i switched to version 1.2.4.1 for Appium i had no problems!

Related

How to open browser and android at the same time and run the tests simultaneously?

I have a test scenario that is as follows, I start the application on android using appium, then I need to open the browser to authenticate some things, right after that I need to go back to android. How to do this process ? I thought of using multithread .
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class ioSampleTest {
public AndroidDriver<MobileElement> driver;
public WebDriverWait wait;
//Elements
String secondNewJob = "//android.widget.FrameLayout[2]/android.widget.LinearLayout/" +
"android.widget.RelativeLayout/android.widget.ImageView";
#BeforeMethod
public void setup () throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Galaxy Nexus API 24");
caps.setCapability("udid", "emulator-5554"); //DeviceId from "adb devices" command
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.0");
caps.setCapability("skipUnlock","true");
caps.setCapability("appPackage", "com.isinolsun.app");
caps.setCapability("appActivity","com.isinolsun.app.activities.SplashActivity");
caps.setCapability("noReset","false");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"),caps);
wait = new WebDriverWait(driver, 10);
}
#Test
public void basicTest () throws InterruptedException {
//Click and pass Splash
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("com.isinolsun.app:id/animation_view"))).click();
//Click I am searching a job
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("com.isinolsun.app:id/bluecollar_type_button"))).click();
//Notification Allow
if (driver.findElements(By.id("com.android.packageinstaller:id/permission_allow_button")).size()>0) {
driver.findElements(By.id("com.android.packageinstaller:id/permission_allow_button")).get(0).click();
}
WebDriver browserFireFox = new FirefoxDriver();
try {
browserFireFox.get("https://google.com/ncr");
browserFireFox.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
System.out.println(firstResult.getAttribute("textContent"));
} finally {
browserFireFox.quit();
}
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.xpath(secondNewJob)));
}
#AfterMethod
public void teardown(){
driver.quit();
}
}
This is a sample code in java, I took the code from these sites Tutorial Appium Java and Selenium , and set up an example to make it a little easier to understand what I want to do.
Note The code may not work, it is just an example.
Is your application launching WebView from your android application to as what you have mentioned, "authentication" before you can do anything to your app?
I believe Appium's set context is what you need, the api will let you switch back and forth from native to webview vice versa.
But if you are insisting to test thru appium and at the same time to desktop browser with selenium, then it's not possible.

findElementByAccessibilityid is not shown in eclipse intellisense

I am trying to do a POC for a windows app using winappdriver . I have the winappdriver version 1.1 installed up and running. I want to find the elements by using their automationId. As per winappdriver documentation, elements with AutiomationID can be located by "findElementByAccessibilityId". I am not able to see this locator strategy in my Eclipse intellisense. Instead "findElementsByAccessibilityId" ( notice elements) is been shown up. What should i do so that i can see "findElementByAccessibilityId" locator in intellisense.
import java.net.MalformedURLException;
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.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.windows.WindowsDriver;
public class LoginTest {
private static WindowsDriver<WebElement> driver = null;
#BeforeClass
public static void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app", "XXXXXXXXXXXXXXXXXXXXXX");
capabilities.setCapability("platformName", "windows");
capabilities.setCapability("deviceName", "windowsPC");
capabilities.setCapability("appWorkingDir", "XXXXXXXXXXXXXXXXXXXXXXXXXX");
driver = new WindowsDriver<WebElement>(new URL("http://127.0.0.1:4723"), capabilities);
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
}
#Test
public void Testing()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.className("TextBox")).sendKeys("XXXX");
driver.findElementById("TxtPwd").sendKeys("XXXX");
driver.findElementsByAccessibilityId("TxtPwd");
driver.findElement(By.id("BtnLogin")).click();
System.out.println("Hi");
}
}
POM.xml

Android app closes after .click() command

I'm trying to automate a app to do everything that you would have to do by hand. My main goal right now is to get it to click a button after logging into the app.
This is the very last line of code in my IDE
driver.findElement(By.id("com.offerup:id/main_text")).click();
After this line of code executes, OfferUp, the app that I'm testing on, closes. There are no failures in console but, I don't want it to close after that line of code executes.
When I log into the app without running my code, the app stays open but, when I run my code, it closes after driver.findElement(By.id("com.offerup:id/main_text")).click(); is executed.
Why this is happening?
Here is my full code -
package OpenOfferUpTest;
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.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.server.handler.FindElement;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class OpenOfferUp {
AndroidDriver driver;
#Test
public void OpensOfferUp() throws MalformedURLException
{
File OfferUp = new File("C:\\Users\\boung\\Desktop\\OfferUp.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Virtual Device");
cap.setCapability("platformName", "android");
cap.setCapability("null", "OfferUp");
cap.setCapability("appPackage", "com.offerup");
cap.setCapability("appActivity", "com.offerup.android.login.splash.LoginSplashActivity");
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);
}
#Test
public void SimpleTest() throws InterruptedException {
driver.findElement(By.id("com.offerup:id/email_button")).click();
By path = By.xpath("//*[#text='Enter your email address']");
driver.findElement(path).sendKeys("sourgta#gmail.com");
driver.findElement(By.id("com.offerup:id/next_button")).click();
By path1 = By.xpath("//*[#text='']");
driver.findElement(path1).sendKeys("12manytimes");
driver.findElement(By.id("com.offerup:id/main_text")).click();
}
}

web element can not find the element in eclipse java with appium android app test

This is my Code. actually i was run the android app with appium using eclipse. here app is launch but it is not find any element and it show the error in console. the error is " Exception in thread "main" java.lang.NullPointerException ".
package amazon;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
//import junit.framework.Assert;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
//import org.junit.Assert;
//import org.junit.Test;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class StartApplication {
private static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "a71ce4a7");
capabilities.setCapability("platformVersion", "5.0.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.hypersoft.vwallet");
//capabilities.setCapability("appActivity", "com.hypersoft.vwallet.ui.activity.HomeActivityNew_");
capabilities.setCapability("appActivity", "com.hypersoft.vwallet.ui.activity.SignInActivity_");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Thread.sleep(10000);
System.out.println("App launched");
//Thread.sleep(10000);
driver.findElementById("com.hypersoft.vwallet:id/tvGetHelp").click();
driver.findElement(By.id("com.hypersoft.vwallet:id/etMailAddress")).sendKeys("test#gmail.com");
//driver.findElementByClassName("android.widget.EditText").sendKeys("test#gmail.com");;
System.out.println("test");
driver.findElement(By.id("com.hypersoft.vwallet:id/btnAgree")).click();
driver.findElement(By.id("com.hypersoft.vwallet:id/etMailAddress")).sendKeys("test321#gmail.com");
driver.findElement(By.id("com.hypersoft.vwallet:id/etPassword")).sendKeys("test4321");
driver.findElement(By.id("com.hypersoft.vwallet:id/etPasswordRe")).sendKeys("test4321");
driver.findElement(By.id("com.hypersoft.vwallet:id/etLastName")).sendKeys("Patel");
driver.findElement(By.id("com.hypersoft.vwallet:id/etFirstName")).sendKeys("Niravpp");
driver.hideKeyboard();
driver.findElement(By.id("com.hypersoft.vwallet:id/btnSignUp")).click();
driver.findElement(By.className("android.widget.ImageButton")).click();
driver.findElementByName("Logout").click();
driver.findElement(By.id("android:id/button1")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("com.hypersoft.vwallet:id/etMailAddress")).sendKeys("test321#gmail.com");
driver.findElement(By.id("com.hypersoft.vwallet:id/etPassword")).sendKeys("test4321");
driver.findElement(By.id("com.hypersoft.vwallet:id/btnSignIn")).click();
WebElement element = driver.findElement(By.name("VCA Wallet"));
String strng = element.getText();
Assert.assertEquals(strng,"VCA Wallet");
if(strng.equals("VCA Wallet"))
{
System.out.printf("pass\n");
} else {
System.out.println("Fail\n");
}
System.out.println(strng);
//driver.swipe(startx, starty, endx, endy, duration);
driver.close();
}
}
I will add my console in bellow screen shoot so, please check it and give some suggestion.

Able to install amazon app but not able to open it

with the help of apk file I am able to install the android app but it is not opening. Please see my code once:
package Test1;
import io.appium.java_client.android.AndroidDriver;
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.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Amazon_LogIn_Test {
private static AndroidDriver driver;
#BeforeMethod
#Test
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Apps/Amazon/");
File app = new File(appDir, "com.amazon.mShop.android.shopping-5.2.3-502030- minAPI9.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "BECUPJTWGA7HAQQK");
capabilities.setCapability("platformVersion", "5");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.splashscreen.StartupActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(10000);
// Click on Shop by Deparment link
driver.findElement(By.id("com.amazon.mShop.android.shopping:id/search_edit_text")).click();
// Click on Main menu
driver.findElementByClassName("android.widget.ImageView").click();
// Click on Home link under Main menu
driver.findElement(By.name("Home")).click();
// Click on Sign In link on the Home Screen
driver.findElementByName("Sign inHello. Link").click();
Thread.sleep(10000);
driver.findElement(By.xpath("//*[#content-desc='Hello. Sign in']"));
}
With the above code, I am able to install the amazon app and able to open the app as well but the last line which is there to click the sign in option is not getting clicked , i tried many way to do it.please help me on this.
Under desired capability instead of sign-in activity, please use splash activity, you need to change the activity in below line code:
capabilities.setCapability("appActivity","com.amazon.mShop.sso.SigninPromptActivity")
Try this appActivity:
capabilities.setCapability("appPackage", "in.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.splashscreen.StartupActivity");
i think your package name is wrong it should be "in.amazon.mShop.android.shopping" not this "com.amazon.mShop.android.shopping"

Categories

Resources