Last active
May 29, 2022 07:16
-
-
Save opsJson/6d1318cba3038ededa7e0d9489f5f7a3 to your computer and use it in GitHub Desktop.
Simple WebDriver
This file contains hidden or 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
| #include <stdio.h> | |
| #include "./easy-curl.h" //https://gist.github.com/opsJson/6200d1fe4666b6bfa6919677a05428b0 | |
| char _WEBDRIVER_SESSION[33]; | |
| typedef enum WEBDRIVER_CODE { | |
| WEBDRIVER_SUCCESS, | |
| WEBDRIVER_ERROR_JAVASCRIPT, | |
| WEBDRIVER_ERROR_NO_SESSION | |
| } WEBDRIVER_CODE; | |
| void webdriver_init() { | |
| //open webdriver | |
| ShellExecuteA(NULL, "open", "msedgedriver.exe", NULL, NULL, 0); | |
| //open webdriver session | |
| long int code; | |
| char *response = GO("POST", "localhost:9515/session", "{\"capabilities\":{}}", &code, ""); | |
| if (code != 200) | |
| { | |
| fprintf(stderr, "ERROR: Could not create webdriver session.\n"); | |
| ShellExecuteA(NULL, "open", "taskkill", "/f /im \"msedgedriver.exe\"", NULL, 0); | |
| exit(1); | |
| } | |
| //get session | |
| char *token = "sessionId\":\""; | |
| char *sessionID = strstr(response, token); | |
| sessionID += strlen(token); | |
| char *aux = strstr(sessionID, "\""); | |
| if (aux == NULL) | |
| { | |
| fprintf(stderr, "ERROR: Could not create webdriver session.\n"); | |
| ShellExecuteA(NULL, "open", "taskkill", "/f /im \"msedgedriver.exe\"", NULL, 0); | |
| exit(1); | |
| } | |
| *aux = 0; | |
| snprintf(_WEBDRIVER_SESSION, sizeof(_WEBDRIVER_SESSION)-1, "%s", sessionID); | |
| free(response); | |
| } | |
| void webdriver_cleanup() { | |
| char url[128]; | |
| //close webdriver session | |
| snprintf(url, sizeof(url)-1, "localhost:9515/session/%s/window", _WEBDRIVER_SESSION); | |
| GO("DELETE", url, "", 0, ""); | |
| //close webdriver | |
| ShellExecuteA(NULL, "open", "taskkill", "/f /im \"msedgedriver.exe\"", NULL, 0); | |
| } | |
| WEBDRIVER_CODE webdriver_execute(char *script) { | |
| long int code; | |
| char data[1024]; | |
| char url[128]; | |
| char *r; | |
| snprintf(url, sizeof(url)-1, "localhost:9515/session/%s/execute/sync", _WEBDRIVER_SESSION); | |
| snprintf(data, sizeof(data)-1, "{\"script\":\"%s\",\"args\":[]}", script); | |
| r = GO("POST", url, data, &code, ""); | |
| free(r); | |
| if (code == 500) return WEBDRIVER_ERROR_JAVASCRIPT; | |
| if (code == 404) return WEBDRIVER_ERROR_NO_SESSION; | |
| return WEBDRIVER_SUCCESS; | |
| } | |
| /*/////////////////////////////////// | |
| Testing: | |
| ///////////////////////////////////*/ | |
| int main(void) { | |
| webdriver_init(); | |
| webdriver_execute("window.location.href = 'https://google.com'"); | |
| webdriver_cleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment