问题:
在用robot写手机app的自动化时,打开商城商品详情页H5页面立即购买确认支付用例;
需要切换到对应的webview,切换到webview后,点击商品详情页-立即购买,跳转到支付页面,支付确认页面也是H5,
与商品详情页页面的webview一样,此时点击立即购买页面的元素,提示找不到元素,用get source获取商品详情页面源码,
获取到的源码是第一个商品详情页的立即购买页面的源码,所以在商品详情页面执行操作找不到元素。
解决:
在商品详情页面点击立即购买后,先切换回NATIVE_APP,再重新切换回对应的webview(WEBVIEW_aye_com.aye_aye_paste_android),此后就能执行商品详情页面的元素了
步骤:
前提是:debug包,让安卓开发人员在H5页面开启webview的权限:
1.设置webview.setWebContentsDebuggingEnabled(true);
不一定要更改原来的webview继承的父类,先看现在已有的继承父类有没有这个属性,如果有,就先开启就行了;
2.没有webviewsetWebContentsDebuggingEnabled此属性:
就继承:
public class MyActivity extends CordovaActivity {
CordovaWebView cwv;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.appView.setWebContentsDebuggingEnabled(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
robotframework代码:
@{contexts} Get Contexts #获取原生app跟webview的名称
Log Many @{contexts} #打印出原生app跟webview名称
${cur_context} Get Current Context #得到当前app内容
log ${cur_context} #打印当前app内容名称
Switch To Context WEBVIEW_aye_com.aye_aye_paste_android #切换到webview
${cur_context} Get Current Context #得到当前app内容
log ${cur_context} #打印当前app内容名称
${page} Log Source #获取当前webview的源码
log ${page} #打印当前webview的源码
Page Should Contain Text 立即购买
Click Element xpath=//div[contains(@class,'addPurchase')] #立即购买
Wait Until Page Contains 确定
Click Element xpath=//div[contains(@class,'addPurchase')] #确定
Wait Until Page Contains 结算
Click Element xpath=//a[@class="am-button am-button-warning"] #结算
Wait Until Page Contains
Click Elementxpath=//a[@class="am-button am-button-primary"]
原理是:原生APP页面跳转到H5页面(webview),然后再定位xpath;