Jacobvu84 / serenity-archetype-mobile

Sample project
0 stars 1 forks source link

Click by offset #19

Open Jacobvu84 opened 5 years ago

Jacobvu84 commented 5 years ago
Tap.on(TargetOption.element(theActorInTheSpotlight(), ChatScreen.COLLAPSE_BUTTON)
                                                     .adjustByMultiplyBaseSize(0.9f, -0.9f)
Jacobvu84 commented 5 years ago
package com.linecorp.interactions;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Optional.ofNullable;

import java.util.HashMap;
import java.util.Map;

import net.serenitybdd.core.pages.WebElementFacade;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.targets.Target;

import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class TargetOption extends PointOption<ElementOption> {

    private Target target;
    private Actor actor;
    private float xTimes = 0;
    private float yTimes = 0;

    public static TargetOption element(Actor actor, Target target) {
        return new TargetOption().withActor(actor).withElement(target);
    }

    public TargetOption withElement(Target target) {
        checkNotNull(target);
        this.target = target;
        return this;
    }

    public TargetOption withActor(Actor actor) {
        checkNotNull(actor);
        this.actor = actor;
        return this;
    }

    public TargetOption adjustByMultiplyBaseSize(float xTimes, float yTimes) {
        this.xTimes = xTimes;
        this.yTimes = yTimes;
        return this;
    }

    @Override
    protected void verify() {
        ofNullable(target).orElseThrow(() -> new IllegalArgumentException("Target should be defined"));
    }

    @Override
    public Map<String, Object> build() {
        verify();

        WebElementFacade element = target.resolveFor(actor);
        int xPoint = element.getLocation().getX();
        int yPoint = element.getLocation().getY();
        xPoint += (element.getSize().width * xTimes);
        yPoint += (element.getSize().height * yTimes);

        final Map<String, Object> result = new HashMap<>();
        result.put("x", xPoint);
        result.put("y", yPoint);
        return result;
    }
}