JMeter Script Path missing "\" so not working? - javascript

I am trying to use the beanshell script posted here to get the path of the jmx that is being run in my jmeter test - Access to JMeter script path
It is working and if I log the output of the path when set by beanshell or view the variables with the debugger I get the path to the script displayed as I expected -
c:\my\path\to\script
but when I then try to pass that variable into sendKeys, the slashes "\" are being removed so -
c:mypathtoscript
And this doesn't work so I am unable to attach/upload my file..
Sure I am missing something stupid
Thanks

Needed to user vars.put to put the JMeter UDV value into a Javascript variable, then use javascript concatenate to link it all together.

There are at least 2 ways to get this done without using Beanshell:
Call FileServer methods from WebDriver Sampler:
someElement.sendKeys(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())
Get the value from JMeterVariables
var vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables()
someElement.sendKeys(vars.get('homepath'))
Example full code:
WDS.sampleResult.sampleStart()
WDS.browser.get('http://ya.ru')
var searchInput = WDS.browser.findElement(org.openqa.selenium.By.id('text'))
//directly access function from JavaScript
searchInput.sendKeys(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())
//alternative way - getting the value from JMeter Variables
var vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables()
searchInput.sendKeys(vars.get('homepath'))
WDS.sampleResult.sampleEnd()
Comprehensive information on accessing JMeter API classes from WebDriver Sampler and few more tips and tricks: The WebDriver Sampler: Your Top 10 Questions Answered

Related

Error Updating a Chromebook's Organizational Unit with Google Script

I have been experiencing an issue recently with a Google Script code I wrote to update a few Chromebooks' organization units.
Here is a portion of the code that I am running:
let admin = AdminDirectory.Chromeosdevices.get("my_customer", active_deviceid[serial_index])
admin.annotatedAssetId = device_ID.toString()
admin.annotatedLocation = name
admin.orgUnitPath = _location_(device_ID).toString().toUpperCase()
AdminDirectory.Chromeosdevices.update(admin, 'my_customer', active_deviceid[serial_index])
Once the script executes I get the following error:
GoogleJsonResponseException: API call to directory.chromeosdevices.update failed with error: Invalid Input: Inconsistent Orgunit id and path in request - 11006550017573025, /1 SCHOOLS/COVID LOANERS
What is strange is that if I comment or remove "admin.orgUnitPath = location(device_ID).toString().toUpperCase()" the script will run fine. It seems the orgUnitPath is causing this error.
I tried the following:
Removing the first slash "/"
Do only 1 Chromebook
Remove and reapply the AdminDirectory
Run previous scripts that were used to change the OrgUnitPath
Added quotes on the beginning and end of the OrgUnitPath
Converted the path to String with toString()
used Stript() function to eliminate any empty spaces
All the above attempts failed to fix this issue. I will also include an image of an error I am getting from a previous script I made that used to work about a year ago that also changes the OrgUnitPath.
Does anyone know how to fix this issue?
Thanks in advance.
For some reason there has been a change that now requires the orgUnitId in addition to the orgUnitPath.
So before you run AdminDirectory.Chromeosdevices.update you need to obtain the orgUnitId and update that property
var orgUnitPathStr = "/Tech Dept/Storage"
admin.orgUnitId = AdminDirectory.Orgunits.get("my_customer",orgUnitPathStr.substring(1)).orgUnitId;
//substring(1) above removes the first slash in the orgUnitPath, which is required for this method
Credit for hints:
https://github.com/taers232c/GAMADV-XTD3/issues/225

My javascript code can't recognise my array element defined in Python

So I'm trying to open a new window by executing a script in Selenium using driver.execute_script("window.open('');")
But I want to open a link given by the user.
So I got the link input from my array and put it to my javascript code just like this:
driver.execute_script("window.open(data[0]);")
Now It's giving an error like this:
selenium.common.exceptions.JavascriptException: Message: javascript error: data is not defined
How to fix this? Thanks for your time.
EDIT: A part of my code is something like that:
from selenium import webdriver
import PySimpleGUI as sg
import time
global data
data = []
layouts = [[[sg.Text("Enter the Wordpress New Post link: "), sg.InputText(key=0)]],
[sg.Button('Start The Process'), [sg.Button('Exit')]]]
window = sg.Window("Title", layouts)
def selenium_process():
# Getting the driver path
driver = webdriver.Chrome(r'Driver\path')
driver.get('https://google.com')
driver.execute_script(f"window.open({data[0]});")
time.sleep(10000)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
data.append(values[0])
selenium_process()
did you try string interpolation ?
Try this:
driver.execute_script(f"window.open({data[0]});")
Your solution does not work since data[0] is a string, not a variable. You instead need to substitute data[0] with its value (must be a value that JS can understand).
Please read the description of Javascript window.open : https://developer.mozilla.org/fr/docs/Web/API/Window/open
If you just need to get to an URL:
driver.get(data[0])

Error whilst trying to use the .difference() function in Python Jupyter

Context
I am currently going through a course on webscraping. Upon getting to the module on scraping javascript, a function set_1.difference(set_2) was used to distinguish the old variables from the newly created variables. But when I did it, it brought up this error:
AttributeError: 'list' object has no attribute 'difference'
I searched online and stumbled on this website. But running the example on their own website brought up an error
Problem
Any reason why this is not working? I want to print the newly generated javascript links. Below is the code I am trying to run:
from requests_html import AsyncHTMLSession
session = AsyncHTMLSession()
r = await session.get('https://www.ons.gov.uk/economy/economicoutputandproductivity/output/datasets/economicactivityfasterindicatorsuk')
r.status_code
divs = r.html.find('div')
downloads = r.html.find('a')
urls = r.html.absolute_links
# Now need to render the javascript. Downloads chromium the first time we use it,
# It is a browser that has no GUI
await r.html.arender()
new_divs = r.html.find('div')
new_downloads = r.html.find('a')
new_urls = r.html.absolute_links
# Get only the newly created html
new_downloads.difference(downloads)
Don't know what the "r" object is, so can't verify your code but difference is a method of sets, not lists.
https://docs.python.org/3/library/stdtypes.html#frozenset.difference
This should do the trick: set(new_downloads).difference(downloads)

Node JS - Express.js get query with multiple parameters

I'm quite new to JavaScript and Node JS and I have a such a situation. When I try to call get of express.js with a single parameter everything works fine, but when I try to call get with more than one parameter, it trims the query.
For example I have such call and function
app.get('path/data', myFunc);
// in another file
function myFunc(req, res) {
// do some stuff
}
When the url is path/data?id=5 or path/data?name=foo everything is fine. But when I use for example url like path/data?id=5&name=foo in myFunc I get url as path/data?id=5. So I get url's first part - what is before & sign.
Now what am I doing wrong? Is there something that I'm missing? How can I get whole url in myFunc without being trimmed?
Use
app.get('path/data?:id?:name')
And for retrieving the values, use req.query.id and req.query.name.
For accessing the REST api, you need to hit:
http://localhost:8080/demo?id=3&name=stack
So, by this you can add multiple parameters in your api.
Hope this helps.
I found the problem. I was requesting via curl and it turns out that shell command trims in case of there is an & in the url. So there is a need no add quotes like this
curl "path/data?id=5&name=foo"

Scraping with Nokogiri and Ruby before and after JavaScript changes the value

I have a program that scrapes value from https://web.apps.markit.com/WMXAXLP?YYY2220_zJkhPN/sWPxwhzYw8K4DcqW07HfIQykbYMaXf8fTzWT6WKnuivTcM0W584u1QRwj
My current code is:
doc = Nokogiri::HTML(open(source_url))
puts doc.css('span.indexDate').text
date = doc.css('span.indexDate').text
date = Date.parse(date)
puts date
values = doc.css('table#CdsIndexTable td.col2 span')
puts values
This scrapes the date and values of the second column from the "CDS Indexes" table correctly which is fine. Now, I want to scrape the similar values from the "Bond Indexes" table where I am facing the problem.
I can see a JavaScript function changes it without loading the page and without changing the URL of the page. The difference between these two tables is their IDs are different which is exactly that it should be. But, unfortunately when I try with:
values = doc.css('table#BondIndexTable')
puts values
I get nothing from the Bond Indexes table. But I get values from CDS Indexes table if I use:
values = doc.css('table#CdsIndexTable')
puts values
How can I get the values from both tables?
You can use Capybara with the Poltergeist driver to execute the Javascript and format the page. Poltergeist is a wrapper for the PhantomJS headless browser. Here's an example of how you can do it:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
Capybara.default_driver = :poltergeist
Capybara.run_server = false
module GetPrice
class WebScraper
include Capybara::DSL
def get_page_data(url)
visit(url)
doc = Nokogiri::HTML(page.html)
doc.css('td.col2 span')
end
end
end
scraper = GetPrice::WebScraper.new
puts scraper.get_page_data('https://web.apps.markit.com/WMXAXLP?YYY2220_zJkhPN/sWPxwhzYw8K4DcqW07HfIQykbYMaXf8fTzWT6WKnuivTcM0W584u1QRwj').map(&:text).inspect
Visit here for a complete example using Amazon.com:
https://github.com/wakproductions/amazon_get_price/blob/master/getprice.rb
If you don't want to use PhantomJS you can also use the network sniffer on Firefox or Chrome development tools, and you will see that the HTML table data is returned with a javascript POST request to the server.
Then instead of opening the original page URL with Nokogiri, you'd instead run this POST from your Ruby script and parse and interpret that data instead. It looks like it's just JSON data with HTML embedded into it. You could extract the HTML and feed that to Nokogiri.
It requires a bit of extra detective work, but I've used this method many times with JavaScript web pages and scraping. It works OK for most simple tasks, but it requires a bit of digging into the inner workings of the page and network traffic.
Here's an example of the JSON data from the Javascript POST request:
Bonds:
https://web.apps.markit.com/AppsApi/GetIndexData?indexOrBond=bond&ClientCode=WSJ
CDS:
https://web.apps.markit.com/AppsApi/GetIndexData?indexOrBond=cds&ClientCode=WSJ
Here's the quick and dirty solution just so you get an idea. This will grab the cookie from the initial page and use it in the request to get the JSON data, then parse the JSON data and feed the extracted HTML to Nokogiri:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'json'
# Open the initial page to grab the cookie from it
p1 = open('https://web.apps.markit.com/WMXAXLP?YYY2220_zJkhPN/sWPxwhzYw8K4DcqW07HfIQykbYMaXf8fTzWT6WKnuivTcM0W584u1QRwj')
# Save the cookie
cookie = p1.meta['set-cookie'].split('; ',2)[0]
# Open the JSON data page using our cookie we just obtained
p2 = open('https://web.apps.markit.com/AppsApi/GetIndexData?indexOrBond=bond&ClientCode=WSJ',
'Cookie' => cookie)
# Get the raw JSON
json = p2.read
# Parse it
data = JSON.parse(json)
# Feed the html portion to Nokogiri
doc = Nokogiri.parse(data['html'])
# Extract the values
values = doc.css('td.col2 span')
puts values.map(&:text).inspect
=> ["0.02%", "0.02%", "n.a.", "-0.03%", "0.02%", "0.04%",
"0.01%", "0.02%", "0.08%", "-0.01%", "0.03%", "0.01%", "0.05%", "0.04%"]
PhantomJS is a headless browser with a JavaScript API. Since you need to run the scripts on the page you are scraping, a browser will do that for you; and PhantomJS will allow you to manipulate and scrape the page after the script execution.

Categories