-
-
Save enepomnyaschih/5847929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.avisi.langur.testing; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.remote.RemoteWebElement; | |
import java.util.List; | |
public class JQuerySelector extends By { | |
private final String jQuerySelector; | |
public JQuerySelector(String selector) { | |
this(selector, true); | |
} | |
public JQuerySelector(String selector, boolean visible) { | |
this.jQuerySelector = selector + (visible ? ":visible" : ""); | |
} | |
@Override | |
public List<WebElement> findElements(SearchContext context) { | |
return (List<WebElement>) findWithJQuery(context, true); | |
} | |
@Override | |
public WebElement findElement(SearchContext context) { | |
return (WebElement) findWithJQuery(context, false); | |
} | |
private Object findWithJQuery(SearchContext context, boolean returnList) { | |
String getArgument = returnList ? "" : "0"; | |
if (context instanceof RemoteWebElement) { | |
WebDriver driver = ((RemoteWebElement) context).getWrappedDriver(); | |
return ((JavascriptExecutor) driver).executeScript("return $(arguments[0]).find('" + escape(jQuerySelector) + "').get(" + getArgument + ");", context); | |
} | |
return ((JavascriptExecutor) context).executeScript("return $('" + escape(jQuerySelector) + "').get(" + getArgument + ");"); | |
} | |
@Override | |
public String toString() { | |
return "By.jQuerySelector: " + jQuerySelector; | |
} | |
private String escape(String s) { | |
return s.replace("'", "\\'"); | |
} | |
} |
What would happen if Jquery is not included in the Application.
if jquery is not included with the application you have to inject it somehow.
https://gist.github.com/djangofan/7677995
one method also in java. I just used this and it works. There is also apparently a method to have selenium inject it as a user extension.
Please write an example code for how this class can be used.
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like the visibility addition, changed it in our own codebase!