Scott
Scott

Reputation: 21

Appium android driver

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
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.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import android.provider.Browser;
import android.test.ActivityInstrumentationTestCase2;

import com.example.secretsanta.MainActivity;

public class AppiumExampleTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private RemoteWebDriver driver;

    public AppiumExampleTest(Class <MainActivity> activityClass) {
        super(activityClass);
    }

    @Before
    public void setup() throws MalformedURLException {
        File appDir = new File("../secret_santa//bin");
        File app = new File(appDir, "secret_santa.apk");

        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("version", "5");
        caps.setCapability("device", "emulator-5554");
        caps.setCapability("platform", "ANDROID");
        caps.setCapability("browser", "CHROME");
        caps.setCapability("app-package", "com.example.secretsanta");
        caps.setCapability("app-activity", "MainActivity.class");
        caps.setCapability("app", app.getAbsolutePath());


            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);


        driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

    }

    @Test
    public void testAppiumExample() throws Exception {

        // find button with label or content-description "Button 1"

        driver.findElement(By.id("createListButton")).click();

        // click on button and start second Activity

        // we are on second screen now
        // check if second screen contains element with text “Activity2”
        driver.findElement(By.id("createListView"));

        // click back button
        HashMap<String, Integer> keycode = new HashMap<String, Integer>();
        keycode.put("keycode", 4);
        ((JavascriptExecutor) driver).executeScript("mobile: keyevent", keycode);
        //
        // // we are again in main activity
        driver.findElement(By.id("createListButton"));
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

}

I am trying to press the button and then go to the next activity and then return to the main activity. At the moment I have a few errors, one with the actvityinstrumentationtestcase2 and then the constructor and then another with the element.

Upvotes: 0

Views: 17968

Answers (5)

Alex
Alex

Reputation: 372

Try to set capabilities something like the following:

  public class AbstractTest{

    public static AndroidDriver driver;
    private static String appFileName = "";
    private static String appDirName = "";
    public static String appPack = "";

    static File app;
    static File appDir;

    static DesiredCapabilities cap = new DesiredCapabilities();

    @BeforeSuite
    public void setUp() throws MalformedURLException {

        try {
            appFileName = PropertyLoader.loadProperty("appFileName");
            appDirName = PropertyLoader.loadProperty("appDirName");
            appPack = PropertyLoader.loadProperty("appPack");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        appDir = new File(appDirName);
        app = new File(appDir, appFileName);

        cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");

        cap.setCapability(MobileCapabilityType.PLATFORM_NAME,
                MobilePlatform.ANDROID);
//      For Android Emulator
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        cap.setCapability("app","C:\\secret\\secret\\secret\\secret.apk");

        cap.setCapability("appPackage","secret");

        cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
        cap.setCapability("unicodeKeyboard", "True");


        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        PageFactory.initElements(new AppiumFieldDecorator(driver),new LoginPage());


    }

you should provide your actual file name, dir name, pack name and so on

Upvotes: 0

Suman
Suman

Reputation: 436

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

works for me.

Upvotes: 0

Tomas
Tomas

Reputation: 1887

Did you try to use

private RemoteWebDriver driver;

instead of

private WebDriver driver; ?

Like that it works for me.

Upvotes: 1

Alex
Alex

Reputation: 372

And you also can try to have in your Test Base (parent) class the following:

    public static AppiumDriver getDriver() {
    return driver;
}

Upvotes: 0

Gaurav
Gaurav

Reputation: 1370

Try to use Appium driver instead:

protected AppiumDriver driver;

Upvotes: 0

Related Questions