Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run scripts #34

Closed
SantoshSrinivas79 opened this issue Jan 20, 2015 · 4 comments
Closed

How to run scripts #34

SantoshSrinivas79 opened this issue Jan 20, 2015 · 4 comments

Comments

@SantoshSrinivas79
Copy link

Hi, I just came across this project and unable to figure out how to use this project.

I want to run the sample script mentioned, https://gist.github.com/DinisCruz/516417b0e70a2ba5e8bb#file-91-multiple-google-searches-js

I installed nwr using sudo npm install nwr -g.

How do I run the script? node 91-multiple-google-searches-js does not work.

Sorry for the basic question!

@DinisCruz
Copy link
Contributor

No worries, that is not at all a basic question (since I kinda droped a bit crome-driver support for this version (and have not update the docs))

Here is how you get it to work

git clone git@github.com:o2platform/nwr.git
cd nwr
npm install

image

...

image

The command to run the version with selenium/chrome-driver support is:

coffee src/selenium/app.coffee

if on first run you get
image
...
image

is because you need to do this (from the README)

as per documented at https://github.com/rogerwang/node-webkit/wiki/Chromedriver you need to
download the chrome driver for your platform from http://dl.node-webkit.org/v0.11.2/ and copy it into
the ./node_modules/nodewebkit/nodewebkit folder.

In OSx the file to download is the chromedriver-nw-v0.11.2-osx-x64 and you can open the folder to
copy the unziped file using open ./node_modules/nodewebkit/nodewebkit

for example with:

cd node_modules/nodewebkit/nodewebkit/
curl -O http://dl.nwjs.io/v0.11.2/chromedriver-nw-v0.11.2-osx-x64.zip
unzip chromedriver-nw-v0.11.2-osx-x64.zip
mv chromedriver-nw-v0.11.2-osx-x64/chromedriver .
ls

image

now it should be a case of

cd ../../..
coffee src/selenium/app.coffee

which should look like this:

image

If all went well you should see something like this (with the console log messages confirming that the WebDriver was driving the requests)

image

The script that is shown by default already the main thing that you get from that gist you linked

text = "o2platform node-webkit-repl"
browser.eval "window.location.href='http://www.google.com'", ()->
    browser.elementByName 'q', (err, element)->
        element.clear ->
            element.sendKeys text, nop
            browser.elementByName 'btnG', (err, button) ->
                button.click(text)

value = 'The answer is ' + 42;
return value;

For reference here is the updated version of that script (in coffee script)

# same as example 91 but written in coffee script
resetToParentFrame = (next)->
    browser._jsonWireCall
        method: 'POST' , 
        relPath: '/frame/parent' , 
        data: {} , 
        cb: next

select_Frame1 = (next)->
    browser.frame('iframe1', next)

open_Search_Google = (text, next)->
        resetToParentFrame ()->
            select_Frame1 ()->
                browser.eval "window.location='http://www.google.com'", ()->
                    browser.elementByName 'q', (err, element)->
                        element.clear () ->
                            element.sendKeys(text)
                            browser.elementByName 'btnG', (err, button)->
                                button.click(next)

open_Search_Google 'o2platform', ()->
    open_Search_Google 'owasp', ->


return "done"

Btw, the script above can sometimes lose the bottom frame (although it will still run), you can get it back by clicking on the back button (or refreshing the window)

also if you lose a script that you were writing take a look at #9

@DinisCruz
Copy link
Contributor

Btw, I actually found much more powerful (and faster) to execute browser automation commands directly via chrome-remote-debugger, which you can see in action if you run

npm run repl-gui

or

bin/node-webkit-repl gui

image

In that UI you could do something like

if (iframe1.contentWindow.location.href.indexOf 'google') is -1
    iframe1.src='https://www.google.com'
else
    document = iframe1.contentDocument
    document.getElementsByName('q')[0].value = 'Node WebKit REPL'
    document.getElementsByName('btnG')[0].click()

Here are the Node-Webkit window commands/api: https://github.com/o2platform/nwr/blob/master/src/api/NodeWebKit-Service.coffee

Here are the chrome commands/api: https://github.com/o2platform/nwr/blob/master/src/api/Remote-Chrome-API.coffee

The main use I get from this module everyday is to use it to run mocha tests which use this https://github.com/TeamMentor/TM_4_0_QA/blob/master/tests/API/QA-TM_4_0_Design.coffee to create the environment you can see at https://vimeo.com/116027788

btw there are tons of example in that repo on how to use this NWR module to drive chrome an insane speeds. See for example https://github.com/TeamMentor/TM_4_0_QA/blob/master/tests/API/Jade-API.coffee and https://github.com/TeamMentor/TM_4_0_QA/blob/master/tests/regression-sprint-1.coffee

@SantoshSrinivas79
Copy link
Author

@DinisCruz Thank you. Everything works like your described. I'll try to work with this now.
The project looks very interesting and useful. Thank you.

@DinisCruz
Copy link
Contributor

cool, let me know if you have more questions (don't be afraid to open more issues, even for small things/questions, since that is a great way to document how it works).

I'm using it every day, but have not had time to update this (I did start a refactoring which you can see https://github.com/o2platform/nwr/tree/nwr-refactoring which needs more work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants