using selenium based python code on azure server - javascript

Selemium uses browser to open the page and get content. But on my azure server(command line based) I could not have chrome or firefox. So what is the alternative to use python based selenium code on azure server.
I looked at http://phantomjs.org/ a headless browser. But I guess it is javascript so I would require to convert python code in JS.
Is there any other better alternative?
code snippet:
driver = webdriver.Chrome()
def getVideoTrend(self, item):
driver.get(item['link'])
element = WebDriverWait(driver, 20).until(lambda driver: driver.find_elements_by_class_name('yvp-main'))
self.yahoo_video_trend = []
for s in driver.find_elements_by_class_name('yvp-main'):
print "Processing link - ", item['link']
trend = item
trend['video_link'] = s.find_element_by_tag_name('video').get_attribute('src')
print s.find_element_by_tag_name('video').get_attribute('src')
self.yahoo_video_trend.append(trend)

Try using requests for your browsing needs and BeautifulSoup4 for parsing

So what is the alternative to use python based selenium code on azure
server.
May I know whether you are using Azure Web App? Per my understanding, Azure Web App does not allow us to install the custom software on the server. So in this case you may want to use a virtual machine instead. Windows based virtual machines have GUI pre-enabled, so you only need to use remote desktop to login to the server and install a browser (if you don’t want to use the default IE). For a Linux server, you can install a desktop and then install a browser as well. Please refer to http://blogs.technet.com/b/uktechnet/archive/2013/11/12/running-a-remote-desktop-on-a-windows-azure-linux-vm.aspx to see if this instruction is helpful.

Related

How to automate the Mobile Virtual device testing using Testcafe

I wrote a simple code(using Javascript) to launch a site and to do some operations.
Using the Testcafe remote command, I am able to get the remote connection URL and QR code.
3.Using(copied) the remote URL, I wrote another Java program to run the remote URL in the virtual device using appium.
Can someone let me know, How can I automate the process to link step2 and step3?
The question is tagged as 'testcafe', but it is more related to automation of processes than to e2e testing with TestCafe.
TestCafe itself only gives you the remote link.
If you want to run your tests on mobile devices, you can check the Browsers in Cloud Testing Services article.
 

Running a script command remotely using Python

I have a Python program that uses BeautifulSoup to extract some data from a website.
In Google Chrome, there is this option called the Developers Console; it is used to execute javascript commands live right on the webpage.
Is there a way, or a work-around-of-a-way to execute javascript commands remotely using Python? Like casting a webpage to an object and running a javascript command in the background (without launching a new Chrome window)?
You can use Native Messaging API to communicate between a shell script and the browser.
You can also launch Chrome or Chromium with --headless flag set. See also puppeteer.

How to create a simple WebSocket Server in Chrome App?

I'm trying to develop a Chrome App that will work together with a Chrome Extension that I already created, wherein the Chrome Extension will send information to the Chrome App.
For this communication I thought use the WebSocket locally, in Chrome Extension I managed to make the Client, but now I'm having difficulty in creating the Server in the Chrome App, because I wanted to make as simple as possible without having to install something beyond of the Chrome App.
Among the first Google results there is a sample app from Chrome team: Http WebSocket Server.
You've got to understand that making a server in Chrome Apps is difficult; you are given access to a raw socket, and you need to fully implement the protocol that a particular server must use. Even a HTTP server is non-trivial, WebSockets is even less so.
So: it's possible, but it's not simple unless you're using an existing library.
Just to add to the accepted answer:
There is a Chrome Extension already in the Chrome Web Store: Web Server for Chrome.
And it is opensource: GitHub Link. You can use it as a library to your Chrome App.
Cheers!

Embedding node.js in a Firefox extension and running a server in-browser

I am trying to figure out how to embed node.js within a Firefox extension, so that I can run a persistent server process (specifically PeerServer) from within the browser, as long as the user has the extension enabled. The only thing I've been able to find online is this guide ... but I haven't been able to make those instructions work, and need to find some more resources.
1) Does anyone have any other resources besides the article I linked to above that talk about embedding node.js in Firefox extensions? Any code examples?
2) Is there some reason that it would not be possible to run a persistent server process such as PeerServer from within a Firefox extension? Are there some kind of limitations on extensions that would prevent me from being able to do that?
You can just have the executable in a folder of your extension and have JS code in the extension launch that executable. Running an external executable is described in the resource you linked or here at MDN.
Example copied from MDN:
var file = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
file.initWithPath("myapp.exe");
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(file);
var args = ["argument1", "argument2"];
process.run(false, args, args.length);
A bit more logic is needed to find the absolute path of the user's profile to derive the path of the application to launch but it's doable.
Now if you want to interact with node from the extension you could use HTTP requests as a means of communication.
It sounds a bit strange to embed node in Firefox though when Firefox itself has a JS engine at its core. A more elegant approach would be to try to get PeerJS running directly in Firefox addon context, without node. Maybe more complicated but it should be possible. See for example this addon "Browser Server".

how to communicate with a desktop application from browser?

Is it possible to communicate with a desktop application from browser?
I want to do something like this,
Let's say there is a button in my web application with a URL to a data source and when button is clicked desktop application opens and get that data source URL and process data with desktop application.
Is it difficult to do such thing? Any examples?
On windows its trivial to create a custom URL Protocol that's invokable via
..
This works in IE, FF and Chrome, although in the latter the link must be opened via javascript to avoid omni-bar confusion.
You will need to have something running on the deskop, like a server, and make a request to it for the server to open up an application. You could do it with a Node.js. Of course that requires the server to be running on the client's desktop.
The alternative would be to make a browser extension / plugin, and have people install that. Those extensions could probably launch an application on the desktop.
You can easily add Fleck WebSocket server to your desktop application, and then access this using Websocket.
Note: Only Windows 8 and 10 support WebSockets through Microsoft's WebSockets implementation, but Fleck will work with Windows 7.
https://github.com/statianzo/Fleck
It's quite easy to add Fleck to your project using NuGet Package Manager:
Install-Package Fleck
Here is the echo example from Fleck webpage (add this to the C# program to execute during startup):
var server = new WebSocketServer("ws://127.0.0.1:8181");
server.Start(socket =>
{
socket.OnOpen = () => Console.WriteLine("Open!");
socket.OnClose = () => Console.WriteLine("Close!");
socket.OnMessage = message => socket.Send(message);
});
In the javascript:
var exampleSocket = new WebSocket("ws://127.0.0.1:8181", "protocolOne");
exampleSocket.send("Here's some text that the server is urgently awaiting!");
//and receive (make a listener for the socket) :
exampleSocket.onmessage = function (event) {
console.log(event.data);
}
Hm, you need something like client-server application. The server is a lightweight http server, which is waiting for messages from the client (browser). The browser can communicate with your server via ajax for example.
Here is a clunky suggestion, but I think worth mentioning all the options since the custom URI and running server solutions are pretty involved... Generate a small file containing the parameters of interest, with a custom extension associated with your desktop app. So when the user clicks the browser button they will have to go through the browser's file download dialog/toolbar and maybe some annoying security verification popups... not ideal user experience, but might be the easiest way to implement this type of communication, and doesn't require a process running in the background like a server.
I have a web app used within my company for interfacing to old databases and poorly organized files. I need a way to allow the users to open the actual files from the network and not download copies, so they can be edited in place. Considering a solution like this or the custom URI scheme so that a small executable not running in the background can simply be passed the filename and open it for the user directly.
the desktop application should embed a small server in it, like Jetty. Since the browser content source domain (e.g. www.myDomain.com) is different than the localhost domain of the Jetty, you would run into security problems. These should be overcome by the use of CORS (Cross Origin Resource Sharing) which is a new standard. Using CORS, the Jetty server tells the browser that it/localhost allows Cross domain access to its resources if the requests originate from the source domain www.myDomain.com. For security reasons, i would also make the Jetty reject any request whose source ip is not localhost
In addtion to Alex K's answer for windows... for those looking for solution on macOS and Linux.
Linux
Most of the modern distros implement freedesktop standards and one of them is desktop files. You can create a desktop file with [service]
section.
$ cat test.desktop
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Comment=My test app
Name=TestApp
Icon=TestIcon
Exec=/opt/test/test.sh %u
DBusActivatable=true
Categories=Network;
MimeType=x-scheme-handler/test; <------ This is handler for test://somedata URLs
NoDisplay=false
Copy this file in /usr/share/applications/test.desktop
macOS
Simply add something like following in your applications Info.plist file
<array>
<dict>
<key>CFBundleTypeIconFile</key>
<string>/tmp/test.png</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>com.mytest</string>
<key>CFBundleURLSchemes</key>
<array>
<string>test</string> <---- This is handler for test://somedata URLs hit on browser
</array>
</dict>
</array>

Categories

Resources