Unable to send keyevents for navigation keys on 3rd party applications - marionette

I am doing automation on Firefox OS on a feature phone using merionette in Python.
My problem is I am not able to simulate hardware keyevents for the navigation keys and other selection keys on some applications (in below case name is ApplicationName).
However I am able to use them on native apps.
Below is my code snippet:
#creating marionette client on port 2828
client = Marionette('localhost', port=2828)
client.start_session()
#import js script to launch app by name
js = os.path.abspath("/usr/local/script.js")
client.import_script(js)
result=client.execute_async_script("GaiaApps.launchWithName('%s')" % "ApplicationName")
client.switch_to_frame(result.get("frame"))
num = "123456"
client.find_element('css selector','input[type=text]').send_keys(num)
#Above code is working fine
#below code is not working on some thirdparty applications
client.execute_script("window.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', keyCode: 40}))")
#closing the client
client.close
What could be the cause?

Related

DIAL with Cobalt

Cobalt can default load the URL. We are investigating how to add the DIAL paring code to the URL. Maybe it needs a changeURL API for avoiding re-launching YouTube.
// Restrict navigation to a couple of whitelisted URLs by default.
const char kYouTubeTvLocationPolicy[] =
"h5vcc-location-src "
"https://www.youtube.com/tv "
"https://web-release-qa.youtube.com/tv "
[scenario]
launch YTTV with your remote controller
open YouTube app on your phone
try to pairing to TV
Cobalt currently doesn't implement DIAL smooth-pairing once launched, but it does support being launched with alternate query parameters via the --url= command-line parameter.
For example:
https://www.youtube.com/tv?...anything...
Will pass the navigation whitelist.
Full implementation of smooth-pairing support is planned for Q1 2017.

Best way to control firefox via webdriver

I need to control Firefox browser via webdriver. Note, I'm not trying to control page elements (i.e. find element, click, get text, etc); rather I need access to Firefox's profiler and force gc (i.e. I need firefox's Chrome Authority and sdk). For context, I'm creating a micro benchmark framework, not running a normal webdriver test.
Obviously raw webdriver won't work, so what I've been trying to do is
1) Create a firefox extension/add-on that does what I need: i.e.
var customActions = function() {
console.log('calling customActions.')
// I need to access chrome authority:
var {Cc,Ci,Cu} = require("chrome");
Cc["#mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler);
Cu.forceGC();
var file = require('sdk/io/file');
// And do some writes:
var textWriter = file.open('a/local/path.txt', 'w');
textWriter.write('sample data');
textWriter.close();
console.log('called customActions.')
};
2) Expose my customActions function to a page:
var mod = require("sdk/page-mod");
var data = require("sdk/self").data;
mod.PageMod({
include: ['*'],
contentScriptFile: data.url("myscript.js"),
onAttach: function(worker) {
worker.port.on('callCustomActions', function() {
customActions();
});
}
});
and in myscript.js:
exportFunction(function() {
self.port.emit('callCustomActions');
}, unsafeWindow, {defineAs: "callCustomActions"});
3) Load the xpi during my webdriver test, and call out to global function callCustomActions
So two questions about this process.
1) This entire process is very roundabout. Is there a better practice for talking to a firefox extension via webdriver?
2) My current solution isn't working well. If I run my extension via cfx run directly (without webdriver) it works as expected. However, neither the sdk nor chrome authority do anything when running via webdriver.
By the way, I know my function is being called because the log line "calling customActions." and "called customActions." both do print.
Maybe there are some firefox preferences that I need to set but haven't?
It may be that you do not need the add-on at all. Mozilla uses Marionette for test automation of Firefox OS amongst other things:
Marionette is an automation driver for Mozilla's Gecko engine. It can
remotely control either the UI or the internal JavaScript of a Gecko
platform, such as Firefox or Firefox OS. It can control both the
chrome (i.e. menus and functions) or the content (the webpage loaded
inside the browsing context), giving a high level of control and
ability to replicate user actions. In addition to performing actions
on the browser, Marionette can also read the properties and attributes
of the DOM.
If this sounds similar to Selenium/WebDriver then you're correct!
Marionette shares much of the same ethos and API as
Selenium/WebDriver, with additional commands to interact with Gecko's
chrome interface. Its goal is to replicate what Selenium does for web
content: to enable the tester to have the ability to send commands to
remotely control a user agent.

add a share button in Firefox OS application

I am creating a firefox OS application, I want the user to be able to share a link through any application installed and eligible (facebook, twitter etc). I saw android has this kind of feature and firefox OS has it as well, as I saw it in one of it's built in applications. Went through Web API, didn't find a suitable match,
Any ideas how to do it?
This is the intended use of the Web Activity API . The idea is that applications register to handle activities like share. The Firefox OS Boiler Plate app has several examples of using Web Activities. In that example a user could share a url using code like:
var share = document.querySelector("#share");
if (share) {
share.onclick = function () {
new MozActivity({
name: "share",
data: {
number: 1,
url: "http://robertnyman.com"
}
});
};
}
Any app that handles the share activity will be shown allowing the user to pick the proper app to handle the share.

Programmatically changing wireless router settings - Netgear ideally

Is it possible to programmatically change settings on a Netgear wireless router using C#? I have settings that I change often and I would like to create my own interface for making those changes. Currently I navigate to the admin web page (10.0.0.1) and it prompts me for a username and password. After I authenticate I can use the web interface to change the router's configuration.
If this isn't possible with Netgear, do any outher wireless routers have an API for developers?
There aren't any APIs out there to do this, but you can write something to make HTTP requests to the router to simulate the webUI being used.
I'm guessing most consumer routers are probably pretty simple to talk to. Authentication is probably nothing more than basic realm.
Selenium offers a firefox plugin that lets you record manual interactions with your browser. And then you can export the steps to python, ruby, java or c#. It worked for me to programmatically adjust my router settings to turn off wifi. Clicking on the elements while recording identifies everything you need.
This code works on an Actiontec MI424WR (FIOS)
Edit the code to add your username, password, and router address.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Routr(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://routerip_or_address"
self.verificationErrors = []
self.accept_next_alert = True
def test_routr(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_name("user_name").clear()
driver.find_element_by_name("user_name").send_keys("your_username")
driver.find_element_by_id("pass2").clear()
driver.find_element_by_id("pass2").send_keys("enter_your_password_here")
driver.find_element_by_link_text("OK").click()
driver.find_element_by_link_text("Change Wireless Settings").click()
driver.find_element_by_id("ws_off").click()
driver.find_element_by_link_text("Apply").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
If this is just a few things you want to change programmatically, simulating HTTP requests should be simple enough. Another option would be to install DD-WRT in your router, basically transforming it into a small Linux installation that allows full programmatic configuration through SSH using standard Linux commands.
I'm unaware of any consumer-grade routers that have an API like that, but you could always build something that (ab)uses the Web interface to do what you want, using something like selenium-rc or watir
MiktoTik sells customer grade routers that allow ssh configuration (mind that they use ssh, but not bash inside ssh). You can even roll your own PHP REST API for router (not that I like PHP, but people are doing it).
I'm not familiar with this router, but I have done similar stuff programmatically via a telnet connection the router with Python.
There's a cood telnet lib for C#:
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
There is a python based Github repo here that describes a SOAP based API. I've used it to program a device schedule for my kids devices. Not willing to pay Disney for Circle. Works great. There's also a js version here.

Ruby:Watir cannot connect to IE running under non-Admin account on 'default' desktop

Some Background on architecture of the app is needed:
Windows 2003/Apache-v2.2/IE7/Watir-v1.6.2/Ruby-v1.8.5
Apache running under 'localsystem' account.
Request to run a Watir script comes in.
Apache CGI kicks off IE7 under a particular user, e.g. 'tester', and attaches the IE7 window to the "default" desktop environment. This allows us to VNC into the machine and see all IE7 windows from all the accounts running.
IE7 has ruby embedded into the process through plugin and executes the ruby script in a thread.
For clicking on links/buttons in IE, Watir supplies a synchronous 'click' method and async 'click_no_wait' method. 'click_no_wait' spawns a completely new ruby process to connect back to the IE7 window to click the link/button.
This is what is failing for me. Since click_no_wait is spawning a new process. It cannot seem to see the IE7 window to connect to it and click on the link/button.
I have to use 'click_no_wait' due to a dialog box that IE pops up on a certain page, so that another ruby thread can close it.
I've tried a few things:
- using fire_event('OnClick') instead of click_no_wait hangs script like 'click' would.
- Modifying 'click' by commenting out '#container.wait', but the 'click!' itself is the API that hangs waiting for the dialog box to close.
- Make 'click_no_wait' spawn a new thread instead of a process', but other threads appear to be suspended while the 'click!' call executes in that thread. Which is strange.
- Executing the 'click_no_wait' process using the exact same code that is used to spawn the IE7 process, but it still can't find any IE7 windows.
By 'default' desktop described above, the code that spawns IE basically does a series of C++ calls to attach the IE7 window to the 'winsta0' desktop:
- LogonUser() // log in as tester account
- OpenWindowStation("winsta0") // to get default desktop
- SetProcessWindowStation()
- // add the user to interactive window station using (GetUserObjectSecurity, GetSecurityDescriptorDacl, GetAclInformation, AddAce, SetSecurityDescriptorDacl, SetUserObjectSecurity)
- // add user to "default" desktop using APIs listed above.
- CreateEnvironmentBlock
- ImpersonateLoggedOnUser
- CreateProcessAsUser('iexplore.exe')
- // cleanup
Running the following Watir script under the particular user IE7 is running under yields:
$IE = Watir::IE.attach(:title, /Google/)
$IE.button(:name, 'btnG').click!
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:246:in `method_missing': Windows (WIN32OLERuntimeError)
OLE error code:80040154 in
HRESULT error code:0x80020009
Exception occurred. from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:246:in "each"
Which contains:
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:246
shell = WIN32OLE.new("Shell.Application")
windows = shell.Windows ## Fails here
Let me know if I can supply more details or disambiguation! :)
Thanks,
Shane.
Ok, I have solved my own issue.
It turns out that the method that Watir uses to enumerate IE windows (i.e. Shell.Application) does not work across user spaces. So I had to write an intermediate application to retrieve the IWebBrowser2 COM pointer for the IE window, and modify the 'eval_in_spawned_process' Watir function to use this handle for 'pc = Watir::IE.bind(iWebBrowserPtr)'.
It's possible that I didn't need the external app to retrieve the IWebBrowser2 object, but I didn't know how to do it in Ruby, and I already had an IE BHO app that I could leverage for the purposes.
This now works fine. :)
Watir/page-container.rb:56
# This evaluates a cmd (e.g. "button(:id, 1).click!") in a new ruby process
# This is to primarily avoid a script hanging while a modal dialog is displayed in IE.
# It gets an IE handle from our IE BHO based off of window handle. It needs to do this
# to get around the problem of finding the IE window itself using Shell.Application
# which does not work across User-contexts.
# Note: This change probably invalidates the Watir unit-test for this API.
def eval_in_spawned_process(command)
command.strip!
load_path_code = _code_that_copies_readonly_array($LOAD_PATH, '$LOAD_PATH')
ruby_code = "require 'watir/ie'; "
ruby_code << 'controller = WIN32OLE.new("HttpPlugin.Controller"); '
ruby_code << "window = controller.BrowserFromHWND(#{hwnd}); "
ruby_code << "pc = Watir::IE.bind(window); "
ruby_code << "pc.#{command}; "
exec_string = "start rubyw -e #{(load_path_code + '; ' + ruby_code).inspect}"
result = system(exec_string)
end

Resources