int wdFindElementByXPath(WebDriver* driver, WebElement* element, const wchar_t* xpath, WebElement** result) { if (!driver || !driver->ie) { return ENOSUCHDRIVER; } CComPtr<IHTMLDOMNode> res; InternetExplorerDriver* ie = driver->ie; CComPtr<IHTMLElement> elem; if (element && element->element) { elem = element->element->getWrappedElement(); } try { ElementWrapper* wrapper; int res = ie->selectElementByXPath(elem, xpath, &wrapper); if (res != SUCCESS) { return res; } WebElement* toReturn = new WebElement(); toReturn->element = wrapper; *result = toReturn; return SUCCESS; } END_TRY; }
int wdFindElementByXPath(WebDriver* driver, WebElement* element, const wchar_t* xpath, WebElement** result) { CComPtr<IHTMLDOMNode> res; InternetExplorerDriver* ie = driver->ie; CComPtr<IHTMLElement> elem; if (element && element->element) { elem = element->element->getWrappedElement(); } ElementWrapper* wrapper = NULL; try { wrapper = ie->selectElementByXPath(elem, xpath); } catch (std::wstring& ) { return -1; } if (!wrapper) { return -2; } WebElement* toReturn = new WebElement(); toReturn->element = wrapper; *result = toReturn; return 0; }
int wdFindElementById(WebDriver* driver, WebElement* element, const wchar_t* id, WebElement** result) { *result = NULL; if (!driver || !driver->ie) { return ENOSUCHDRIVER; } InternetExplorerDriver* ie = driver->ie; CComPtr<IHTMLElement> elem; if (element && element->element) { elem = element->element->getWrappedElement(); } try { clock_t end = endAt(driver); int res = ENOSUCHELEMENT; do { ElementWrapper* wrapper; res = ie->selectElementById(elem, id, &wrapper); if (res != SUCCESS) { continue; } WebElement* toReturn = new WebElement(); toReturn->element = wrapper; *result = toReturn; return SUCCESS; } while (clock() < end); return res; } END_TRY; }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_get (JNIEnv *env, jobject obj, jstring url) { InternetExplorerDriver* ie = getIe(env, obj); const wchar_t* converted = (wchar_t *)env->GetStringChars(url, 0); ie->get(converted); env->ReleaseStringChars(url, (jchar*) converted); }
JNIEXPORT jobject JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_close (JNIEnv *env, jobject obj) { InternetExplorerDriver* wrapper = getIe(env, obj); wrapper->close(); return NULL; }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_setFrameIndex (JNIEnv *env, jobject obj, jstring pathToFrame) { const wchar_t* path = (wchar_t *)env->GetStringChars(pathToFrame, 0); InternetExplorerDriver* ie = getIe(env, obj); if (!ie->switchToFrame(path)) { std::wstring msg(L"Cannot locate frame using path: "); msg += path; throwNoSuchFrameException(env, msg.c_str()); } env->ReleaseStringChars(pathToFrame, (jchar*) path); }
JNIEXPORT jobject JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_doSwitchToActiveElement (JNIEnv *env, jobject obj) { InternetExplorerDriver* ie = getIe(env, obj); ElementWrapper* element = ie->getActiveElement(); if (!element) return NULL; jclass clazz = env->FindClass("org/openqa/selenium/ie/InternetExplorerElement"); jmethodID cId = env->GetMethodID(clazz, "<init>", "(J)V"); return env->NewObject(clazz, cId, (jlong) element); }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_doSetMouseSpeed (JNIEnv *env, jobject obj, jint speed) { InternetExplorerDriver* ie = getIe(env, obj); ie->setSpeed((int) speed); }
JNIEXPORT jstring JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_doGetCookies (JNIEnv *env, jobject obj) { InternetExplorerDriver* ie = getIe(env, obj); return wstring2jstring(env, ie->getCookies()); }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_goForward (JNIEnv *env, jobject obj) { InternetExplorerDriver* ie = getIe(env, obj); ie->goForward(); }
JNIEXPORT jobject JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_doExecuteScript (JNIEnv *env, jobject obj, jstring script, jobjectArray args) { InternetExplorerDriver* wrapper = getIe(env, obj); // Convert the args into something we can use elsewhere. jclass numberClazz = env->FindClass("java/lang/Number"); jclass booleanClazz = env->FindClass("java/lang/Boolean"); jclass stringClazz = env->FindClass("java/lang/String"); jclass elementClazz = env->FindClass("org/openqa/selenium/ie/InternetExplorerElement"); jmethodID longValue = env->GetMethodID(numberClazz, "longValue", "()J"); jmethodID booleanValue = env->GetMethodID(booleanClazz, "booleanValue", "()Z"); jfieldID elementPointer = env->GetFieldID(elementClazz, "nodePointer", "J"); jsize length = env->GetArrayLength(args); SAFEARRAYBOUND bounds; bounds.cElements = length; bounds.lLbound = 0; SAFEARRAY* convertedItems = SafeArrayCreate(VT_VARIANT, 1, &bounds); LONG index[1]; for (jsize i = 0; i < length; i++) { index[0] = i; VARIANT dest; VariantInit(&dest); jobject arrayObject = env->GetObjectArrayElement(args, i); jclass objClazz = env->GetObjectClass(arrayObject); if (env->IsInstanceOf(arrayObject, numberClazz)) { jlong value = env->CallLongMethod(arrayObject, longValue); dest.vt = VT_I4; dest.lVal = (LONG) value; } else if (env->IsInstanceOf(arrayObject, stringClazz)) { wchar_t *converted = (wchar_t *)env->GetStringChars((jstring) arrayObject, 0); std::wstring value(converted); env->ReleaseStringChars((jstring) arrayObject, (jchar*) converted); dest.vt = VT_BSTR; dest.bstrVal = SysAllocString(value.c_str()); } else if (env->IsInstanceOf(arrayObject, booleanClazz)) { bool value = env->CallBooleanMethod(arrayObject, booleanValue) == JNI_TRUE; dest.vt = VT_BOOL; dest.boolVal = value; } else if (env->IsInstanceOf(arrayObject, elementClazz)) { ElementWrapper* element = (ElementWrapper*) env->GetLongField(arrayObject, elementPointer); dest.vt = VT_DISPATCH; dest.pdispVal = element->getWrappedElement(); } SafeArrayPutElement(convertedItems, &i, &dest); } const wchar_t* converted = (wchar_t *)env->GetStringChars(script, 0); VARIANT result; VariantInit(&result); wrapper->executeScript(converted, convertedItems, &result); env->ReleaseStringChars(script, (jchar*) converted); // TODO (simon): Does this clear everything properly? SafeArrayDestroy(convertedItems); if (result.vt == VT_BSTR) { return wstring2jstring(env, bstr2wstring(result.bstrVal)); } else if (result.vt == VT_DISPATCH) { // Attempt to create a new webelement CComQIPtr<IHTMLDOMNode> node(result.pdispVal); if (!node) { cerr << "Cannot convert response to node. Attempting to convert to string" << endl; return wstring2jstring(env, variant2wchar(result)); } ElementWrapper* element = new ElementWrapper(wrapper, node); jclass clazz = env->FindClass("org/openqa/selenium/ie/InternetExplorerElement"); jmethodID cId = env->GetMethodID(clazz, "<init>", "(J)V"); return env->NewObject(clazz, cId, (jlong) element); } else if (result.vt == VT_BOOL) { jclass clazz = env->FindClass("java/lang/Boolean"); jmethodID cId = env->GetMethodID(clazz, "<init>", "(Z)V"); return env->NewObject(clazz, cId, (jboolean) (result.boolVal == VARIANT_TRUE)); } else if (result.vt == VT_I4) { jclass clazz = env->FindClass("java/lang/Long"); jmethodID cId = env->GetMethodID(clazz, "<init>", "(J)V"); return env->NewObject(clazz, cId, (jlong) result.lVal); } else if (result.vt == VT_I8) { jclass clazz = env->FindClass("java/lang/Long"); jmethodID cId = env->GetMethodID(clazz, "<init>", "(J)V"); return env->NewObject(clazz, cId, (jlong) result.dblVal); } else if (result.vt == VT_USERDEFINED) { jclass newExcCls; env->ExceptionDescribe(); env->ExceptionClear(); newExcCls = env->FindClass("java/lang/RuntimeException"); jmethodID cId = env->GetMethodID(newExcCls, "<init>", "(Ljava/lang/String;)V"); jstring message = wstring2jstring(env, bstr2wstring(result.bstrVal)); jobject exception; if (message) { exception = env->NewObject(newExcCls, cId, message); } else { cout << "Falling back" << endl; exception = env->NewObject(newExcCls, cId, (jstring) "Cannot extract cause of error"); } env->Throw((jthrowable) exception); return NULL; } cerr << "Unknown variant type. Will attempt to coerce to string: " << result.vt << endl; return wstring2jstring(env, variant2wchar(result)); }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_waitForLoadToComplete (JNIEnv *env, jobject obj) { InternetExplorerDriver* ie = getIe(env, obj); ie->waitForNavigateToFinish(); }
JNIEXPORT void JNICALL Java_org_openqa_selenium_ie_InternetExplorerDriver_setVisible (JNIEnv *env, jobject obj, jboolean isVisible) { InternetExplorerDriver* ie = getIe(env, obj); ie->setVisible(isVisible == JNI_TRUE); }