How to use build method of io.appium.java_client.ios.IOSStartScreenRecordingOptions class

Best io.appium code snippet using io.appium.java_client.ios.IOSStartScreenRecordingOptions.build

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

IosDevice.java

Source: IosDevice.java Github

copy
1package com.daxiang.core.ios;
2
3import com.daxiang.App;
4import com.daxiang.core.MobileDevice;
5import com.daxiang.core.appium.AppiumServer;
6import com.daxiang.core.appium.IosDriverBuilder;
7import com.daxiang.core.appium.IosPageSourceHandler;
8import com.daxiang.model.Device;
9import com.daxiang.utils.Terminal;
10import com.daxiang.utils.UUIDUtil;
11import io.appium.java_client.AppiumDriver;
12import io.appium.java_client.ios.IOSDriver;
13import io.appium.java_client.ios.IOSStartScreenRecordingOptions;
14import lombok.extern.slf4j.Slf4j;
15import org.apache.commons.exec.ExecuteWatchdog;
16import org.apache.commons.io.FileUtils;
17import org.dom4j.DocumentException;
18import org.springframework.util.StringUtils;
19import org.springframework.web.client.RestTemplate;
20
21import java.io.File;
22import java.io.IOException;
23import java.time.Duration;
24import java.util.Base64;
25
26/**
27 * Created by jiangyitao.
28 */
29@Slf4j
30public class IosDevice extends MobileDevice {
31
32    /**
33     * iproxy localPort remotePort deviceId
34     */
35    private static final String IPROXY = "iproxy %d %d %s";
36    private ExecuteWatchdog mjpegServerIproxyWatchdog;
37
38    public IosDevice(Device device, AppiumServer appiumServer) {
39        super(device, appiumServer);
40    }
41
42    @Override
43    public AppiumDriver newAppiumDriver() {
44        return new IosDriverBuilder().build(this, false);
45    }
46
47    @Override
48    public AppiumDriver initAppiumDriver() {
49        return new IosDriverBuilder().build(this, true);
50    }
51
52    @Override
53    public File screenshot() throws Exception {
54        return IosUtil.screenshotByIdeviceScreenshot(getId());
55    }
56
57    @Override
58    public void installApp(File app) throws IOException {
59        IosUtil.installIpa(app.getAbsolutePath(), getId());
60    }
61
62    @Override
63    public String dump() throws IOException, DocumentException {
64        return new IosPageSourceHandler(getAppiumDriver()).getPageSource();
65    }
66
67    @Override
68    public void startRecordingScreen() {
69        IOSStartScreenRecordingOptions iosOptions = new IOSStartScreenRecordingOptions();
70        // The maximum value is 30 minutes.
71        iosOptions.withTimeLimit(Duration.ofMinutes(30));
72        iosOptions.withFps(10); // default 10
73        iosOptions.withVideoQuality(IOSStartScreenRecordingOptions.VideoQuality.LOW);
74        iosOptions.withVideoType("libx264");
75        ((IOSDriver) getAppiumDriver()).startRecordingScreen(iosOptions);
76    }
77
78    @Override
79    public File stopRecordingScreen() throws IOException {
80        File videoFile = new File(UUIDUtil.getUUID() + ".mp4");
81        String base64Video = ((IOSDriver) getAppiumDriver()).stopRecordingScreen();
82        FileUtils.writeByteArrayToFile(videoFile, Base64.getDecoder().decode(base64Video), false);
83        return videoFile;
84    }
85
86    @Override
87    public void installApp(String appDownloadUrl) throws Exception {
88        if (StringUtils.isEmpty(appDownloadUrl)) {
89            throw new RuntimeException("appDownloadUrl cannot be empty");
90        }
91
92        // download ipa
93        RestTemplate restTemplate = App.getBean(RestTemplate.class);
94        byte[] ipaBytes = restTemplate.getForObject(appDownloadUrl, byte[].class);
95
96        File ipa = new File(UUIDUtil.getUUID() + ".ipa");
97        try {
98            FileUtils.writeByteArrayToFile(ipa, ipaBytes, false);
99            // install
100            IosUtil.installIpa(ipa.getAbsolutePath(), getId());
101        } finally {
102            // delete ipa
103            FileUtils.deleteQuietly(ipa);
104        }
105    }
106
107    public int getMjpegServerPort() {
108        Object mjpegServerPort = getAppiumDriver().getCapabilities().asMap().get("mjpegServerPort");
109        return (int) ((long) mjpegServerPort);
110    }
111
112    public void startMjpegServerIproxy() throws IOException {
113        int mjpegServerPort = getMjpegServerPort();
114        String cmd = String.format(IPROXY, mjpegServerPort, mjpegServerPort, getId());
115        log.info("[ios][{}]mjpegServer: {}", getId(), cmd);
116        mjpegServerIproxyWatchdog = Terminal.executeAsyncAndGetWatchdog(cmd);
117    }
118
119    public void stopMjpegServerIproxy() {
120        if (mjpegServerIproxyWatchdog != null) {
121            log.info("[ios][{}]mjpegServer iproxy stop", getId());
122            mjpegServerIproxyWatchdog.destroyProcess();
123        }
124    }
125}
126
Full Screen

TestMobileApp.java

Source: TestMobileApp.java Github

copy
1package jp.shiftinc.automation.trial;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;
6import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;
7import io.appium.java_client.ios.IOSStartScreenRecordingOptions;
8import io.appium.java_client.screenrecording.CanRecordScreen;
9import io.qameta.allure.Allure;
10import io.qameta.allure.Attachment;
11import io.qameta.allure.Feature;
12import io.qameta.allure.Step;
13import org.junit.jupiter.api.AfterEach;
14import org.junit.jupiter.api.BeforeAll;
15import org.junit.jupiter.api.TestInstance;
16import org.junit.jupiter.params.ParameterizedTest;
17import org.junit.jupiter.params.provider.Arguments;
18import org.junit.jupiter.params.provider.MethodSource;
19import org.openqa.selenium.OutputType;
20import org.openqa.selenium.WebElement;
21
22import javax.imageio.ImageIO;
23import java.awt.image.BufferedImage;
24import java.io.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.File;
27import java.io.IOException;
28import java.nio.file.Files;
29import java.nio.file.Paths;
30import java.util.Base64;
31import java.util.concurrent.TimeUnit;
32import java.util.stream.Stream;
33
34import static org.junit.jupiter.api.Assertions.assertNotNull;
35import static org.junit.jupiter.params.provider.Arguments.arguments;
36
37@SuppressWarnings({"SameParameterValue", "FieldCanBeLocal"})
38@Feature("Appiumイメージセレクタテスト")
39@TestInstance(TestInstance.Lifecycle.PER_CLASS)
40class TestMobileApp {
41    private AppiumDriver driver;
42    private OSBase osInfo;
43
44    @BeforeAll
45    void setupClass() {
46    }
47
48    @Step("{osName}/イメージセレクタでログイン")
49    @ParameterizedTest
50    @MethodSource("osMartix")
51    void imageSelectorTest(String osName) throws IOException, InterruptedException {
52        setup(osName);
53
54        // イメージセレクタ
55        WebElement loginElm = driver.findElementByImage(getReferenceImageB64("racine_sut_login.png"));
56        // visualization
57        String imageResult = loginElm.getAttribute("visual");
58        // Allureにアタッチ(サービス関数経由)
59        addAttachment(Base64.getDecoder().decode(imageResult), "image-selector");
60        loginElm.click();
61
62        Thread.sleep(20000);
63        //addAttachment(driver.getScreenshotAs(OutputType.BYTES),"img1");
64        // テンプレートマッチング
65        OccurrenceMatchingResult result = templateMatch("original_template.png");
66        // Allureにアタッチ(サービス関数経由)
67        addAttachment(Base64.getDecoder().decode(result.getVisualization()), "template-matched-result");
68        assertNotNull(result.getRect());
69    }
70
71    Stream<Arguments> osMartix() {
72        return Stream.of(
73                arguments("ios"),
74                arguments("android")
75        );
76    }
77
78    @AfterEach
79    void tearDown() throws IOException {
80        if (driver != null) {
81            String base64Video = ((CanRecordScreen) driver).stopRecordingScreen();
82            String path = String.format("./build/test_%s.mp4",osInfo.osName());
83            Files.write(Paths.get(path), Base64.getDecoder().decode(base64Video));
84            attachVideo(path);
85            driver.closeApp();
86        }
87    }
88
89    private void setup(String os) {
90        osInfo =  os.equals("ios")? new IOSBase():new AndroidBase();
91        driver = osInfo.driver();
92        driver.launchApp();
93        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
94        if (os.equals("ios")) {
95            ((IOSBase)osInfo).iosDriver.startRecordingScreen(
96                    new IOSStartScreenRecordingOptions()
97                            .withVideoType("libx264")
98                            .withVideoScale("320:640")
99                            .withVideoQuality(IOSStartScreenRecordingOptions.VideoQuality.MEDIUM)
100            );
101        } else {
102            ((AndroidBase)osInfo).androidDriver.startRecordingScreen();
103        }
104        MobileElement loginId = (MobileElement) driver.findElement(osInfo.loginId());
105        MobileElement loginPwd = (MobileElement) driver.findElement(osInfo.loginPwd());
106        loginId.sendKeys("demo");
107        loginPwd.click();
108        loginPwd.sendKeys("demo");
109    }
110
111    private String getReferenceImageB64(String imageName) throws IOException {
112        BufferedImage image = ImageIO.read(new File(osInfo.resourceDir() + imageName));
113        ByteArrayOutputStream os = new ByteArrayOutputStream();
114        ImageIO.write(image, "png", os);
115        return Base64.getEncoder().encodeToString(os.toByteArray());
116    }
117
118    private OccurrenceMatchingResult templateMatch(String template) throws IOException {
119        return driver
120                .findImageOccurrence(
121                        driver.getScreenshotAs(OutputType.FILE),
122                        new File(osInfo.resourceDir() + template),
123                        new OccurrenceMatchingOptions()
124                                .withThreshold(0.6)
125                                .withEnabledVisualization());
126    }
127
128    private static void addAttachment(byte[] byteArray, String imageName) {
129        Allure.addAttachment(imageName, "image/png", new ByteArrayInputStream(byteArray), "png");
130    }
131
132    @SuppressWarnings("UnusedReturnValue")
133    @Attachment(value = "video",type="video/mp4",fileExtension = "mp4")
134    private byte[] attachVideo(String path) throws IOException {
135        return getFile(path);
136    }
137
138    private byte[] getFile(String fileName) throws IOException {
139        File file = new File(fileName);
140        return Files.readAllBytes(Paths.get(file.getAbsolutePath()));
141    }
142}
143
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger build code on LambdaTest Cloud Grid

Execute automation tests with build on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)