Appniumで簡単なログインを実現

9520 ワード

public class SLogin {
    AndroidDriver driver;
    WebElement et_pwd, et_user, bt_login, et_main, longPress_item,add_bookmark,remove_bookmark,tag_bookmark,btn_sure;
    TouchAction action_item;

    @Before
    public void setup() throws Exception {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "Android Device");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "4.4.2");
        capabilities.setCapability("appPackage", "news");
        capabilities.setCapability("appActivity",
                "news.view.activity.StartActivity");
        URL url = new URL(URLlist.SERVER_URL);
        driver = new AndroidDriver(url, capabilities);
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

    }

    @Test
    public void testLogin() throws Exception {
        et_user = AppniumFindElementUtil.findElement(driver,"ByText",Constants.user_txt);
        et_user.sendKeys(Constants.userText);
        et_pwd=AppniumFindElementUtil.findElement(driver, "ById",Constants.pwd_ID);
        et_pwd.sendKeys(Constants.pwdText);
//Login
        bt_login = AppniumFindElementUtil.findElement(driver,"ByText",Constants.login_txt);
        bt_login.click();
        driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

//Home  
        et_main = AppniumFindElementUtil.findElement(driver,"ById",Constants.title_ID);
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        Assert.assertEquals("Login successfully", "DMS News", et_main.getText());
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

        SwipeUtil.swipeToUp(driver, 3);
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);        
        longPress_item =  AppniumFindElementUtil.findElement(driver,"ById",Constants.popUpWindow_ID);
        action_item = new TouchAction(driver);
        action_item.longPress(longPress_item).perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

//Add bookmark
        add_bookmark=AppniumFindElementUtil.findElement(driver,"ByText",Constants.add_bookmark_txt);

        btn_sure=AppniumFindElementUtil.findElement(driver,"ByText",Constants.sure_txt);
        btn_sure.click();
        Thread.sleep(5000);
//      TODO
//      Assert.assertEquals(expected, actual);

        // item
            /*  List newsList = driver.findElements(By.id(Constants.popUpWindow_ID));
                int n = newsList.size();
                for(int i=0;i
    }

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

Constantsは次のとおりです.
public class Constants {

    public static String user_txt="new UiSelector().text(\"Username\")";
    public static String pwd_ID="news:id/login_pass";
    public static String login_txt="new UiSelector().text(\"Login\")";
    public static String title_ID="news:id/main_title";
    public static String popUpWindow_ID="news:id/item_img";
    public static String tag_bookmark_ID="news:id/rb_book";
    public static String add_bookmark_txt="new UiSelector().text(\"Add bookmark\")";
    public static String remove_bookmark_txt="new UiSelector().text(\"Remove bookmark\")";
    public static String sure_txt="new UiSelector().text(\"Ok\")";


    public static String userText = "admin";
    public static String pwdText = "user";
}