Appium Workshop - Sử dụng hình ảnh để tìm đối tượng

Nơi thông tin về các vấn đề liên quan đến khóa học Appium - Mobile Automated Test.
Forum rules
Nơi thông tin về các vấn đề liên quan đến khóa học Appium - Mobile Automated Test.
Post Reply
cuhavp
Jr. Tester
Posts: 61
Joined: Mon 21 Jan, 2013 3:52 pm
Contact:

Appium Workshop - Sử dụng hình ảnh để tìm đối tượng

Post by cuhavp »

Các bạn có thể tham khảo các bài post trước về : Một trong những tính năng mới của Appium phiên bản mới nhất đó là sử dụng hình để tìm đối tượng lúc làm việc. Cách này thì đã có một số tool như Silkuli sử dụng trước đây. Mục đích của tính năng này là để hỗ trợ test các ứng dụng Game, đây là một trong những mảng mà cho tới thời điểm hiện tại rất khó để tự động hoá kiểm thử này. Appium sử dụng OpenVC để chuyển đổi hình thành mã code sau đó so sánh với màn hình hiện tại

Trước hết để có thể sử dụng tính năng này bạn cần sử dụng thư viện Appium của java như dưới đây :

Code: Select all

<dependency>
            <groupId>com.github.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>969fac6a1c6dd229bdefb02ff10e71c812701e0c</version>
        </dependency>
Đây là phiên bản appium đang trong quá trình phát triển, hi vọn sắp tới phiên bản này sẽ được release chính thức

Và dưới đây là đoạn code ví dụ:

Code: Select all

public class FindByImage {
    private String APP_IOS = "https://github.com/cloudgrey-io/the-app/releases/download/v1.7.0/TheApp-v1.7.0.app.zip";
    private String APP_ANDROID = "https://github.com/cloudgrey-io/the-app/releases/download/v1.7.0/TheApp-v1.7.0.apk";

    private static By photos = MobileBy.AccessibilityId("Photo Demo");

    @Test
    public void testImage_iOS() throws URISyntaxException, IOException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("platformVersion", "12.0");
        capabilities.setCapability("deviceName", "iPhone X");
        capabilities.setCapability("app", APP_IOS);

        IOSDriver driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
        actualTest(driver);
    }

    @Test
    public void testImage_Android() throws URISyntaxException, IOException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("automationName", "UiAutomator2");
        capabilities.setCapability("app", APP_ANDROID);

        AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
        actualTest(driver);
    }

    private String getReferenceImageB64() throws URISyntaxException, IOException {
        URL refImgUrl = getClass().getClassLoader().getResource("Reference_Image.png");
        File refImgFile = Paths.get(refImgUrl.toURI()).toFile();
        return Base64.getEncoder().encodeToString(Files.readAllBytes(refImgFile.toPath()));
    }

    public void actualTest(AppiumDriver driver) throws URISyntaxException, IOException {
        WebDriverWait wait = new WebDriverWait(driver, 10);

        try {
            // get to the photo view
            wait.until(ExpectedConditions.presenceOfElementLocated(photos)).click();

            // wait for and click the correct image using a reference image template
            By sunriseImage = MobileBy.image(getReferenceImageB64());
            wait.until(ExpectedConditions.presenceOfElementLocated(sunriseImage)).click();

            // verify that the resulting alert proves we clicked the right image
            wait.until(ExpectedConditions.alertIsPresent());
            String alertText = driver.switchTo().alert().getText();
            Assert.assertEquals(alertText, Matchers.containsString("sunrise"));
        } finally {
            driver.quit();
        }
    }
}
Bạn có thể thấy là có thê một hàm MobileBy.image(getReferenceImageB64()); ở trong đoạn code này.



Post Reply

Return to “Dạy Appium - Mobile Automated Test”