测试期间如何在Android设备之间切换

问题描述 投票:0回答:2

我是自动化初学者。我正在本机应用程序上编写自动测试。 当使用一台设备通过测试时,一切正常。 但我希望在测试过程中涉及 2 个或更多可以分布式工作的设备。 例子: 设备 1(用户 1) 申请开始 登录应用程序 创建一条消息并发送给用户 2

设备2(用户2) 申请开始 登录应用程序 检查收到的来自用户 1 的消息

由于我是初学者,根据我的理解,这应该在一次测试中发生,以免进行相互依赖的多个测试,只需在驱动程序(设备)之间切换

现在一切都在以下层次结构中完成: MobileDriver 类 - 驱动程序在其中初始化 课堂测试 帮助类 - 用于延迟、期望等。 具有测试本身逻辑方法的类

如果我的逻辑错误或不可能这样做,请为这个问题提出更正确的解决方案

我在 Idea 工作 爪哇8 Appium 1.14.0 Windows 10

public class MobileDriver {

    public static AppiumDriver<MobileElement> driver;
    public static AppiumDriver<MobileElement> driver2;

    public void mobileDriver(String arg) throws MalformedURLException {

        if (arg.equals("1")) {
            emulatorDevice5554();
        } else if (arg.equals("2")) {
            emulatorDevice5556();
        } else if (arg.equals("3")) {
            realDevice();
        } else if (arg.equals("4")) {
            test2devices();
        }


    public void emulatorDevice5554() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        //desiredCapabilities.setCapability("deviceName", "Android Emulator");
        desiredCapabilities.setCapability("deviceName", "emulator-5554");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion", "8.1.0");
        desiredCapabilities.setCapability("systemPort", "8201");
        //desiredCapabilities.setCapability("automationName", "Appium");
        desiredCapabilities.setCapability("automationName", "UiAutomator2");
        desiredCapabilities.setCapability("app", "C:/Users/Asus/IdeaProjects/iopayphonex/app/app.apk");
        desiredCapabilities.setCapability("appPackage", "package");
        desiredCapabilities.setCapability("appActivity", "Activity");
        desiredCapabilities.setCapability("noReset", true);

        //initialize mobileDriver
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    public void emulatorDevice5556() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        //desiredCapabilities.setCapability("deviceName", "Android Emulator");
        desiredCapabilities.setCapability("deviceName", "emulator-5556");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion", "7.0");
        desiredCapabilities.setCapability("systemPort", "8202");
        //desiredCapabilities.setCapability("automationName", "Appium");
        desiredCapabilities.setCapability("automationName", "UiAutomator2");
        desiredCapabilities.setCapability("app", "C:/Users/Asus/IdeaProjects/iopayphonex/app/app.apk");
        desiredCapabilities.setCapability("appPackage", "package");
        desiredCapabilities.setCapability("appActivity", "Activity");
        desiredCapabilities.setCapability("noReset", true);

        //initialize mobileDriver
        driver2 = new AndroidDriver(new URL("http://127.0.0.1:5000/wd/hub"), desiredCapabilities);
        driver2.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    }
}


public abstract class Page {
    public MobileDriver driver;
    public WEBDriver chromeDriver;
}


public class CreateQuestionScreen extends Page {

    public CreateQuestionScreen(MobileDriver driver) {
        super.driver = driver;
    }
    public SwipesAndClicks swipesAndClicks = new SwipesAndClicks(driver);
    public WaitsMobile waitsMobile = new WaitsMobile(driver);
    public Randomizer randomizer = new Randomizer();
    Logger logger = LoggerFactory.getLogger(ContinueScreen.class);

public void searchQuestion() {
        waitsMobile.waitForElementAndClick(By.xpath(C.BTN_FORUM),
                "element BTN_FORUM not found",
                2);
        logger.info("success click to BTN_FORUM element");
        waitsMobile.waitForElementAndClick(By.xpath(C.CHOOSE_BUSINESS_CATEGORY),
                "element CHOOSE_BUSINESS_CATEGORY not found",
                2);
        logger.info("success choose CHOOSE_BUSINESS_CATEGORY element");
        try {
            waitsMobile.waitForElementAndClick(By.xpath("//android.widget.TextView[@text='" + testQuestion + "']"),
                    "element" + testQuestion + "not found from try",
                    2);
            logger.info("message '" + testQuestion + "' found");
        } catch (NoSuchElementException e) {
            System.out.println("element" + testQuestion + "not found from catch");
        }
    }
}


public class SendMessageToExpertTest extends utility.tested.Test {

    public static MobileDriver driver;
    public static SwipesAndClicks swipesAndClicks;
    public static WaitsMobile waitsMobile;
    public static ContinueScreen continueScreen;
    public static SignInScreen signInScreen;
    public static ProfileScreen profileScreen;
    public static ExpertProfileScreen expertProfileScreen;

    @BeforeClass
    public static void setUp() throws MalformedURLException, InterruptedException {
        driver = new MobileDriver();
        continueScreen = new ContinueScreen(driver);
        signInScreen = new SignInScreen(driver);
        profileScreen = new ProfileScreen(driver);
        waitsMobile = new WaitsMobile(driver);
        driver.mobileDriver("1");
        driver2.mobileDriver("2");
        swipesAndClicks = new SwipesAndClicks(driver);
        expertProfileScreen = new ExpertProfileScreen(driver);

        continueScreen.clickContinueButton();
        signInScreen.signInViaGoogle();
        Thread.sleep(6000);
        swipesAndClicks.clickToTips();
    }


    @Category(Regression.class)
    @Test
    public void sendMessageToExpertTest() throws Exception{
        expertProfileScreen.sendMessageToExpert();
        swipesAndClicks.clickToPinCode();
        expertProfileScreen.checkMessage();

    }

}
java android appium appium-android
2个回答
1
投票

你的代码有点乱。我建议您阅读有关 Java 面向对象编程的更多内容。因为我认为同时使用两个设备很有趣,所以我会尽力帮助您,并且我清理了您的代码(我没有测试它,它只会引导您走向正确的方向)。

我改变的事情:

  • MobileDriver 类:
  • 现在进行测试本身:
    • 如果您在所有测试中都需要这两种设备,您可以将它们添加到基类
      Page.java
      。如果没有,我只会将第二个驱动程序添加到您需要的测试类中。
    • 不要将这些字段设置为静态。 Java中静态字段的具体含义是什么?
    • 初始化两个设备后,您可以在测试方法中随意使用它们。

SetUpDriver.java

public final class SetUpDriver {

    private SetUpDriver() {
        // Hide the constructor.
    }

    public static AndroidDriver<MobileElement> getMobileDriver(String type) throws MalformedURLException {
        switch (type) {
        case "1":
            return emulatorDevice5554();
        case "2":
            return emulatorDevice5556();
        default:
            throw new IllegalArgumentException();
        }
    }

    private static AndroidDriver<MobileElement> emulatorDevice5554() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = getCommonCapabilities();
        desiredCapabilities.setCapability("deviceName", "emulator-5554");
        desiredCapabilities.setCapability("platformVersion", "8.1.0");
        desiredCapabilities.setCapability("systemPort", "8201");

        // initialize mobileDriver
        AndroidDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),
                desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        return driver;
    }

    private static AndroidDriver<MobileElement> emulatorDevice5556() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = getCommonCapabilities();
        desiredCapabilities.setCapability("deviceName", "emulator-5556");
        desiredCapabilities.setCapability("platformVersion", "7.0");
        desiredCapabilities.setCapability("systemPort", "8202");

        // initialize mobileDriver
        AndroidDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:5000/wd/hub"),
                desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        return driver;
    }

    private static DesiredCapabilities getCommonCapabilities() {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("automationName", "UiAutomator2");
        desiredCapabilities.setCapability("app", "C:/Users/Asus/IdeaProjects/iopayphonex/app/app.apk");
        desiredCapabilities.setCapability("appPackage", "package");
        desiredCapabilities.setCapability("appActivity", "Activity");
        desiredCapabilities.setCapability("noReset", true);
        return desiredCapabilities;
    }
}

SendMessageToExpertTest.java

public class SendMessageToExpertTest extends utility.tested.Test {

    private AndroidDriver<MobileElement> driver; // first device
    private AndroidDriver<MobileElement> driver2; // second device
    // Other members if needed.

    @BeforeClass
    public void setUp() throws MalformedURLException, InterruptedException {
        driver = SetUpDriver.getMobileDriver("1");
        driver2 = SetUpDriver.getMobileDriver("2");
        // Init other objects if needed.
    }

    @Category(Regression.class)
    @Test
    public void sendMessageToExpertTest() throws Exception {
        // Perform your test.
        // TODO
    }
}


总而言之,我认为在开始编写自动化测试之前最好先学习 Java 和 OOP。


-1
投票

看看www.repeato.app,它允许您使用“切换设备步骤”在设备之间切换。

© www.soinside.com 2019 - 2024. All rights reserved.