nightwatch.js: keyboard action - javascript

I am using nightwatch.js to write automation scripts. I want to use keyboard keys but it seems not working.
I have tried:
hitEnter: function () {
this.setValue('#submitButton', this.Keys.ENTER);
}
call this function in test_file.js as
loginPage
.hitEnter();
It gives error TypeError: Cannot read property 'ENTER' of undefined
What am I doing wrong?

this.Keys.ENTER
In this case, this = loginPage , not browser, you should execute with browser object through api :
this.setValue('#submitButton', this.api.Keys.ENTER);
edit:
api will return an object which contains " custom command/assertion" + "core command/assertion" + "global variables". Keys is core command ,based on selenium.

Related

How to push in 'class instance' or 'this' into a 'Function' method

i am trying to put the user defined logic (Javascript) in frontend and my backend function execute it with 'Function' method (as below)
let cmdResult = Function('"use strict"; let _AW_DAY_SUMMARY_=' + JSON.stringify(_AW_DAY_SUMMARY_) + ';' + sUserDefinedCmd)()
...
this.alicia.dump(); // run the code correctly if hardcoded in script. But hit error below when defined by user frontpage
The user defined logic work correctly, and i was able to push in object with tons of values with JSON.stringify method. But i have a challenge as i have loaded quite a number of classes
constructor(
private alicia: AliciaService,
private benny: BennyService,
private cass: CassService,
) { }
How could the user defined logic to allow call like this.alicia.dump();
Whenever i use it in my user defined logic, it complained
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'alicia' of undefined
There are many business process intensive method in those classes and i need the user defined logic to call and return rather than re-write from scratch.
The execution context of function that was created through Function constructor is a global object: window in your case.
In order to change this context you specify with each context to execute your function through Function.prototype.call():
Function('...').call(this)
Stackblitz Example

Javascript square bracket notation couldn't be evaluated properly while using an android webview. Any advice to fix it?

I'm using android webview and want to make dynamic function calls over the Javascript Interface. My interface's name is AndroidBridge. And here is my code:
AndroidBridge[key](values[key]);
It works properly on the browser. But when it comes to my webview, is throwing the error below:
Error connecting controller TypeError: AndroidBridge[key] is not a function [object Object]"
Any advice to jump over that issue?
I can give you the two possible reasons for that issue now:
Missing #JavascriptInterface annotation.
The method is missing or params not matching.
I don't know why browser's js interpreter looks to "a square named version of that object", but method names that match should work without that errors.
I can leave here a fully working example:
class JsBridge {
#JavascriptInterface
fun showToast(message: String) {
Log.d("DEBUG", "JsBridge showToast message:$message")
val currentActivity = current ?: return
Toast.makeText(currentActivity, message, Toast.LENGTH_SHORT).show()
}
companion object {
#JvmStatic
val instance: JsBridge = JsBridge()
}
}
// Somewhere in your code
webView.addJavascriptInterface(JsBridge.instance, "AndroidBridge")
Now you can call that method in the js side:
AndroidBridge.showToast('A message');
// or
AndroidBridge['showToast']('A message');
They both should work.

Change data from html page with Javascript in WebView

In my Android application, i need to use a WebView for one function. But i want translate this webpage. So i've make a Javascript code for remplace "MyValueEN" in "MyValueFR" with string.xml but i can't usegetResources.GetString(...).
Exemple of my code:
String js_string = "javascript:\n" +
"document.getElementsByClassName('Div1')[0].style.display='none';\n" +
"document.getElementsByClassName('Div2')[0].style.display='none';void(0)" +
"document.body.innerHTML = document.body.innerHTML.split(\"MyTextToChange\").join("+ getResources().getString(R.string.MyTextToChange) + ");void(0);"; //It's here the error
When i call getResources().getString(R.string.MyTextToChange"), i've got an error :
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test/com.test}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Can i use getRessources() in Javascript code ? Did i have do an error ? Or i need use another function ?
Thanks for your help
you will need to initialise
getResources().getString(R.string.MyTextToChange)
inside onCreate, incase you initialised it outside

issue with $cordovaContacts

i'm new in stackoverflow forum and i decide to create this topic to solve a problem wich i'm stuck for 2 weeks.
I have the following :
Visual Studio Android Emulator
Ionic 1 project created with the tabs pattern.
I'm trying to use the ngCordova contacts module ( $cordovaContacts ) to retrieve the phone numbers of the contacts. The problem is that there is an error with the "navigator" object. I found that there is not possible to acces the fields of these object ( functions , app , contacts , splashscreen ) so when i call to $cordovaContacts.find(...) it shows the error : "Can not find property find of undefined" . I invested some time debugging and when i use :
console.log(navigator);
The console shows:
[object Object]
services.js (21,15)
CordovaNavigator
_ {_
_ [functions]: ,_
_ proto: { },_
_ app: { },_
_ contacts: { },_
_ splashscreen: { }_
_ }_
But when i print navigator.contacts object it shows undefined
I also tried to use navigator.CordovaNavigator but is also undefined.
This code is called when loading controllers.
I have no idea why it shows that navigator has an atribute contacts and then when i call it it shows UNDEFINED . Maybe i need to stablish some acces rights ??.
I'm really lost so anything you tell me will help. Thanks a lot.
If you are trying to fetch the contacts on controller load (means, that you don't trigger it manually inside another method or via a timeout / interval), you'll need to wrap it with the ionicPlatform.ready() method, else the plugin isn't ready at that point.
ionic.Platform.ready(function(){
$cordovaContacts.find().then(function(allContacts) {
$scope.contacts = allContacts;
});
});

Javascript variable function method call

I apologise if this has been asked elsewhere, I looked but without knowing the name for what I am doing is, I couldn't find anything.
Anyways, the code is as follows:
function alertTypeOptions(AlertType, AlertOptions) {
navigator.notification.AlertType(AlertOptions);
}
This code is for a phonegap / cordova application.
The basic idea is that you pass the function a two variables and these are used to execute the appropriate method. Examples of this could be alertTypeOptions('beep', '3') or alertTypeOptions('vibrate', '2000'). This (should) play the default alert tone 3x or vibrate the phone for 2 seconds.
I am currently getting the following error:
02-21 15:36:07.185: E/Web Console(7206): Uncaught TypeError:
Object #<Object> has no method 'AlertType'
at file:///android_asset/www/res/scripts.js:181
Obviously the function is currently just using the alertType variable as written rather than as a variable.
Is there a way to get this to work elegantly? Currently my only thoughts are to use a switch statement with AlertType as the check.
Jack
It looks like you want to access your function using the bracket notation :
navigator.notification[AlertType](AlertOptions);
or rather, if I trust this documentation and if AlertOptions is an array :
navigator.notification[AlertType].apply(navigator, AlertOptions);

Categories

Resources