Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkbox get label improvement #62

Closed
wants to merge 4 commits into from
Closed
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
Expand Up @@ -3,6 +3,10 @@
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import ru.yandex.qatools.htmlelements.utils.HtmlElementUtils;

import java.util.LinkedList;
import java.util.List;

/**
* Represents web page checkbox control.
Expand All @@ -21,14 +25,52 @@ public CheckBox(WebElement wrappedElement) {
}

/**
* Finds label corresponding to this checkbox using "following-sibling::label" xpath.
* Finds label corresponding to checkbox element
*
* @return {@code WebElement} representing label or {@code null} if no label has been found.
*/
public WebElement getLabel() {
WebElement label = null;
String id = null;

// Trying to find id attibute in element
try {
return getWrappedElement().findElement(By.xpath("following-sibling::label"));
} catch (NoSuchElementException e) {
id = HtmlElementUtils.xpathLiteral(getWrappedElement().getAttribute("id"));
} catch (NullPointerException | NoSuchElementException ex) {
// Do nothing. Element doesn't have id attribute
}

if (id != null) {
// Label with matching "for" attribute
// Trying to find element from !ROOT! node
String xpath = String.format("(//label[@for = %s])[1]", id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

label = findLabel(By.xpath(xpath));
}

if (label == null) {
// Label wrapped around checkbox
label = findLabel(By.xpath("parent::label"));
}

if (label == null) {
// Label right next to checkbox
label = findLabel(By.xpath("following-sibling::label"));
}

return label;
}

/**
* Finds label in checkbox element
*
* @param by path to label
* @return {@code WebElement} representing label or {@code null} if label not found
*/
private WebElement findLabel(By by) {
try {
return getWrappedElement().findElement(by);

} catch (NoSuchElementException ex) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.lang.reflect.*;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import static org.apache.commons.lang3.reflect.ConstructorUtils.invokeConstructor;
Expand Down Expand Up @@ -177,4 +178,40 @@ public static boolean existsInClasspath(final String fileName) {
public static URL getResourceFromClasspath(final String fileName) {
return Thread.currentThread().getContextClassLoader().getResource(fileName);
}

public static String xpathLiteral(String s) {
if (s.indexOf("'") == -1) {
return String.format("'%s'", s);
}

if (s.indexOf("\"") == -1) {
return String.format("\"%s\"", s);
}

String string = s;
List<String> parts = new LinkedList<>();

while (true) {
int pos;
if ((pos = string.indexOf("'")) != -1) {
parts.add(string.substring(0, pos));
parts.add("\"'\"");

string = string.substring(pos + 1);
} else {
parts.add(String.format("'%s'", string));
break;
}
}

StringBuilder result = new StringBuilder();
for (String part: parts) {
result.append(part);
result.append(",");
}
result.deleteCharAt(result.length() - 1);

return result.toString();
}

}