Lua script fails but the JS works in console - lua

I have this very basic lua script that returns an error, but running the querySelector directly in the console works just fine.
Any hints on what is wrong with my lua?
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(1))
assert(splash:runjs('document.querySelector("button.btn.btn-primary.btn-show-rates").click()'))
splash:set_viewport_full()
return {
html = splash:html(),
}
end
BTW: website is here
Error log:
{
"type": "ScriptError",
"error": 400,
"info": {
"type": "LUA_ERROR",
"line_number": 4,
"error": "JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
"message": "Lua error: [string \"function main(splash, args)\r...\"]:4: JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
"source": "[string \"function main(splash, args)\r...\"]"
},
"description": "Error happened while executing Lua script"
}

Turns out it was kinda simple...
A slightly longer "wait" and changing to document.querySelectorAll made it work. Final code here:
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(5.0))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[0].click()'))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[1].click()'))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[2].click()'))
splash:set_viewport_full()
return {
html = splash:html(),
}
end

Related

JSON data types cannot be deserialized from a GraphQL query by using Ferry package

I have this GraphQL query:
query QuoteRequests($page: Int!) {
getQuoteRequestsList(page: $page) {
vehicle
body
licensePlate
vin
quality
currency
items
}
}
It generates this sample result:
{
"data": {
"getQuoteRequestsList": [
{
"vehicle": "1997 TOYOTA AVALON 4 DR ",
"body": "Sedan",
"licensePlate": "RHJ456",
"vin": "XBGGDFYYREAXVJJJD",
"quality": [
"GENUINO"
],
"currency": "USD",
"items": [
{
"uid": "74355f85-5312-9999-8acd-709ceccda00a",
"name": "Doble cero que es",
"description": "no me interesa",
"quantity": 11
},
{
"uid": "66db6fe0-1044-4d58-8454-5e51ab7a313f",
"name": "El arenero",
"description": "el duende verde",
"quantity": 2
}
]
},
]
}
}
The items data is a JSON type, and when trying to fetch that data by using Ferry package with
client.request(GQuoteRequestsReq((b) => b..vars.page = 0)).listen((response) => print(response.data.toString()));
I get a Null result, but if I leave items out of the query, I get a no-null result. I have no problem if I run that query using the HTTP package.
Is this is an error from Package? or do I need to configure something?
Thanks.
UPDATE 1
This is definitely an error because if I try to make a hot-reload in Flutter I get this error in debug console:
Reloaded 1 of 1700 libraries in 860ms.
E/flutter ( 2286): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Deserializing '[__typename, Query, getQuoteRequestsList, [{__typename: GetQuoteRequestsRecor...' to 'GQuoteRequestsData' failed due to: Deserializing '[{__typename: GetQuoteRequestsRecord, uid: 0bf6709f-7ab7-464e-8ee3-6a94e46f05...' to 'BuiltList<GQuoteRequestsData_getQuoteRequestsList>' failed due to: Deserializing '[__typename, GetQuoteRequestsRecord, uid, 0bf6709f-7ab7-464e-8ee3-6a94e46f057...' to 'GQuoteRequestsData_getQuoteRequestsList' failed due to: Deserializing '[{uid: 16870250-5acb-4c23-a7e4-f4e23bbd23ad, name: Doble cero que es, descrip...' to 'GJSON' failed due to: type 'List<dynamic>' is not a subtype of type 'String?' in type cast
E/flutter ( 2286): #0 BuiltJsonSerializers._deserialize
package:built_value/src/built_json_serializers.dart:178
E/flutter ( 2286): #1 BuiltJsonSerializers.deserialize
package:built_value/src/built_json_serializers.dart:124
It's clear it is fetching the expected result but it cannot deserialize properly, then it returns a null result.
Is there any workaround?
UPDATE 2
I've tried this solution but I'm getting same error.
In my case, I've followed these steps, I've
installed built_value package,
edited build.yaml file,
run the command flutter packages pub run build_runner build
The answer can be found in this issue.

Lua Error: 'null is not an object (evaluating \'document.querySelector...).click\')'

I'm scraping https://www.tayara.tn/sc/immobilier/appartements and I need to click on a "load more" button so I used this Lua script:
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(0.5))
assert(splash:runjs('document.querySelector(".DbXTC b7ygi _1JgR4 _1U08s button").click()'))
splash:set_viewport_full()
return {
html = splash:html(),
png = splash:png(),
har = splash:har(),
}
end
after clicking on "render" button, I had this error:
{
"description": "Error happened while executing Lua script",
"type": "ScriptError",
"info": {
"line_number": 4,
"type": "LUA_ERROR",
"error": "JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\".DbXTC b7ygi _1JgR4 _1U08s button\").click\\')'",
"message": "Lua error: [string \"function main(splash, args)\r...\"]:4: JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\".DbXTC b7ygi _1JgR4 _1U08s button\").click\\')'",
"source": "[string \"function main(splash, args)\r...\"]"
},
"error": 400
}
what I did wrong ?
It sounds like document.querySelector(".DbXTC b7ygi _1JgR4 _1U08s button") returns null, in which case you are calling the click method on null. .DbXTC b7ygi _1JgR4 _1U08s does not make sense: it matches an <button> tag inside a <_1U08s> tag inside a <_1JgR4> tag inside a <b7ygi> tag inside a tag with the class DbXTC. There is no such element, so document.querySelector returns null. document.querySelector(".DbXTC button") seems to work.

Submitting a transaction via RESTful API (how to handle transactionid and timestamp)

Problem:
I am unable to POST a transaction via the RESTful API generated by the composer-rest-server. I am receiving statusCode 422; the transaction instance is not valid. However, the same example works in the Playground.
Scenario:
I've set up a transaction called Offer in my .cto file which posts an offer to buy a house:
// Offer - Specifies an offer that a bidder places on a house listing with an associated price
transaction Offer {
o Double bidPrice
--> HouseListing listing
--> Person bidder
}
The composer-rest-server has generated an API with the following JSON string to post a transaction of type Offer:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 0,
"listing": "string",
"bidder": "string",
"transactionId": "string",
"timestamp": "2017-07-21T13:37:09.460Z"
}
I've since replaced this with a sample transaction using the following JSON code derived from the above example:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 1000,
"listing": "001",
"bidder": "RJOHNSON",
"transactionId": "1b9aa63c-dfad-4aad-a610-dfc80f2796b2",
"timestamp": "2017-07-21T13:37:09.460Z"
}
The response returned is error code 422:
{
"error": {
"statusCode": 422,
"name": "ValidationError",
"message": "The `Offer` instance is not valid. Details: `transactionId` can't be set (value: \"1b9aa63c-dfad-4aad-a610-d...6b2\").",
"details": {
"context": "Offer",
"codes": {
"transactionId": [
"absence"
]
},
"messages": {
"transactionId": [
"can't be set"
]
}
},
"stack": "ValidationError: The `Offer` instance is not valid. Details: `transactionId` can't be set (value: \"1b9aa63c-dfad-4aad-a610-d...6b2\").\n at /usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/dao.js:355:12\n at ModelConstructor.<anonymous> (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:566:11)\n at ModelConstructor.next (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/hooks.js:93:12)\n at ModelConstructor.<anonymous> (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:563:23)\n at ModelConstructor.trigger (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/hooks.js:83:12)\n at ModelConstructor.Validatable.isValid (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:529:8)\n at /usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/dao.js:351:9\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:178:5)\n at Function.ObserverMixin.notifyObserversOf (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)\n at Function.ObserverMixin.notifyObserversOf (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)"
}
}
Now the strange thing is that I've deployed the same BNA onto the Hyperledger Composer Playground and am able to execute transactions of type Offer successfully.
Note that in the Playground, "transactionId" and "timestamp" are not specified as the Playground appears to take care of these values. For example, this is what Playground proposes to me initially:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 0,
"listing": "resource:org.acme.purchasing.HouseListing#id:7965",
"bidder": "resource:org.acme.purchasing.Person#id:4441"
}
Can anyone advise why it's saying the Offer instance is not valid? My first thought was that it's not liking the string I'm placing in "transactionId" but another Stack Overflow post points out that the transactionId is just an arbitrary UUIDv4 string which I've generated already.
Update #1: Failing even with default demo
In order to ensure by BNA is error-free, I've deployed the default carauction-demo (resembles my example closely) onto my local Hyperledger Fabric instance and deployed the composer-rest-server. I've also deployed the same BNA into the Playground. All assets and participants were created identical in both from the Explorer (local instance) and Playground. When it comes time to submit an Offer transaction:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 800,
"listing": "resource:org.acme.vehicle.auction.VehicleListing#L001",
"member": "resource:org.acme.vehicle.auction.Member#member3#acme.org"
}
This JSON was generated by the Playground and succeeds there. Copy/paste/executing into the Explorer yields a status 500 error.
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "error trying invoke chaincode. Error: chaincode error (status: 500, message: Error: Object with ID 'string' in collection with ID 'Asset:org.acme.vehicle.auction.VehicleListing' does not exist)",
"stack": "Error: error trying invoke chaincode. Error: chaincode error (status: 500, message: Error: Object with ID 'string' in collection with ID 'Asset:org.acme.vehicle.auction.VehicleListing' does not exist)\n at _initializeChannel.then.then.then.then.catch (/usr/lib/node_modules/composer-rest-server/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:806:34)"
}
}
I'm still at a lost as to what is wrong here.
After much experimentation and some searching, I've concluded that the problem was that NPM was installed using sudo (as root). I redid the installation as non-root and the problem has now been solved. Everything is working as expected.
Composer updates the transactionId itself as its generated, you cannot do this in your JSON and hence why you get the error. This is a Loopback -> Swagger conversion issue as it should not appear in a /POST REST operation - captured here https://github.com/hyperledger/composer/issues/663
So I have successfully executed Offer transactions (/POST). I think the '500' error is because there is a missing field ('string' type), or relationship in your Offer transaction via REST.
Using the example of the Car Auction network https://github.com/hyperledger/composer-sample-networks/blob/master/packages/carauction-network/models/auction.cto
Either of these Offer transactions using /POST were successful in Explorer:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com"
}
OR
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com",
"timestamp": "2017-07-28T14:07:02.558Z"
}
responded with a 200 (SUCCESS) and a transactionId in Explorer:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com",
"transactionId": "e75b9934-1f08-4daf-90db-702bbe4b8fa1"
}
This is my VehicleListing asset JSON for #100 above
{
"$class": "org.acme.vehicle.auction.VehicleListing",
"listingId": "100",
"reservePrice": 50,
"description": "string",
"state": "FOR_SALE",
"offers": [
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 50,
"listing": "100",
"member": "resource:org.acme.vehicle.auction.Member#a#b.com",
"transactionId": "string123",
"timestamp": "2017-07-28T14:07:02.825Z"
}
],
"vehicle": "resource:org.acme.vehicle.auction.Vehicle#123"
}
And I created asset for Vehicle 123 as well as Member a#b.com

Trigger.io logging config error

I have added the following to my config.json as per the Trigger.io documentation:
"modules": {
"logging": {
"level": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
}
}
When I run forge build I get the following error:
[ ERROR] Forge API call to app/fe491ecee6fb11e19ada12313d1adcbe/template went wrong: Value [u'DEBUG', u'INFO', u'WARNING', u'ERROR', u'CRITICAL'] for field 'level' is not of type string
Is the documentation wrong or am I misreading it?
You have to choose one of the options of the array given in the docs, for example:
"modules": {
"logging": {
"level": "DEBUG"
}
}

RestClient undefined method `delete_if' for #<String:0x4b699a8>

OK so, I'm using RestClient and sending this:
{"x_select_params":["account.*"],"domain":"thedomain"}
Via
response = RestClient.get servername, #payload
And I get
undefined method `delete_if' for #<String:0x4b699a8>
In response.
I know that the request is good as it works fine when I use the Chrome rest client extension, I'm thinking that RestClient is having an issue in parsing the response, which is supposed to look like this:
{
"user": {
"id": 4035,
"first_name": "Chris",
"last_name": "Penta",
"status": "PENDING",
"domain": "thedomain",
"default_account_id": 111315004004010,
"primary_alias_id": 131090,
"accounts": [
{
"id": 111315004004010,
"urn": "111315004004010",
"balance": 0,
"status": 0,
"currency_code": 840
}
]
}
}
Is this a bug in RestClient that needs patching? And if so, anyone know how to fix?
Thanks...Chris
I found this error when the headers were in a json format. They needed to be a hash to work.

Resources