From rlpowell at digitalkingdom.org Tue Jul 1 05:21:22 2008 From: rlpowell at digitalkingdom.org (Robin Lee Powell) Date: Mon, 30 Jun 2008 22:21:22 -0700 Subject: [cl-selenium-devel] Problems with cl-selenium, Drakma, and nasty JavaScript Message-ID: <20080701052122.GA30134@digitalkingdom.org> I just moved over from Scheme, and am trying to get my project, which is TDD-via-Selenium based, ported over. Mostly going well; thank you for saving me the trouble of creating a Selenium port. Again. The problem I'm having is with a function I wrote to retrieve CSS properties of page elements; I like to be able to check that things are being rendered properly. As far as I know, there's nothing for this in the Selenium API. Here's the function. It's horrible; I apologize. - ------------------ ; Takes an element locator and a CSS property name, and returns the ; value for that property on that locator. ; ; Example: ; ; (expect-string= "And: the ephemeral bit is bolded." ; "bold" ; (element-attribute hpv "Num Ratings Text Ephemera" "font-weight")) ; ; The Javascript here is a bit scary. (defun element-css-raw (elem prop) (let* ( ; Some browsers want font-family; some want fontFamily. ; ; *sigh* (prop-lis (cl-ppcre:split "[-]" prop)) (camel-prop (apply #'concatenate 'string (cons (car prop-lis) (mapcar #'(lambda (x) (string-capitalize x)) (cdr prop-lis))))) ; The actual javascript (js (format nil (concatenate 'string "element = this.page().findElement(\"~A\"); " "if( element.currentStyle ) " "{ element.currentStyle.~A } " "else { this.browserbot.getCurrentWindow()." "getComputedStyle( element, null )." "getPropertyValue('~A') }") elem camel-prop prop)) ; Running the javascript (js-out (do-get-eval js)) ; Some browers think colours look like rgb( rrr, ggg, bbb ) ; (where all are decimal numbers), but we prefer #rrggbb, ; so we make it that way (color-normalized-js-out (cond ((cl-ppcre:scan "^\\s*rgb\\(" js-out) (apply format nil "#~2,'0X~2,'0X~2,'0X" (map (lambda (num-str) (parse-integer num-str)) (cl-ppcre:split ", *" (cl-ppcre:regex-replace "\\)$" (cl-ppcre:regex-replace "^\\s*rgb\\(" js-out "") ""))))) (t js-out))) ) ;(format #t "element-css: ~A, ~A, ~A, ~A, ~A\n" elem prop camel-prop js color-normalized-js-out) color-normalized-js-out)) - ------------------ The problem is with "element = this.page().findElement(\"~A\"); " Specifically, the embedded ". Drakma simply won't accept them: Parse error:URI "http://localhost:4444/selenium-server/driver/?cmd=getEval&1=element%20%3D%20this.page().findElement(\"id%3Dhome\")%3B%20if(%20element.currentStyle%20)%20%7B%20element.currentStyle.textDecoration%20%7D%20else%20%7B%20this.browserbot.getCurrentWindow().getComputedStyle(%20element,%20null%20).getPropertyValue('text-decoration')%20%7D&sessionId=821656" contains illegal character #\" at position 100. The obvious solution of replacing them with ' doesn't work, because some of my xpaths have ' in them, so that fails entirely. I can't just replace the ' in the xpaths with " to make up for *that*, because if I do, the " still end up in the Drakma request, and again Drakma refuses. I haven't started hacking source for cl-selenium for Drakma yet; any help would be appreciated. -Robin -- Lojban Reason #17: http://en.wikipedia.org/wiki/Buffalo_buffalo Proud Supporter of the Singularity Institute - http://singinst.org/ http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/ From mkennedy at common-lisp.net Tue Jul 1 07:04:26 2008 From: mkennedy at common-lisp.net (Matthew Kennedy) Date: Tue, 1 Jul 2008 02:04:26 -0500 Subject: [cl-selenium-devel] Problems with cl-selenium, Drakma, and nasty JavaScript In-Reply-To: <20080701052122.GA30134@digitalkingdom.org> References: <20080701052122.GA30134@digitalkingdom.org> Message-ID: <9c1134b50807010004lc2395a7r976e9369e17f26f@mail.gmail.com> On Tue, Jul 1, 2008 at 12:21 AM, Robin Lee Powell wrote: > > The problem is with "element = this.page().findElement(\"~A\"); " > > Specifically, the embedded ". > > Drakma simply won't accept them: > > Parse error:URI "http://localhost:4444/selenium-server/driver/?cmd=getEval&1=element%20%3D%20this.page().findElement(\"id%3Dhome\")%3B%20if(%20element.currentStyle%20)%20%7B%20element.currentStyle.textDecoration%20%7D%20else%20%7B%20this.browserbot.getCurrentWindow().getComputedStyle(%20element,%20null%20).getPropertyValue('text-decoration')%20%7D&sessionId=821656" contains illegal character #\" at position 100. > > The obvious solution of replacing them with ' doesn't work, because > some of my xpaths have ' in them, so that fails entirely. I can't > just replace the ' in the xpaths with " to make up for *that*, > because if I do, the " still end up in the Drakma request, and again > Drakma refuses. Hi Robin, It looks like we were doing our own URL encoding in CL-Selenium so I've changed the code to let Drakma do it (which is the correct place to do it anyway). Now it should work correctly in every case. CL-USER> (let* ((selenium:*selenium-driver-url* "http://localhost:4444/selenium-server/driver") (selenium:*selenium-session* (selenium:do-get-new-browser-session "*opera" "http://www.google.com"))) (selenium:do-open "http://www.google.com/webhp?hl=en") (print (selenium:do-get-eval "\"Hello, \" + \"JavaScript!\"")) (selenium:do-test-complete)) "Hello, JavaScript!" NIL I've created a new 0.3 release which includes the fix. http://common-lisp.net/project/cl-selenium/releases/cl-selenium-0.3.tar.gz Thanks for noting this problem, Matt From rlpowell at digitalkingdom.org Tue Jul 1 16:14:25 2008 From: rlpowell at digitalkingdom.org (Robin Lee Powell) Date: Tue, 1 Jul 2008 09:14:25 -0700 Subject: [cl-selenium-devel] Problems with cl-selenium, Drakma, and nasty JavaScript In-Reply-To: <9c1134b50807010004lc2395a7r976e9369e17f26f@mail.gmail.com> References: <20080701052122.GA30134@digitalkingdom.org> <9c1134b50807010004lc2395a7r976e9369e17f26f@mail.gmail.com> Message-ID: <20080701161425.GD30134@digitalkingdom.org> On Tue, Jul 01, 2008 at 02:04:26AM -0500, Matthew Kennedy wrote: > It looks like we were doing our own URL encoding in CL-Selenium so > I've changed the code to let Drakma do it (which is the correct > place to do it anyway). Now it should work correctly in every > case. > > CL-USER> (let* ((selenium:*selenium-driver-url* > "http://localhost:4444/selenium-server/driver") > (selenium:*selenium-session* (selenium:do-get-new-browser-session > "*opera" "http://www.google.com"))) > (selenium:do-open "http://www.google.com/webhp?hl=en") > (print (selenium:do-get-eval "\"Hello, \" + \"JavaScript!\"")) > (selenium:do-test-complete)) > > "Hello, JavaScript!" > NIL > > I've created a new 0.3 release which includes the fix. > http://common-lisp.net/project/cl-selenium/releases/cl-selenium-0.3.tar.gz *Wow*! Props for the fast response! I can't test this until later today, but I just wanted to say thanks! -Robin From mkennedy at common-lisp.net Wed Jul 2 03:58:03 2008 From: mkennedy at common-lisp.net (Matthew Kennedy) Date: Tue, 1 Jul 2008 22:58:03 -0500 Subject: [cl-selenium-devel] New release 0.4 Message-ID: <9c1134b50807012058p6cee38c1g357e22268d1de840@mail.gmail.com> In 0.4, I updated the driver URL to be the RC default, so there's less thinking involved when getting started. Also added macro with-selenium-session which wraps up a session like with-open-file does with files. eg. (with-selenium-session (*selenium-session* "*opera" "http://www.google.com") (do-open "http://www.google.com/webhp?hl=en") (do-type "q" "hello world") (do-click "btnG") (do-wait-for-page-to-load "5000") (string= (do-get-title) "hello world - Google Search")) There's no API changes. Matt From rlpowell at digitalkingdom.org Wed Jul 2 05:14:03 2008 From: rlpowell at digitalkingdom.org (Robin Lee Powell) Date: Tue, 1 Jul 2008 22:14:03 -0700 Subject: [cl-selenium-devel] Problems with cl-selenium, Drakma, and nasty JavaScript In-Reply-To: <9c1134b50807010004lc2395a7r976e9369e17f26f@mail.gmail.com> References: <20080701052122.GA30134@digitalkingdom.org> <9c1134b50807010004lc2395a7r976e9369e17f26f@mail.gmail.com> Message-ID: <20080702051403.GF30134@digitalkingdom.org> On Tue, Jul 01, 2008 at 02:04:26AM -0500, Matthew Kennedy wrote: > I've created a new 0.3 release which includes the fix. > http://common-lisp.net/project/cl-selenium/releases/cl-selenium-0.3.tar.gz asdf-install is only grabbing me 0.3, for the record, but I can confirm that this fix works for me. Thanks! -Robin -- Lojban Reason #17: http://en.wikipedia.org/wiki/Buffalo_buffalo Proud Supporter of the Singularity Institute - http://singinst.org/ http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/