Jacobvu84 / serenity-screenplay-junit-webdriver

Quản lý giáo trình cho khoá học
0 stars 2 forks source link

Remember and recall question #34

Open Jacobvu84 opened 4 years ago

Jacobvu84 commented 4 years ago

Remember

private String noteKey = "stickerLocation";
private String stickerImage = "sticker_a.png";

actor.remember(noteKey, locationOf(getImageData(stickerImage)));

....

actor.should(seeThat(
          locationOf(getImageData(stickerImage)),equalTo(actor.recall(noteKey))))
Jacobvu84 commented 4 years ago

Question

import static com.linecorp.questions.core.ScreenshotQuestion.screenshot;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.Point;

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Question;

import com.linecorp.utils.DriverUtils;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;
import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;

public class LocationQuestion implements Question<Point> {
    private File image;

    private LocationQuestion(File image) {
        this.image = image;
    }

    public static LocationQuestion locationOf(File image) {
        return new LocationQuestion(image);
    }

    @Override
    public Point answeredBy(Actor actor) {
        try {
            AppiumDriver driver = (AppiumDriver) DriverUtils.getDriverFrom(actor);
            File screenshot = screenshot().answeredBy(actor);
            OccurrenceMatchingResult location = driver.findImageOccurrence(screenshot, image, new OccurrenceMatchingOptions().withEnabledVisualization());
            location.storeVisualization(new File("DebugOccurrence.png"));
            return new Point(location.getRect().x, location.getRect().y);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Jacobvu84 commented 4 years ago