Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Jul 31, 2022
1 parent 0237754 commit 75b7202
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/cloud/sonic/core/ios/models/IOSRect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) [SonicCloudOrg] Sonic Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cloud.sonic.core.ios.models;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class IOSRect {
private int x;
private int y;
private int width;
private int height;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.cloud.sonic.core.ios.service;

import org.cloud.sonic.core.ios.models.IOSRect;
import org.cloud.sonic.core.tool.SonicRespException;

/**
Expand All @@ -29,4 +30,10 @@ public interface WebElement {
void sendKeys(String text) throws SonicRespException;

void sendKeys(String text,int frequency) throws SonicRespException;

void clear() throws SonicRespException;

String getText() throws SonicRespException;

IOSRect getRect() throws SonicRespException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.cloud.sonic.core.ios.models.BaseResp;
import org.cloud.sonic.core.ios.models.IOSRect;
import org.cloud.sonic.core.ios.service.WdaClient;
import org.cloud.sonic.core.ios.service.WebElement;
import org.cloud.sonic.core.tool.SonicRespException;
Expand Down Expand Up @@ -61,12 +63,58 @@ public void sendKeys(String text, int frequency) throws SonicRespException {
data.put("frequency", frequency);
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createPost(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/value"));
+ wdaClient.getSessionId() + "/element/" + id + "/value")
.body(data.toJSONString()), 60000);
if (b.getErr() == null) {
log.info("send key to {}.", id);
} else {
log.error("send key to {} failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void clear() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createPost(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/clear"), 60000);
if (b.getErr() == null) {
log.info("clear {}.", id);
} else {
log.error("clear {} failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public String getText() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createGet(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/text"));
if (b.getErr() == null) {
log.info("get {} text {}.", id, b.getValue());
return b.getValue().toString();
} else {
log.error("get {} text failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public IOSRect getRect() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createGet(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/rect"));
if (b.getErr() == null) {
log.info("get {} rect {}.", id, b.getValue());
IOSRect iosRect = JSON.parseObject(b.getValue().toString(),IOSRect.class);
return iosRect;
} else {
log.error("get {} rect failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}
}
25 changes: 21 additions & 4 deletions src/test/java/org/cloud/sonic/core/ios/IOSDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.fastjson.JSONObject;
import org.cloud.sonic.core.ios.enums.*;
import org.cloud.sonic.core.ios.models.*;
import org.cloud.sonic.core.ios.service.WebElement;
import org.cloud.sonic.core.tool.SonicRespException;
import org.junit.*;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -124,7 +125,7 @@ public void testPasteboard() throws SonicRespException, InterruptedException {
iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "WebDriverAgentRunner-Runner").click();
iosDriver.setPasteboard(PasteboardType.PLAIN_TEXT, text);
Thread.sleep(1000);
Assert.assertEquals(text, new String(iosDriver.getPasteboard(PasteboardType.PLAIN_TEXT.getType())));
Assert.assertEquals(text, new String(iosDriver.getPasteboard(PasteboardType.PLAIN_TEXT)));
iosDriver.pressButton(SystemButton.HOME);
}

Expand Down Expand Up @@ -206,13 +207,29 @@ public void testSession() {
}

@Test
public void testFindElement() throws SonicRespException {
public void testFindElement() throws SonicRespException, InterruptedException {
iosDriver.pressButton(SystemButton.HOME);
Thread.sleep(2000);
iosDriver.findElement("accessibility id", "地图").click();
iosDriver.findElement(XCUIElementType.ANY);
Thread.sleep(2000);
iosDriver.pressButton(SystemButton.HOME);
Thread.sleep(2000);
iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "地图").click();
iosDriver.pressButton(SystemButton.HOME);
iosDriver.findElement(XCUIElementType.ANY);
Thread.sleep(2000);
iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "搜索地点或地址").click();
WebElement w = iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "搜索地点或地址");
String text = UUID.randomUUID().toString();
w.sendKeys(text);
Assert.assertEquals(text, w.getText());
w.clear();
Assert.assertEquals("搜索地点或地址", w.getText());
IOSRect iosRect = w.getRect();
Assert.assertTrue(iosRect.getX() > 0);
Assert.assertTrue(iosRect.getY() > 0);
Assert.assertTrue(iosRect.getWidth() > 0);
Assert.assertTrue(iosRect.getHeight() > 0);
iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "取消").click();
iosDriver.pressButton(SystemButton.HOME);
}

Expand Down

0 comments on commit 75b7202

Please sign in to comment.