Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.unittest.test;

import android.content.Context;
import android.content.Intent;

import androidx.test.platform.app.InstrumentationRegistry;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import io.appium.uiautomator2.model.By;
import io.appium.uiautomator2.unittest.fixture.ClassRegexActivity;
import io.appium.uiautomator2.unittest.test.internal.BaseTest;
import io.appium.uiautomator2.unittest.test.internal.Response;

import static io.appium.uiautomator2.unittest.test.internal.TestUtils.extractElementId;
import static io.appium.uiautomator2.unittest.test.internal.TestUtils.waitForElement;
import static io.appium.uiautomator2.unittest.test.internal.commands.DeviceCommands.findElements;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ClassRegexCommandsTest extends BaseTest {
@Before
@Override
public void launchAUT() throws JSONException {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
context.startActivity(new Intent(context, ClassRegexActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
assertTrue(waitForElement(By.accessibilityId("regex-button-1")).isSuccessful());
}

@Test
public void findsMatchesAtTheSameIndexUnderDifferentParents() throws JSONException {
// Both labels have index 0, so enumerating index(0), index(1), ... loses one.
assertRegexMatchesExactClass("android.widget.TextView");
}

@Test
public void findsMatchesWhenNoMatchHasIndexZero() throws JSONException {
// Both buttons have index 1, so index(0) stops the old enumeration immediately.
assertRegexMatchesExactClass("android.widget.Button");
}

@Test
public void preservesExplicitInstance() throws JSONException {
for (int i = 0; i < 2; i++) {
Set<String> expected = elementIds(By.accessibilityId("regex-button-" + i));
assertEquals(1, expected.size());
assertEquals(expected, elementIds(By.androidUiAutomator(
selector("android.widget.Button", true) + ".instance(" + i + ")")));
}
}

private void assertRegexMatchesExactClass(String className) throws JSONException {
Set<String> expected = elementIds(By.androidUiAutomator(selector(className, false)));
assertEquals("The fixture must expose two distinct matching elements", 2, expected.size());
assertEquals(expected, elementIds(By.androidUiAutomator(selector(className, true))));
}

private String selector(String className, boolean regex) {
return "new UiSelector().packageName(\"io.appium.uiautomator2.e2etest\")."
+ (regex ? "classNameMatches" : "className")
+ "(\"" + (regex ? className.replace(".", "[.]") : className) + "\")";
}

private Set<String> elementIds(By by) throws JSONException {
Response response = findElements(by);
assertTrue(by + " should succeed", response.isSuccessful());
JSONArray elements = response.getValue();
Set<String> result = new HashSet<>();
for (int i = 0; i < elements.length(); i++) {
String id = extractElementId(elements.getJSONObject(i));
assertNotNull(id);
assertTrue("Duplicate element: " + id, result.add(id));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name="io.appium.uiautomator2.unittest.fixture.ClassRegexActivity"
android:exported="false"
android:theme="@android:style/Theme.Material.Light.NoActionBar" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.unittest.fixture;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/** A fixed hierarchy for testing regex selectors across different parents. */
public class ClassRegexActivity extends Activity {
// Fixed strings are intentional: this screen is only a test fixture.
@SuppressLint("SetTextI18n")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 2; i++) {
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.VERTICAL);
TextView label = new TextView(this);
label.setText("Label " + i);
label.setContentDescription("regex-label-" + i);
row.addView(label);
Button button = new Button(this);
button.setText("Button " + i);
button.setContentDescription("regex-button-" + i);
row.addView(button);
root.addView(row);
}
setContentView(root);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,17 @@ public static List<AccessibleUiObject> matchDescendantElements(UiSelector sel,
: Collections.singletonList(instanceObj);
}

final boolean useIndex = doesUiSelectorHaveAttribute(sel, "CLASS_REGEX");
if (useIndex) {
Logger.debug("Selector has CLASS_REGEX attribute");
}
final List<AccessibleUiObject> elements = new ArrayList<>();
int descendantIndex = 0;
do {
AccessibleUiObject lastFoundObj;
UiSelector tmpSelector = sel.instance(descendantIndex);
Logger.debug(String.format("matchDescendantElements temporary selector: %s", tmpSelector));
if (context == null) {
UiSelector tmpSelector = useIndex ? sel.index(descendantIndex) : sel.instance(descendantIndex);
Logger.debug(String.format("matchDescendantElements temporary selector: %s", tmpSelector));
lastFoundObj = toAccessibleUiObject(getUiDevice().findObject(tmpSelector));
} else {
try {
lastFoundObj = context.getChild(sel.instance(descendantIndex));
lastFoundObj = context.getChild(tmpSelector);
} catch (UiObjectNotFoundException e) {
lastFoundObj = null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/uiautomator2-server/scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ adb shell settings put global hidden_api_policy_p_apps 1
# bottom edge then becomes the system home gesture, which breaks scroll tests in landscape.
adb shell cmd overlay enable com.android.internal.systemui.navbar.threebutton || true

classes=(AlertCommandsTest ActionsCommandsTest ElementCommandsTest DeviceCommandsTest)
classes=(AlertCommandsTest ActionsCommandsTest ElementCommandsTest DeviceCommandsTest ClassRegexCommandsTest)
did_fail=0
for cls_name in "${classes[@]}"; do
if ! ./gradlew connectedE2eTestDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=io.appium.uiautomator2.unittest.test.$cls_name; then
Expand Down
Loading