Skip to content

Commit

Permalink
bug: webelement can not re use.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Sep 16, 2022
1 parent 298457b commit 4592393
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 126 deletions.
109 changes: 19 additions & 90 deletions src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.cloud.sonic.driver.android;

import com.alibaba.fastjson.JSONObject;
import org.cloud.sonic.driver.android.enmus.AndroidSelector;
import org.cloud.sonic.driver.android.service.UiaClient;
import org.cloud.sonic.driver.android.service.impl.UiaClientImpl;
import org.cloud.sonic.driver.common.service.WebElement;
Expand Down Expand Up @@ -220,24 +221,13 @@ public void setDefaultFindElementInterval(Integer retry, Integer interval) {
/**
* find element in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @return
* @throws SonicRespException
*/
public WebElement findElement(IOSSelector iosSelector, String value) throws SonicRespException {
return findElement(iosSelector, value, null);
}

/**
* find element in device.
*
* @param xcuiElementType
* @return
* @throws SonicRespException
*/
public WebElement findElement(XCUIElementType xcuiElementType) throws SonicRespException {
return findElement(xcuiElementType, null);
public WebElement findElement(AndroidSelector androidSelector, String value) throws SonicRespException {
return findElement(androidSelector, value, null);
}

/**
Expand All @@ -255,26 +245,14 @@ public WebElement findElement(String selector, String value) throws SonicRespExc
/**
* find element in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @param retry
* @return
* @throws SonicRespException
*/
public WebElement findElement(IOSSelector iosSelector, String value, Integer retry) throws SonicRespException {
return findElement(iosSelector, value, retry, null);
}

/**
* find element in device.
*
* @param xcuiElementType
* @param retry
* @return
* @throws SonicRespException
*/
public WebElement findElement(XCUIElementType xcuiElementType, Integer retry) throws SonicRespException {
return findElement(xcuiElementType, retry, null);
public WebElement findElement(AndroidSelector androidSelector, String value, Integer retry) throws SonicRespException {
return findElement(androidSelector, value, retry, null);
}

/**
Expand All @@ -293,28 +271,15 @@ public WebElement findElement(String selector, String value, Integer retry) thro
/**
* find element in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public WebElement findElement(IOSSelector iosSelector, String value, Integer retry, Integer interval) throws SonicRespException {
return findElement(iosSelector.getSelector(), value, retry, interval);
}

/**
* find element in device.
*
* @param xcuiElementType
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public WebElement findElement(XCUIElementType xcuiElementType, Integer retry, Integer interval) throws SonicRespException {
return findElement(IOSSelector.CLASS_NAME.getSelector(), xcuiElementType.getType(), retry, interval);
public WebElement findElement(AndroidSelector androidSelector, String value, Integer retry, Integer interval) throws SonicRespException {
return findElement(androidSelector.getSelector(), value, retry, interval);
}

/**
Expand All @@ -334,24 +299,13 @@ public WebElement findElement(String selector, String value, Integer retry, Inte
/**
* find element list in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value) throws SonicRespException {
return findElementList(iosSelector, value, null);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType) throws SonicRespException {
return findElementList(xcuiElementType, null);
public List<WebElement> findElementList(AndroidSelector androidSelector, String value) throws SonicRespException {
return findElementList(androidSelector, value, null);
}

/**
Expand All @@ -369,26 +323,14 @@ public List<WebElement> findElementList(String selector, String value) throws So
/**
* find element list in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @param retry
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value, Integer retry) throws SonicRespException {
return findElementList(iosSelector, value, retry, null);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @param retry
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType, Integer retry) throws SonicRespException {
return findElementList(xcuiElementType, retry, null);
public List<WebElement> findElementList(AndroidSelector androidSelector, String value, Integer retry) throws SonicRespException {
return findElementList(androidSelector, value, retry, null);
}

/**
Expand All @@ -407,28 +349,15 @@ public List<WebElement> findElementList(String selector, String value, Integer r
/**
* find element list in device.
*
* @param iosSelector
* @param androidSelector
* @param value
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value, Integer retry, Integer interval) throws SonicRespException {
return findElementList(iosSelector.getSelector(), value, retry, interval);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType, Integer retry, Integer interval) throws SonicRespException {
return findElementList(IOSSelector.CLASS_NAME.getSelector(), xcuiElementType.getType(), retry, interval);
public List<WebElement> findElementList(AndroidSelector androidSelector, String value, Integer retry, Integer interval) throws SonicRespException {
return findElementList(androidSelector.getSelector(), value, retry, interval);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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:https://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.driver.android.enmus;

public enum AndroidSelector {
CLASS_NAME("class name"),
Id("id"),
ACCESSIBILITY_ID("accessibility id"),

XPATH("xpath"),

CLASS_CHAIN("-android uiautomator");

private final String selector;
AndroidSelector(String selector) {
this.selector = selector;
}

public String getSelector() {
return selector;
}
}
50 changes: 14 additions & 36 deletions src/test/java/org/cloud/sonic/driver/android/AndroidDriverTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cloud.sonic.driver.android;

import com.alibaba.fastjson.JSONObject;
import org.cloud.sonic.driver.android.enmus.AndroidSelector;
import org.cloud.sonic.driver.common.service.WebElement;
import org.cloud.sonic.driver.common.tool.SonicRespException;
import org.cloud.sonic.driver.common.models.WindowSize;
Expand Down Expand Up @@ -57,7 +58,7 @@ public static void beforeClass() throws SonicRespException {

@AfterClass
public static void afterClass() throws SonicRespException {
androidDriver.closeDriver();
// androidDriver.closeDriver();
}

@Test
Expand All @@ -75,53 +76,30 @@ public void testGetWindowSize() throws SonicRespException {

@Test
public void testClipboard() throws SonicRespException {
androidDriver.setPasteboard(PasteboardType.PLAIN_TEXT,"abc");
androidDriver.setPasteboard(PasteboardType.PLAIN_TEXT, "abc");
// System.out.println(androidDriver.getPasteboard(PasteboardType.PLAIN_TEXT));
}

@Test
public void testFindElement() throws SonicRespException, InterruptedException, IOException {
Boolean hasThrow = false;
try {
androidDriver.findElement("id", "android:id/content").click();
androidDriver.findElement("id", "android:id/content1").click();
} catch (Throwable e) {
hasThrow = true;
Assert.assertEquals(SonicRespException.class, e.getClass());
Assert.assertTrue(e.getMessage().contains("unable to find an element"));
Assert.assertTrue(e.getMessage().contains("An element could not be located on the page using the given search parameters"));
}
Assert.assertTrue(hasThrow);
Thread.sleep(2000);
androidDriver.findElement("id", "android:id/content").click();
// iosDriver.findElement(XCUIElementType.ANY);
// Thread.sleep(2000);
// iosDriver.pressButton(SystemButton.HOME);
// Thread.sleep(2000);
// iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "地图").click();
// Thread.sleep(2000);
// iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "搜索地点或地址").click();
// WebElement w = iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "搜索地点或地址");
// String text = UUID.randomUUID().toString().substring(0, 6) + "中文";
// 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);
// Assert.assertTrue(iosRect.getCenter().getX() > 0);
// Assert.assertTrue(iosRect.getCenter().getY() > 0);
// byte[] bt = w.screenshot();
// File output = new File("./" + UUID.randomUUID() + ".png");
// FileImageOutputStream imageOutput = new FileImageOutputStream(output);
// imageOutput.write(bt, 0, bt.length);
// imageOutput.close();
// output.delete();
// iosDriver.findElement(IOSSelector.ACCESSIBILITY_ID, "取消").click();
// iosDriver.setDefaultFindElementInterval(null, 3000);
// iosDriver.setDefaultFindElementInterval(5, null);
// iosDriver.setDefaultFindElementInterval(null, null);
// iosDriver.pressButton(SystemButton.HOME);
androidDriver.findElement(AndroidSelector.Id, "android:id/content").click();
Thread.sleep(2000);
WebElement w = androidDriver.findElement(AndroidSelector.XPATH, "//*[@text='标题']");
w.click();
w.sendKeys("hello");
w.clear();
androidDriver.setDefaultFindElementInterval(null, 3000);
androidDriver.setDefaultFindElementInterval(5, null);
androidDriver.setDefaultFindElementInterval(null, null);
}
}

0 comments on commit 4592393

Please sign in to comment.