Ejemplo n.º 1
0
int wdFindElementsByTagName(WebDriver* driver, WebElement* element, const wchar_t* name, ElementCollection** result)
{
	*result = NULL;
	if (!driver || !driver->ie) { return ENOSUCHDRIVER; }

	try {
		InternetExplorerDriver* ie = driver->ie;
		CComPtr<IHTMLElement> elem;
		if (element && element->element) {
			elem = element->element->getWrappedElement();
		}

		ElementCollection* collection = new ElementCollection();
		*result = collection;
		clock_t end = endAt(driver);

		do {
			collection->elements = driver->ie->selectElementsByTagName(elem, name);

			if (collection->elements->size() > 0) {
				return SUCCESS;
			}
		} while (clock() < end);

		return SUCCESS;
	} END_TRY;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
// pattern2 : bling each led in random frequency and random duration
void pattern2() {
     gMSec = 99;
     float dOffsetRed = offsetForRotation();
     volatile long  dRedEndAt = 0;     
     float radianRed  = 0.0;
          
     float dOffsetGreen = offsetForRotation();
     long  dGreenEndAt  = 0;    
     float radianGreen  = 0.0;
      
     float dOffsetBlue = offsetForRotation();
     long  dBlueEndAt  = 0;     
     float radianBlue  = 0.0;
     
     float dMultiplyRed   = 0.0;
     float dMultiplyGreen = 0.0;
     float dMultiplyBlue  = 0.0;
        
     while (gMSec < 30000) {    // 1 min
        
        if (gMSec > dRedEndAt) {
            dMultiplyRed = multiplyForRotation();
            dRedEndAt = endAt();
        }
        if (gMSec > dGreenEndAt) {
            dMultiplyGreen = multiplyForRotation();
            dGreenEndAt  = endAt();
        }
        if (gMSec > dBlueEndAt) {
            dMultiplyBlue = multiplyForRotation();
            dBlueEndAt  = endAt();
        }
        putRed(   calcSin(dOffsetRed,   dMultiplyRed,   radianRed)  );
        putGreen( calcSin(dOffsetGreen, dMultiplyGreen, radianGreen));
        putBlue(  calcSin(dOffsetBlue,  dMultiplyBlue,  radianBlue) );

        radianRed   += 0.03;
        radianGreen += 0.03;
        radianBlue  += 0.03;
        
        wait_ms(10);       // pwm needs delay
    }
}
Ejemplo n.º 4
0
int wdFindElementsByXPath(WebDriver* driver, WebElement* element, const wchar_t* xpath, ElementCollection** out)
{
	*out = NULL;
	if (!driver || !driver->ie) { return ENOSUCHDRIVER; }

	try {
		clock_t end = endAt(driver);
		int result = EUNHANDLEDERROR;

		do {

			result = injectXPathEngine(driver);
			if (result != SUCCESS) {
				continue;
			}

			// Call it
			std::wstring query;
			if (element)
				query += L"(function() { return function() {var res = document.__webdriver_evaluate(arguments[0], arguments[1], null, 7, null); return res;};})();";
			else
				query += L"(function() { return function() {var res = document.__webdriver_evaluate(arguments[0], document, null, 7, null); return res;};})();";

			// We need to use the raw functions because we don't allow random objects
			// to be returned from the executeScript method normally
			SAFEARRAYBOUND bounds;
			bounds.cElements = 2;
			bounds.lLbound = 0;
			SAFEARRAY* queryArgs = SafeArrayCreate(VT_VARIANT, 1, &bounds);

			CComVariant queryArg(xpath);
			LONG index = 0;
			SafeArrayPutElement(queryArgs, &index, &queryArg);
		
			if (element) {
				CComVariant elementArg(element->element->getWrappedElement());
				LONG index = 1;
				SafeArrayPutElement(queryArgs, &index, &elementArg);
			}

			CComVariant snapshot;
			result = driver->ie->executeScript(query.c_str(), queryArgs, &snapshot);
			SafeArrayDestroy(queryArgs);
			if (result != SUCCESS) {
				continue;
			}

			bounds.cElements = 1;
			SAFEARRAY* lengthArgs = SafeArrayCreate(VT_VARIANT, 1, &bounds);
			index = 0;
			SafeArrayPutElement(lengthArgs, &index, &snapshot);
			CComVariant lengthVar;
			result = driver->ie->executeScript(L"(function(){return function() {return arguments[0].snapshotLength;}})();", lengthArgs, &lengthVar);
			SafeArrayDestroy(lengthArgs);
			if (result != SUCCESS) {
				continue;
			}

			if (lengthVar.vt != VT_I4) {
				result = EUNEXPECTEDJSERROR;
				continue;
			}

			long length = lengthVar.lVal;

			bounds.cElements = 2;
			SAFEARRAY* snapshotArgs = SafeArrayCreate(VT_VARIANT, 1, &bounds);
			index = 0;
			SafeArrayPutElement(snapshotArgs, &index, &snapshot);
		
			ElementCollection* elements = new ElementCollection();
			elements->elements = new std::vector<ElementWrapper*>();

			index = 1;
			for (long i = 0; i < length; i++) {
				ScriptArgs* getElemArgs;
				wdNewScriptArgs(&getElemArgs, 2);
				// Cheat
				index = 0;
				SafeArrayPutElement(getElemArgs->args, &index, &snapshot);
				getElemArgs->currentIndex++;
				wdAddNumberScriptArg(getElemArgs, i);

				ScriptResult* getElemRes;
				wdExecuteScript(driver, L"(function(){return function() {return arguments[0].iterateNext();}})();", getElemArgs, &getElemRes);
				WebElement* e;
				wdGetElementScriptResult(getElemRes, driver, &e);
				elements->elements->push_back(e->element);
				wdFreeScriptArgs(getElemArgs);
			}
			SafeArrayDestroy(queryArgs);

			*out = elements;
			return SUCCESS;
		} while (clock() < end);

		return result;
	} END_TRY;
}
Ejemplo n.º 5
0
int wdFindElementByXPath(WebDriver* driver, WebElement* element, const wchar_t* xpath, WebElement** out)
{
	*out = NULL;
	if (!driver || !driver->ie) { return ENOSUCHDRIVER; }

	try {
		clock_t end = endAt(driver);
		int result = ENOSUCHELEMENT;

		do {
			result = injectXPathEngine(driver);
			// TODO(simon): Why does the injecting sometimes fail?
			/*
			if (result != SUCCESS) {
				return result;
			}
			*/

			// Call it
			std::wstring query;
			if (element) {
				query += L"(function() { return function(){var res = document.__webdriver_evaluate(arguments[0], arguments[1], null, 7, null); return res.snapshotItem(0) ;};})();";
			} else {
				query += L"(function() { return function(){var res = document.__webdriver_evaluate(arguments[0], document, null, 7, null); return res.snapshotLength != 0 ? res.snapshotItem(0) : undefined ;};})();";
			}

			ScriptArgs* queryArgs;
			result = wdNewScriptArgs(&queryArgs, 2);
			if (result != SUCCESS) {
				wdFreeScriptArgs(queryArgs);
				continue;
			}
			result = wdAddStringScriptArg(queryArgs, xpath);
			if (result != SUCCESS) {
				wdFreeScriptArgs(queryArgs);
				continue;
			}
			if (element) {
				result = wdAddElementScriptArg(queryArgs, element);
			}
			if (result != SUCCESS) {
				wdFreeScriptArgs(queryArgs);
				continue;
			}

			ScriptResult* queryResult;
			result = wdExecuteScript(driver, query.c_str(), queryArgs, &queryResult);
			wdFreeScriptArgs(queryArgs);

			// And be done
			if (result == SUCCESS) {
				int type = 0;
				result = wdGetScriptResultType(driver, queryResult, &type);
				if (type != TYPE_EMPTY) {
					result = wdGetElementScriptResult(queryResult, driver, out);
				} else {
					result = ENOSUCHELEMENT;
					wdFreeScriptResult(queryResult);
					continue;
				}
			}
			wdFreeScriptResult(queryResult);

			return result;
		} while (clock() < end);

		return result;
	} END_TRY;
}
Ejemplo n.º 6
0
int wdFindElementsByCss(WebDriver* driver, WebElement* element, const wchar_t* selector, ElementCollection** out)
{
	*out = NULL;
	if (!driver || !driver->ie) { return ENOSUCHDRIVER; }

	try {
		clock_t end = endAt(driver);
		int result = EUNHANDLEDERROR;

		do {
			// Call it
			std::wstring script(L"(function() { return function(){");
			for (int i = 0; SIZZLE[i]; i++) {
				script += SIZZLE[i];
				script += L"\n";
			}
			script += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
			script += L"if (root['querySelectorAll']) { return root.querySelectorAll(arguments[0]); } ";
			script += L"var results = []; Sizzle(arguments[0], root, results);";
			script += L"return results;";
			script += L"};})();";

			// Call it
			ScriptArgs* args;
			result = wdNewScriptArgs(&args, 2);
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}
			result = wdAddStringScriptArg(args, selector);
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}

			result = wdAddElementScriptArg(args, element);
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}

			ScriptResult* queryResult;
			result = wdExecuteScript(driver, script.c_str(), args, &queryResult);
			wdFreeScriptArgs(args);

			// And be done
			if (result != SUCCESS) {
				wdFreeScriptResult(queryResult);
				return result;
			}

			ElementCollection* elements = new ElementCollection();
			elements->elements = new std::vector<ElementWrapper*>();

			int length;
			result = wdGetArrayLengthScriptResult(driver, queryResult, &length);
			if (result != SUCCESS) {
				wdFreeScriptResult(queryResult);
				return result;
			}

			for (long i = 0; i < length; i++) {
				ScriptResult* getElemRes;
				wdGetArrayItemFromScriptResult(driver, queryResult, i, &getElemRes);

				WebElement* e;
				wdGetElementScriptResult(getElemRes, driver, &e);
				elements->elements->push_back(e->element);
				e->element = NULL;
				delete e;
			}
			wdFreeScriptResult(queryResult);

			*out = elements;
			return SUCCESS;
		} while (clock() < end);

		return result;
	} END_TRY;
}
Ejemplo n.º 7
0
int wdFindElementByCss(WebDriver* driver, WebElement* element, const wchar_t* selector, WebElement** out)
{
	*out = NULL;
	if (!driver || !driver->ie) { return ENOSUCHDRIVER; }

	try {
		clock_t end = endAt(driver);
		int result = ENOSUCHELEMENT;

		do {
			std::wstring script(L"(function() { return function(){");
			for (int i = 0; SIZZLE[i]; i++) {
				script += SIZZLE[i];
				script += L"\n";
			}
			script += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
			script += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } ";
			script += L"var results = []; Sizzle(arguments[0], root, results);";
			script += L"return results.length > 0 ? results[0] : null;";
			script += L"};})();";

			// Call it
			ScriptArgs* args;
			result = wdNewScriptArgs(&args, 2);
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}
			result = wdAddStringScriptArg(args, selector);
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}
			if (element) {
				result = wdAddElementScriptArg(args, element);
			}
			if (result != SUCCESS) {
				wdFreeScriptArgs(args);
				continue;
			}

			ScriptResult* queryResult;
			result = wdExecuteScript(driver, script.c_str(), args, &queryResult);
			wdFreeScriptArgs(args);

			// And be done
			if (result == SUCCESS) {
				int type = 0;
				result = wdGetScriptResultType(driver, queryResult, &type);
				if (type != TYPE_EMPTY) {
					result = wdGetElementScriptResult(queryResult, driver, out);
				} else {
					result = ENOSUCHELEMENT;
					wdFreeScriptResult(queryResult);
					continue;
				}
			}
			wdFreeScriptResult(queryResult);

			return result;
		} while (clock() < end);

		return result;
	} END_TRY;
}