1 /* thd_selenium.c
2    use Google's selenium webdriver library from Python to open and drive webpages
3    Allows opening webpages in the same tab of a browser as opposed to calls
4    to open browser applications through system calls which generate new tabs or windows
5    for each webpage request
6 */
7 
8 /* to compile, add -lpython2.7  to program compile option with $(LPYTHON) below and include
9    IPYTHON   = -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
10      -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
11    LPYTHON   = -lpython2.7
12    IFLAGS = ... -I$(IPYTHON)
13  */
14 
15 #include "mrilib.h"
16 
17 #ifdef SELENIUM_READY
18 #include <Python.h>
19 
20 // extern char *THD_abindir(char withslash);
21 // extern char * GetAfniWebBrowser(void);
22 
23 /* variable to monitor level of startup
24   -1 not started
25   0 closed
26   1 selenium started
27   2 browser started
28   3 webpage has been opened
29 */
30 static int selenium_started = -1;
31 
32 /* initialize selenium */
selenium_init()33 int selenium_init()
34 {
35    char *temppath=NULL;
36 
37    /* look for python scripts in same place as afni binary */
38 //   Py_SetProgramName(argv[0]); /* could set to afni/whereami/suma/... here */
39    Py_Initialize();
40    PyObject *sys = PyImport_ImportModule("sys");
41    PyObject *path = PyObject_GetAttrString(sys, "path");
42 
43    PyRun_SimpleString("print 'Initialized Python from AFNI'");
44 
45    PyRun_SimpleString("import sys, os");
46 
47    temppath =  THD_abindir(0);
48 // printf("afni path is %s\n", temppath);
49 
50    PyList_Append(path, PyString_FromString(temppath));
51 //   PyRun_SimpleString("sys.path.extend(['/Users/dglen/abin'])");
52 
53    PyRun_SimpleString("import afni_util as U");
54    PyRun_SimpleString("from selenium import webdriver");
55    PyRun_SimpleString("from selenium.webdriver.common.keys import Keys");
56    selenium_started = 1;
57   return(0);
58 }
59 
60 /* open selenium webdriver browser, not necessarily to a specific webpage */
selenium_open_browser()61 int selenium_open_browser()
62 {
63    static char *webb=NULL ; static int first=1 ;
64 
65    if( first == 1 ){ webb = GetAfniWebBrowser() ; first = 2 ; }
66 
67    if(selenium_started<=0)
68       selenium_init();
69 
70 
71    if((webb==NULL)||(strcasestr(webb,"Chrome"))||(strcasecmp(webb, "open")==0)){
72       if(selenium_started<2) {
73          printf("For Chrome, must install chromedriver binary available from.\n");
74          printf("https://sites.google.com/a/chromium.org/chromedriver\n");
75          printf("Put the chromedriver binary in your path or update path to include\n");
76          printf("the chromedriver binary\n");
77       }
78       PyRun_SimpleString("browser = webdriver.Chrome('chromedriver')");
79    }
80    else if (strcasestr(webb, "Firefox"))
81       PyRun_SimpleString("browser = webdriver.Firefox()");
82    else if (strcasestr(webb, "InternetExplorer"))
83       PyRun_SimpleString("browser = webdriver.Ie()");
84    else if (strcasestr(webb, "Opera")) /* opera's installation is broken now, and requires OperaAppiumDriver */
85       PyRun_SimpleString("browser = webdriver.Opera()");
86    else if (strcasestr(webb, "Safari")) {
87       if(selenium_started<2) {
88          printf("For Safari, must set environment variable first to location of jar file:\n");
89          printf("setenv SELENIUM_SERVER_JAR /Users/myusername/selenium_dev/selenium-server-standalone-2.44.0.jar\n");
90          printf("You may also need to install Selenium extension for Safari.\n");
91          printf("See https://github.com/SeleniumHQ/selenium/wiki/SafariDriver for more information\n");
92       }
93 
94       PyRun_SimpleString("browser = webdriver.Safari()");
95    }
96    else {
97       PyRun_SimpleString("browser = webdriver.Chrome('chromedriver')");
98    }
99    selenium_started = 2;
100    return(0);
101 }
102 
103 /* open a webpage in the current browser instance */
selenium_open_webpage(char * webpage)104 int selenium_open_webpage(char *webpage)
105 {
106    char webstring[1024];
107 
108 printf("selenium_started level = %d, webpage requested is\n   %s\n", selenium_started, webpage);
109    /* legit webpage */
110    if (!webpage || (strlen(webpage)==0))
111       return(-1);
112    /* first time through, start up python, selenium, browser instances */
113    if(selenium_started<=2)
114       selenium_open_browser();
115    sprintf(webstring, "browser.get('%s')", webpage);
116    PyRun_SimpleString(webstring);
117    selenium_started = 3;
118    return(0);
119 }
120 
121 /* close browser, selenium and python */
selenium_close()122 int selenium_close()
123 {
124    if(selenium_started<=0) return(0);
125    PyRun_SimpleString("browser.quit()");
126    Py_Finalize();
127    selenium_started = 0;
128    return(0);
129 }
130 
131 
132 /* use selenium browser instead of stand-alone browser */
afni_uses_selenium()133 int afni_uses_selenium()
134 {
135    return(AFNI_yesenv("AFNI_SELENIUM")) ;
136 }
137 
138 #else
139 
afni_uses_selenium()140 int afni_uses_selenium() { return(0) ; }
141 
selenium_open_webpage(char * webpage)142 int selenium_open_webpage(char *webpage) { return(0); }
selenium_close()143 int selenium_close() { return(0); }
144 
145 #endif
146