I am new to Android Automated test. Currently I am writing a project which allows me to automated post staff on an Android app. I have tried using maven or gradle to build a Java project. And I used Selendroid to automated click the app on Android emulator.
Controlling components in the app went find. But I do not know how to deal with the part related to the system like Location access permission: Location access
and selecting photo from device. I tried Selendroid and Uiautomator, both not working. And here are my codes and project structure:
import io.selendroid.client.SelendroidDriver;
import io.selendroid.client.SelendroidKeys;
import io.selendroid.common.SelendroidCapabilities;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.Actions;
import android.support.test.automator.UiDevice;
import android.support.test.automator.UiObject;
import android.support.test.automator.UiObjectNotFoundException;
import android.support.test.automator.UiSelector;
import android.support.tests.InstrumentationRegistry;
public class AutomatedPost {
private WebDriver driver = null;
private UiDevice mDevice;
@Before
public void setup() throws Exception{
driver = new SelendroidDriver(new SelendroidCapabilities("com.offerup:2.10.1"));
}
@Test
public void start() throws Exception{
driver.get("and-activity://com.offerup.android.activities.SplashActivity");
Thread.sleep(5000);
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject allowButton = mDevice.findObject(new UiSelector().text("ALLOW"));
driver.switchTo().window("NATIVE_APP");
UiObject denyButton = mDevice.findObject(new UiSelector().text(TEXT_DENY));
denyButton.click();
System.out.println("2");
allowButton.click();
driver.findElement(By.id("post_item")).click();
driver.findElement(By.id("loginOfferUp")).click();
Thread.sleep(1000);
driver.findElement(By.id("emailaddress")).sendKeys("xxxx@xxx.com");
driver.findElement(By.id("password")).sendKeys("xxxxx");
driver.findElement(By.id("login")).click();
Thread.sleep(1000);
driver.findElement(By.id("pre_post_help_forward_button")).click();
driver.findElement(By.id("pre_post_help_forward_button")).click();
driver.findElement(By.id("pre_post_help_footer_text")).click();
driver.findElement(By.id("addPhotoFromGallery")).click();
Thread.sleep(20000);
//driver.findElement(By.)
Thread.sleep(50000);
UiObject allowPermissions = mDevice.findObject(new UiSelector().text("DENY"));
if (allowPermissions.exists()) {
try {
allowPermissions.click();
} catch (UiObjectNotFoundException e) {
System.out.println("xxx");
}
}
Thread.sleep(5000);
}
@After
public void teardown() {
driver.quit();
}
}