How do I access the contents of the clipboard from within a headless puppeteer test? - clipboard

I'm writing a test that uses puppeteer to test a component that copies something to the clipboard when it is interacted with. I want to test that the contents of the clipboard are correct after interacting. Other resources like this github issue mention using a tool like clipboardy to accomplish this. I tried using that and it works locally, but when run in my headless CI server, it complains about not having access to the X environment. Is there a way to access the clipboard without starting an X server?
I'm writing a test like this:
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://mypage.com');
await page.click('#my-component');
// This fails in a headless environment
expect(clipboardy.readSync()).toEqual("Some text");

By adding the 'clipboard-read' permission to puppeteer and using the
Clipboard API, you can run navigator.clipboard.readText() to read from the
clipboard in a test. This will work even in a headless environment:
const browser = await puppeteer.launch();
const context = browser.defaultBrowserContext();
context.overridePermissions(/* browser origin */, ['clipboard-read'])
const page = await browser.newPage();
await page.goto('https://mypage.com');
await page.click('#my-component');
expect(await page.evaluate(() => navigator.clipboard.readText()))
.toEqual("Some text");
Documentation of context.overridePermissions()

In my case I couldn't override permissions, as Eduard suggested, because it requires to give origin as a parm. I'm injecting html content in my test to the page via setContent so the page address is about:blank. Setting origin to "*" doesn't work either, nor undefined.
I have ended up mocking the clipboard api:
await page.evaluate((dataInternal) => {
// mock clipboard
let clipboardText = null;
window["navigator"]["clipboard"] = {
writeText: text => new Promise(resolve => clipboardText = text),
readText: () => new Promise(resolve => resolve(clipboardText)),
}
}
then you can just do the following assert:
expect(await page.evaluate(() => navigator.clipboard.readText())).toBe("your text in clipboard");

In my chrome, navigator.clipboard = ... did not work, because navigator had getter and setter for clipboard.
Mocking clipboard:
await page.evaluate(() => {
const clipboard = {
async writeText(text: string) {
this.text = text;
},
};
Object.defineProperty(navigator, 'clipboard', { value: clipboard });
});
Reading clipboard content:
page.evaluate(() => navigator.clipboard.text);

Related

IndexedDB breaks in Firefox after trying to save autoIncremented Blob

I am trying to implement Blob storage via IndexedDB for long Media recordings.
My code works fine in Chrome and Edge (not tested in Safari yet) - but won't do anything in Firefox. There are no errors, it just doesn't try to fulfill my requests past the initial DB Connection (which is successful). Intuitively, it seems that the processing is blocked by something. But I don't have anything in my code which would be blocking.
Simplified version of the code (without heavy logging and excessive error checks which I have added trying to debug):
const dbName = 'recording'
const storeValue = 'blobs'
let connection = null
const handler = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB
function connect() {
return new Promise((resolve, reject) => {
const request = handler.open(dbName)
request.onupgradeneeded = (event) => {
const db = event.target.result
if (db.objectStoreNames.contains(storeValue)) {
db.deleteObjectStore(storeValue)
}
db.createObjectStore(storeValue, {
keyPath: 'id',
autoIncrement: true,
})
}
request.onerror = () => {
reject()
}
request.onsuccess = () => {
connection = request.result
connection.onerror = () => {
connection = null
}
connection.onclose = () => {
connection = null
}
resolve()
}
})
}
async function saveChunk(chunk) {
if (!connection) await connect()
return new Promise((resolve, reject) => {
const store = connection.transaction(
storeValue,
'readwrite'
).objectStore(storeValue)
const req = store.add(chunk)
req.onsuccess = () => {
console.warn('DONE!') // Fires in Chrome and Edge - not in Firefox
resolve(req.result)
}
req.onerror = () => {
reject()
}
req.transaction.oncomplete = () => {
console.warn('DONE!') // Fires in Chrome and Edge - not in Firefox
}
})
}
// ... on blob available
await saveChunk(blob)
What I tried so far:
close any other other browser windows, anything that could count as on "open connection" that might be blocking execution
refresh Firefox profile
let my colleague test the code on his own machine => same result
Additional information that might useful:
Running in Nuxt 2.15.8 dev environment (localhost:3000). Code is used in the component as a Mixin. The project is rather large and uses a bunch of different browser APIs. There might be some kind of collision ?! This is the only place where we use IndexedDB, though, so to get to the bottom of this without any errors being thrown seems almost impossible.
Edit:
When I create a brand new Database, there is a brief window in which Transactions complete fine, but after some time has passed/something triggered, it goes back to being queued indefinitely.
I found out this morning when I had this structure:
...
clearDatabase() {
// get the store
const req = store.clear()
req.transaction.oncomplete = () => console.log('all good!')
}
await this.connect()
await this.clearDatabase()
'All good' fired. But any subsequent requests were broken same as before.
On page reload, even the clearDatabase request was broken again.
Something breaks with ongoing usage.
Edit2:
It's clearly connected to saving a Blob instance without an id with the autoIncrement option. Not only does it fail silently, it basically completely corrupts the DB. If I manually assign an incrementing ID to a Blob object, it works! If I leave out the id field for a regular simple object, it also works! Anyone knows about this? I feel like saving blobs is a common use-case so this should have been found already?!
I've concluded, unless proven otherwise, that it's a Firefox bug and opened a ticket on Bugzilla.
This happens with Blobs but might also be true for other instances. If you find yourself in the same situation there is a workaround. Don't rely on autoIncrement and assign IDs manually before trying to save them to the DB.

add API key to url

Hi I'm build a wildfire app tracker with react using the nasa API it works in development by using the url directly witch is https://eonet.sci.gsfc.nasa.gov/api/v2.1/events
But when I deploy it. It does not get the data. I obviously need a api key witch I have, but how do I implement it in the url above ?
here is my code..
useEffect(() => {
const fetchEvents = async () => {
setLoading(true)
const res = await fetch('https://eonet.sci.gsfc.nasa.gov/api/v2.1/events')
const {events} = await res.json()
setEventData(events)
setLoading(false)
}
fetchEvents()
// eslint-disable-next-line
}, [])
You could try to create a .env file in which you can set URLS as
REACT_APP_PUBLIC_URL=https://eonet.sci.gsfc.nasa.gov/api/v2.1/events
and then in your app component import as
fetch(process.env.REACT_APP_PUBLIC_URL)

Using puppeteer to screenshot local files, but it's still making web requests?

I am using Puppeteer to take screenshots of a web page for my company. I need to test multiple people's accounts so that means visiting the page multiple times (150 times in this case). This results in our firewall kicking me out for making too many requests.
My solution is to just fetch the contents of the page and save them locally. Then I use puppeteer on that local file, overriding the function used to get data from our servers to instead just use data already loaded into Node from a CSV.
All of this works, but it looks like it's still making requests to our servers.
I tried giving it a userDataDir so it could cache any resources. In theory, if it's loading it from file://, it's caching the resources and there's no Ajax requests, it shouldn't be making any further requests, right?
I also tried installing a debugging proxy but since it's https I can't see what it's trying to request.
This is how I start it:
puppeteer.launch({
userDataDir: "temp/"
})
.then(browser => {
next(browser, links);
)
.catch(error => {
cb(error, null);
});
next will iterate through any links it needs to visit.
This part saves the page locally:
if (this._linkCache[baseLink] === undefined) {
fetch(baseLink)
.then(resp => resp.text())
.then(contents => {
fs.writeFile(fullFileName, contents, 'utf8', err => {
if (err) {
cb(err, null);
} else {
this._linkCache[baseLink] = fileUrl;
gotoPage(fileUrl);
}
});
})
.catch(error => {
cb(error, null);
});
}
// Go to the cached version
else {
gotoPage(this._linkCache[baseLink] + queryParams);
}
And this gets the screenshots:
const gotoPage = async(url) => {
try {
const page = await browser.newPage();
// Override 'fetchAccountData' function
await page.evaluateOnNewDocument(testData => {
window["fetchAccountData"] = (cb: (err: any, data: any)=>void) => {
cb(null, testData);
};
}, data);
// Go to page and get screenshot
await page.goto(url);
const screenie = `${outputPath}${uuid()}.png`;
await page.screenshot({ fullPage: true, path: screenie, type: "png" });
pageHtml.push(`<img src="file://${screenie}" />`);
next(browser, rest);
} catch (e) {
cb(e, null);
}
};
I was hoping this would be able to only make a few requests at the beginning while it saves the html locally and caches all the resources, but it seems to make a request for every link.
How can I stop it?

After updating nativescript folder and files are not creating in my android phone

I'm able to create a folder if it not exists and save a newly written file in that folder previously. but after updating to latest nativescript the same code was not working and not give error properly.
and also I'm getting an error
Error: android.util.AndroidRuntimeException: Calling startActivity() from >outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. >Is this really what you want?
const fileSystemModule = require("tns-core-modules/file-system");
const documents = fileSystemModule.knownFolders.documents();
documents._path = "/storage/emulated/0/";
const folder = documents.getFolder('Reports/sample/');
const file = folder.getFile('fileName.xlsx');
file.writeText(viewModel.get("fileTextContent") || html_content)
.then((result) => {
return file.readText()
.then((res) => {
var toast = Toast.makeText("Exported to Excel Succesfully");
toast.show();
return res;
});
}).then((result) => {
console.log("---result---");
console.log(result); // im getting result, a html string
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(file._path)), "application/vnd.ms-excel");
application.android.context.startActivity(android.content.Intent.createChooser(intent, "Open Excel..."));
}).catch((err) => {
console.log(err);
});
before updating it was working fine. but now I don't know what happened to this.
It's a new requirement from Android itself. You must add FLAG_ACTIVITY_NEW_TASK flag to your intent.
With Android 9, you cannot start an activity from a non-activity context unless you pass the intent flag FLAG_ACTIVITY_NEW_TASK. If you attempt to start an activity without passing this flag, the activity does not start, and the system prints a message to the log.
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

How to make make a google request return the results in a certain language?

I'm using Puppeteer to make the requests (it uses Chromium in the background):
;(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`https://www.google.com/search?tbm=bks&q=%22this+is%22`)
const result = await page.evaluate(() => {
const stats = document.querySelector('#resultStats')
return stats.textContent
})
await browser.close()
res.send(result)
})()
Since I live in Taiwan, the results are being returned in Chinese.
How can I modify the URL or puppeteer's config so I get results in English?
Just add &hl=en in url:
await page.goto(`https://www.google.com/search?tbm=bks&q=%22this+is%22&hl=en`)

Resources