如何使用robotium连接数据库?

时间:2012-11-09 11:52:26

标签: android robotium

我正在使用Robotium来测试Android应用程序。

以下是我的代码。我也想验证数据库详细信息。如何添加数据库连接并验证这一点?

package com.robotiumtest.android;

import android.test.ActivityInstrumentationTestCase2;

import com.jayway.android.robotium.solo.Solo;

@SuppressWarnings("unchecked")
public class Home extends ActivityInstrumentationTestCase2 {

    private static final String TARGET_PACKAGE_ID = "com.robotiumtest.android";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "xxxxx";
    private static Class<?> launcherActivityClass;
    static {
        try {
            launcherActivityClass = Class
                    .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public Home() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);
    }

    private Solo solo;

    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testCanOpenSettings() {
        // Press the Menu
        solo.pressMenuItem(0);
            int i = 0;
        //solo.clickOnButton(3);
        //solo.wait(10);
        //solo.clickOnButton(0);
        //solo.waitForText("Title");
        //solo.clickOnText("Title");
        solo.enterText(0,"xxxxx");
        solo.clickOnButton(0);
        solo.waitForText("xxx");
        solo.clickOnButton("xx");
        solo.waitForText("xxxx");
        solo.enterText(0, "robot2@gmail.com");
        solo.enterText(1, "asdfghjkl");
        solo.clickOnButton(1);        
        solo.sleep(10000);
        solo.waitForText("school");
        solo.enterText(0, "vsb");
        solo.clickOnButton(0);
        solo.waitForText("xxxxxx"); 
        solo.clickOnText("1");
        solo.clickOnText("2");
        solo.enterText(0, "672");
        solo.searchButton("BUY");
        solo.clickOnButton("BUY");
        solo.waitForText("Please confirm");
        solo.clickOnButton(1);
        /*solo.sleep(10000);*/  
        solo.waitForText("Thank you");
        solo.sleep(10000);
        solo.clickOnButton(1);
        if (solo.searchText("No thanks"))
        {
            solo.clickOnText("No thanks");
        }
        solo.waitForText("Welcome");
        solo.clickOnButton("LOGOUT");
        solo.sleep(10000);
        }
/*
    @Override
    public void tearDown() throws Exception {
        try {
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        getActivity().finish();
        super.tearDown();
    }*/
}

1 个答案:

答案 0 :(得分:1)

您可以使用Java API,首先与数据库建立连接:

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement st=con.createStatement();

然后使用executeQuery()方法创建查询。