I'm completely beginner to frida.
I've this final method which belongs to class say X.
I want to extract the value of token variable -> result.getToken() when i hook frida to the android app which contains that class at runtime.
can anyone complete this code with javascript API of frida to get the value of token variable ?
Java.perform(function () {
Java.choose("com.xx.xx", {
onMatch: function (inst) {
//.................................
}
});
console.log("Done");
});
then i'll use --> frida -U -f "xxx.apk" -l test.js
thank you so much for help !!
Java.choose is in most cases the wrong approach because that only lists the existing instances of a class, so you can only hook a method if there is already an instance loaded into memory.
The common way is to hook the method itself so that all existing and newly created instances use your hook.
var classInstanceIdResult = Java.use('com.google.firebase.iid.InstanceIdResult');
var getTokenMethod = classInstanceIdResult.getToken.overload();
// replace the getToken() method with out own implementation
getTokenMethod.implementation = function () {
// call the orignal method
var ret = getTokenMethod.call(this);
// do something with ret
console.log("Token: " + ret);
return ret;
}
BTW: The code for hooking a Java method can simply be generated by using Jadx-Gui. Just decompile the APK, select the method and let Jadx generate the Frida code snipped necessary to hook the method (see context menu of the method).
I'm trying to test a ternary branch which is using window.location.
#Injectable()
export class ABCService {
private hostUrl = (window.location.host.indexOf('localhost') > -1) ? 'example.com' : window.location.host;
...
TestCase:
it('check alternate URL', () => {
const URL = 'http://example.org/postMethod';
service.getData().subscribe(data => {
expect(data.length).toBe(1);
});
const req = httpMock.expectOne(URL);
expect(req.request.url).toBe(URL);
req.flush({data:[1]});
});
Getting Error:
Error: Expected one matching request for criteria "Match URL: http://example.org/postMethod", found none.
Any ideas of how can I test if window.location.host is not localhost.
Tried (window as any).location = { ...window.location, host: 'http://example.org' };
still getting host == localhost:9876;
OR
Is there any way I can override window.location.host value of mock it?
Instead of referencing a global variable, which might not be defined depending on what environment you are running your app in (e.g. Angular Universal), use dependency injection.
In this case you can get the information you want from Angular's DOCUMENT constant (https://angular.io/api/common/DOCUMENT).
constructor(#Inject(DOCUMENT) private doc) {
console.log(this.doc.location.href);
}
And in your tests you could mock it with:
const service = new ABCService({location: {href: 'my-test-url'}} as any);
I need to have the whole code to understand yor question and test case more accurately, can you please supply a Stackblitz url for it?
Anyway, you can use jasmine's spyOn function to mock window.location. Thisway you can supply mock it's functions and their return values, without actually setting the host.
Look at Jasmine docs for more information.
I use Cucumber 3.x and I want get scenario name in Before hook. My code seems like this:
Before(function (scenario) {
let name = scenario.name;
console.log(scenario) //debug printing
return require("./driver").createDriver(desired);
});
And console.log in Cucumber 3.x shows only this:
{ sourceLocation:
{ uri: 'name_my_feature.feature',
line: 206 } } //206 - is line current scenario name
How I can get scenario name?
I found it under scenario.pickle.name
I am having an issue with selenium js
I have created my components in json like:
"usernameInputField": {
"selector": {
"xpath": "//*[#id='username']"
}
}
and I am calling webdriver :
var webdriver = require('selenium-webdriver');
using the data like this:
console.log(webdriver.By.xpath("//*[#id='username']"));
it calls correctly
however when I try to run console.log(webdriver.By(usernameInputField.selector));
I get an error (TypeError: Class constructors cannot be invoked without 'new')
what am I doing wrong here?
You can use regular findElement directly without having to use the By "class":
driver.findElement(usernameInputField.selector);
Just a quick question... I currently have the following jQuery code with a selector in it.
var ID = "idControl"
function doesTreeViewExist()
{
if($('#' + ID).length == 0)
{
return false;
}
else
{
return true;
}
}
I was wondering how do I write the test to test the selector using QUnit? More specifically, I'm having trouble coming up with the syntax/code.
EDIT:
Ok, suppose now I want to mock the selector call instead because I cannot get access to the actual website. I'm using JsTestDriver as my testing tool, which means I cannot touch the browser the tests are running in (else the test runs will stop). What about in such situation? How can I possibly test the code?
Thanks.
The function you post, can be heavily simplified:
var ID = "idControl";
function doesTreeViewExist() {
return !!$('#' + ID).length;
}
Using the !! construct (double bitwise NOT), to convert the length property to Boolean, it will return false only when length is zero.
Speaking about qUnit, you can easily setup a simple test like this:
test("Your selector test", function() {
ok($('#idControl').length > 0, "idControl exists");
// or simply
ok($('#idControl').length, "idControl exists");
});
The ok function does a boolean assertion, equivalent to JUnit's assertTrue.
I test selectors manually, then pass them to code that uses them. I can then unit test the code that uses them. If you want to just test a selector, you need access to the HTML it affects. Your test could include HTML to target, something like:
test("selector works", function() {
var html = $('<input type="select"><option value=0/></input');
var result = $('option', html);
ok(result.count() == 1);
});
But I don't do that... I put my selectors at the edge of the code so I can get to them quickly and step through them under the debugger. I'll have a simple class whose properties are those selectors. Then I'll mock/stub that simple class, so I can write code for everything dependent on those selectors.
The reason I don't test my selectors is because the HTML they target is generated by ASP.NET code, and hard to get to from a javascript test. But I can wrap them in a Humble Object ("http://xunitpatterns.com/Humble Object.html") then test code that depends on that humble object. Here's a simple wrapper class that I can replace with test doubles:
var createSelectWidget = function(rootSelector)
{
return {
userText : $('span', rootSelector),
inputList : $('option', rootSelector),
};
}
Whatever dependency injection pattern you use, you can then stub it like below. Suppose my widget has a select input to read the value of, and a span I want to write some results to:
var createSelectWidgetStub = function()
{
return {
userText : { text = function() {}},
inputList : { val = function() {}},
};
}
I can then pass around this stub in tests where I want to isolate the dependency but don't care about interactions with that dependency. When I want to verify interactions with the dependency, I can mock it with JSMock. Suppose I want to verify interactions with the input list, I would prepare a stub with the one element mock'd:
var selectMock = createSelectWidgetStub();
selectMock.inputList = mc.createMock(selectMock.inputList);
I used Jack and successfully mocked the jquery call and returned a custom length and expected result.
Something like:
jack (function() {
jack.expect("$").exactly("1").withArguments("#" + ID).returnValue( {length:0} );
doesTreeViewExist()
assertEquals(false, result);
});
I have also managed to supply a DOM structure to satisfy the jquery call, as I'm using one of JsTestDriver's built-in function HtmlDoc. Something like:
/*:DOC += <span id="idControl"></span> */
or simply create a DOM sturcture without the specified id to get the false statement.
HTH.