Robotium???????WebElement???
???????????? ???????[ 2015/8/14 11:30:20 ] ????????????????? ??????????
????robotium??????WebView????robotium????getWebElements()??getWebElements(By by)??????????android?е?WebView???????????? clickOnWebElement??????????????.android?е???????????ù????????????WebView???????????????????
???????????????JS???????е????????
??????PC???????????????????????javascript???????????Chrome????????????????——JavaScript??????????????Ctrl+Shift+J???????? javascript:prompt(document.URL)???????????????URL???????????????д?????JS????????????????????????????????????RobotiumWeb.js????????????JS???????solo??getWebElements()?????
public ArrayList<WebElement> getWebElements(boolean onlySufficientlyVisible){
boolean javaScriptWasExecuted = executeJavaScriptFunction("allWebElements();");
return getWebElements(javaScriptWasExecuted?? onlySufficientlyVisible);
}
private boolean executeJavaScriptFunction(final String function){
final WebView webView = viewFetcher.getFreshestView(viewFetcher.getCurrentViews(WebView.class?? true));
if(webView == null){
return false;
}
//???ЩJS?????????????????????罫WebView????????????JS???????RobotiumWeb.js?е?????String???????
final String javaScript = prepareForStartOfJavascriptExecution();
activityUtils.getCurrentActivity(false).runOnUiThread(new Runnable() {
public void run() {
if(webView != null){
webView.loadUrl("javascript:" + javaScript + function);
}
}
});
return true;
}
????????????????????е???allWebElements();???????????????RobotiumWeb.js?????????JS??????Σ?
???????????????η??JavaScript??????п?Ч??
javascript:
function allWebElements() {
for (var key in document.all){
try{
promptElement(document.all[key]); //????promptElement??element??????
}catch(ignored){}
}
finished(); //??????????finished????????
}
function promptElement(element) {
var id = element.id;
var text = element.innerText;
if(text.trim().length == 0){
text = element.value;
}
var name = element.getAttribute('name');
var className = element.className;
var tagName = element.tagName;
var attributes = "";
var htmlAttributes = element.attributes;
for (var i = 0?? htmlAttribute; htmlAttribute = htmlAttributes[i]; i++){
attributes += htmlAttribute.name + "::" + htmlAttribute.value;
if (i + 1 < htmlAttributes.length) {
attributes += "#$";
}
}
var rect = element.getBoundingClientRect();
if(rect.width > 0 && rect.height > 0 && rect.left >= 0 && rect.top >= 0){
prompt(id + ';??' + text + ';??' + name + ";??" + className + ";??" + tagName + ";??" + rect.left + ';??' + rect.top + ';??' + rect.width + ';??' + rect.height + ';??' + attributes); //????????id??text??name????ε??????
}
}
function finished(){
prompt('robotium-finished'); //????????robotium-finished??????????????????????????н???
}
?????????п??????JS????????????????????????????????????????????;????????????????????????и???????????????????????finished()????????????????robotium-finished?????????????????????????????????????а???????????Android???????????
?????????????Android?л??WebView??prompt??????е????
??????Android??Webkit?????и?WebChromeClient????????е?onJsPrompt?????????????WebView?е??????????WebView????JS?????????????÷?????String message????????????????е?????????robotiumд????????WebChromeClient???RobotiumWebClient????д??onJsPrompt
onJsPrompt(WebView view?? String url?? String message?? String defaultValue?? JsPromptResult result)
@Override
public boolean onJsPrompt(WebView view?? String url?? String message?? String defaultValue?? JsPromptResult r) {
if(message != null && (message.contains(";??") || message.contains("robotium-finished"))){
//?????????а???robotium-finished???????????????JS?????????????
if(message.equals("robotium-finished")){
webElementCreator.setFinished(true);
}
else{
webElementCreator.createWebElementAndAddInList(message?? view);//??????????е????????????????????е???????д?????
}
r.confirm();
return true;
}
else {
if(originalWebChromeClient != null) {
return originalWebChromeClient.onJsPrompt(view?? url?? message?? defaultValue?? r);
}
return true;
}
}
?????????????WebView?????????????JS??????????????enableJavascriptAndSetRobotiumWebClient????????JavaScriptEnabled?????true??????WebChromeClient?????robotiumWebClient
????public void enableJavascriptAndSetRobotiumWebClient(List<WebView> webViews?? WebChromeClient originalWebChromeClient){
????this.originalWebChromeClient = originalWebChromeClient;
????for(final WebView webView : webViews){
????if(webView != null){
????inst.runOnMainSync(new Runnable() {
????public void run() {
????webView.getSettings().setJavaScriptEnabled(true);
????webView.setWebChromeClient(robotiumWebClient);
????}
????});
????}
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11