Skip to content

Commit

Permalink
Make label comparison in xpaths case-insensitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrevarthen committed Sep 27, 2018
1 parent 0349b82 commit bb0a8ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.detroitlabs.katalonmobileutil.device.Device;

public class XPathBuilder {

private static final String upperCase = "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'";
private static final String lowerCase = "'abcdefghijklmnopqrstuvwxyz'";

// TODO: Make this more generic to build xpath for all types?
public static String xpathForLabelWithResourceId(String resourceId) {
Expand Down Expand Up @@ -139,10 +142,11 @@ public static String addChildWithType(String xpath, String type) {
}

private static String textXPath(String labelText) {
// xpath 1 doesn't have a way to ignore case, so we need to force-lowercase both strings
if (Device.isIOS()) {
return "@label='" + labelText + "'";
return "translate(@label, " + upperCase + ", " + lowerCase + ") = '" + labelText.toLowerCase() + "'";
} else {
return "@text='" + labelText + "'";
return "translate(@text, " + upperCase + ", " + lowerCase + ") = '" + labelText.toLowerCase() + "'";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public void whenXPathAlreadyIncludesProperties_addingLabelProperty_updatesXPathW

// iOS
String xpath = "//*[equals(@type, 'XCUITypeButton')]";
assertEquals("//*[equals(@type, 'XCUITypeButton') and @label='My Label']", XPathBuilder.addLabel(xpath, "My Label"));
assertEquals("//*[equals(@type, 'XCUITypeButton') and translate(@label, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.addLabel(xpath, "My Label"));

// Android
PowerMockito.when(Device.isIOS()).thenReturn(false);
xpath = "//*[contains(@class, 'TextView')]";
assertEquals("//*[contains(@class, 'TextView') and @text='My Label']", XPathBuilder.addLabel(xpath, "My Label"));
assertEquals("//*[contains(@class, 'TextView') and translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.addLabel(xpath, "My Label"));
}

@Test
Expand All @@ -96,12 +96,12 @@ public void whenXPathDoesNotIncludeProperties_addingLabelProperty_updatesXPathWi

// iOS
String xpath = "//XCUITypeButton";
assertEquals("//XCUITypeButton[@label='My Label']", XPathBuilder.addLabel(xpath, "My Label"));
assertEquals("//XCUITypeButton[translate(@label, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.addLabel(xpath, "My Label"));

// Android
PowerMockito.when(Device.isIOS()).thenReturn(false);
xpath = "//TextView";
assertEquals("//TextView[@text='My Label']", XPathBuilder.addLabel(xpath, "My Label"));
assertEquals("//TextView[translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.addLabel(xpath, "My Label"));
}

@Test
Expand Down Expand Up @@ -135,35 +135,35 @@ public void creatingAnXPathForALabelWithResourceId_returnsAnXPathWithALabel() {
public void creatingAnXPathForALabel_returnsAnXPathWithALabel() {

// iOS
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and @name='my_label' and @label='My Label']", XPathBuilder.xpathForLabel("my_label", "My Label"));
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and @name='my_label' and translate(@label, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForLabel("my_label", "My Label"));

// Android
PowerMockito.when(Device.isIOS()).thenReturn(false);
assertEquals("//*[contains(@class, 'TextView') and contains(@resource-id, 'my_label') and @text='My Label']", XPathBuilder.xpathForLabel("my_label", "My Label"));
assertEquals("//*[contains(@class, 'TextView') and contains(@resource-id, 'my_label') and translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForLabel("my_label", "My Label"));
}

@Test
@DisplayName("creating an xpath for label with text creates an xpath with text")
public void creatingAnXPathForALabel_returnsAnXPathWithText() {

// iOS
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and @label='My Label']", XPathBuilder.xpathForLabelWithText("My Label"));
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and translate(@label, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForLabelWithText("My Label"));

// Android
PowerMockito.when(Device.isIOS()).thenReturn(false);
assertEquals("//*[contains(@class, 'TextView') and @text='My Label']", XPathBuilder.xpathForLabelWithText("My Label"));
assertEquals("//*[contains(@class, 'TextView') and translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForLabelWithText("My Label"));
}

@Test
@DisplayName("creating an xpath for checkbox with text creates an xpath with text")
public void creatingAnXPathForACheckbox_returnsAnXPathWithText() {

// iOS
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and @label='My Label']", XPathBuilder.xpathForCheckboxWithText("My Label"));
assertEquals("//*[equals(@type, 'XCUIElementTypeStaticText') and translate(@label, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForCheckboxWithText("My Label"));

// Android
PowerMockito.when(Device.isIOS()).thenReturn(false);
assertEquals("//*[contains(@class, 'CheckBox') and @text='My Label']", XPathBuilder.xpathForCheckboxWithText("My Label"));
assertEquals("//*[contains(@class, 'CheckBox') and translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'my label']", XPathBuilder.xpathForCheckboxWithText("My Label"));
}

@Test
Expand Down

0 comments on commit bb0a8ef

Please sign in to comment.