-1

I'm truing to execute a yui js script with js.executeScript Selenium's method. The script is being executed by selenium webdriver in order simulate a "click" on hybrid mobile app (the button is webview)

 String IncludeYUI = "script = document.createElement('script');script.type = 'text/javascript';script.async = true;script.onload = function(){};script.src = '"
                    + YUI_PATH
                    + "';document.getElementsByTagName('head')[0].appendChild(script);";
            js.executeScript(IncludeYUI);

where the YUI_PATH is an url - https://cdnjs.cloudflare.com/ajax/libs/yui/3.18.0/yui/.....

The problem is that I do not have an access to the global network from the current site.

so I was thinking to save the script under the project and just to load it from FS. But this is a js , no access to the FS ...

Any ideas how to load the script ?

Thanks

Igal
  • 4,603
  • 14
  • 41
  • 66
  • How are you loading the page to start? – epascarello Apr 20 '15 at 12:59
  • I dont have a page to start , just executing js code in order to click on some webElement – Igal Apr 20 '15 at 13:49
  • You could copy paste the whole textual content of [your library](http://cdnjs.cloudflare.com/ajax/libs/yui/3.18.0/yui/yui-min.js) with all newlines removed as an literal to a `String s` that you execute with `js.executeScript(s)`. After that your library should be available. – halex Apr 20 '15 at 19:03
  • @halex - Thanks for your answer , could you please explain about "with all newlines removed as an literal" ? – Igal Apr 27 '15 at 08:45
  • @Igal Remove all occurrences of newlines in your library (like you want to minimize it) to get the source as one big string. This only works if all statements are properly terminated with `;` otherwise you would need to walk through the library's code and bring it into a form that you can use as one big line by hand. – halex Apr 27 '15 at 10:32
  • @halex - this site for example, is it what I need ? http://www.textfixer.com/tools/remove-line-breaks.php – Igal Apr 27 '15 at 10:45
  • @Igal Yes you can use this site. In addition you should escape all the `'` and `"` with backslashes inside the script. – halex Apr 27 '15 at 12:09
  • @halex - then the entire script will be embedded inside the script's definition ------------> String IncludeYUI = "script = document.createElement('script');script.type = 'text/javascript';script.async = true;script.onload = function(){};script.src = '" + HERE_GOES_THE_SCRIPT_ITSELF + "';document.getElementsByTagName('head')[0].appendChild(script);"; – Igal Apr 27 '15 at 12:17
  • The site didn't work, what do I miss ? ---> getting an error from js.execution ------------> SyntaxError: Unexpected identifier\n (Session info: webview=)\n – Igal Apr 27 '15 at 13:20

2 Answers2

0

So, you're loading an html page somewhere, right? Conceptually you would load your JS file the same way: you make a request to your server to load the JS file, just like you did to load your html page.

That would look like this:

<script src="scripts/yourFile.js">

Also, I've never seen anyone loading a js file like you're doing in your code sample...I would most definitely just recommend putting a script tag in your html.

You may want to post your html code as well; we'll be able to provide better help. I'll update this answer accordingly if needed.

wholevinski
  • 3,658
  • 17
  • 23
  • There is no HTML code. The main Idea is to click on webelement on hybrid mobile app. The 'js' is a JavascriptExecutor which comes with selenium package – Igal Apr 20 '15 at 13:29
  • eventually the executed code looks as the following (log file): --> POST /wd/hub/session/fd19efd9-1365-4a2d-9613-19a19b4d518f/execute {"script":"script = document.createElement('script');script.type = 'text/javascript';script.async = true;script.onload = function(){};script.src = 'yui-min.js';document.getElementsByTagName('head')[0].appendChild(script);","args":[]} – Igal Apr 20 '15 at 13:47
  • Ok, I'm sort of lost then. So you're using selenium...and you're loading JS into your selenium-created browser session? So you're injecting code into an existing html page? You may want to try and explain the full problem more in your original question. It's very much possible to load a JS file from a local server somewhere behind your firewall/DMZ/whatever if you can't hit the outside world, but without a better idea of what you're trying to accomplish, we might not be able to help you much. – wholevinski Apr 20 '15 at 14:08
  • 'in order simulate a "click" on hybrid mobile app' -- This isn't quite right. You're injecting the YUI library into the page. And I still can't understand why... Whatever site you're testing has its own responsibility of importing the JS file, not your selenium code. Your selenium code is _injecting_ the download of a JS file into the head of that html page. Let's back up for a second: what JS is the site currently pulling in already? How about images, or css? If you can find that out, you can keep a local copy and put it in the same general location and we can go from there... – wholevinski Apr 27 '15 at 12:10
  • Better yet, load the html page yourself in a browser. Then paste the current section in your original question for me please. – wholevinski Apr 27 '15 at 12:21
  • I'm injecting js code into the "webview" due to the reason that regular selenium driver has issues with "clicking" on webelements ... that's why I'm using this workaround which actually was suggested by my coworker which is located in different site where there is no issues with the proxy so she simply providing a url path as a script source , and for her it works perfectly , I have a proxy issue in my location and I cannot download the script , that's why I need to have it locally – Igal Apr 27 '15 at 13:24
  • Ok, so your best option still is to put the yui js file in the same directory your site is pulling scripts and other assets from. I don't see how or why pulling in YUI components will help selenium clicking issues (which I've never had a problem with...seems odd), but if you want to go this route, it's your best bet. If you have control over what's being served, put the script in the directory your app is pulling JS from; if not, please coordinate with the dev(s) who do. – wholevinski Apr 27 '15 at 13:49
  • Maybe there is some issue with selenium on clicking hybrid app's controls (the actual selenum driver I'm using is AndroiDriver of appium which has acontext switch to WEBVIEW ... – Igal Apr 27 '15 at 13:56
0

Finally , after many tries , some1 has suggested me to work with jquery. after some digging , I've used executeScript with jquery's tap , and it worked...

$('#btn_login_button').trigger('tap');

I was wondering all other methods with click and element's coordinates didn't work

Igal
  • 4,603
  • 14
  • 41
  • 66