我是Android自动测试的新手。目前我正在编写一个项目,允许我在Android应用程序上自动发布帖子。我尝试使用maven或gradle来构建Java项目。我使用Selendroid自动点击Android模拟器上的应用程序。
控制应用程序中的组件。但我不知道如何处理与系统相关的部分,如位置访问权限: Location access
并从设备中选择照片。我试过Selendroid和Uiautomator,两者都不起作用。这是我的代码和项目结构:
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();
}
}