I am trying to start a download of a file with the playwright. right now I have all the info to get to the website and the option to export to the bottom export to excel that downloads an excel but I am having problems getting it to work. here is what I have.
//#cmdExport is the locator of the button
await page.locator('#cmdExport').click();
// trying to get over the dialog popup to start the dowload
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.accept();
await page.locator('#cmdExport').click();
});
//don't know what's next
this is the flow:
right now with what I read so far the code above the image is all I understand, I don't understand the official documentation of the playwright on the alerts and downloads part.
if someone could help me understand better how is this resolved it would be welcome. I have been all day reading posts and watching videos to know how is this flow made by the playwright but I haven't had any luck understanding this problem.
const [download] = await Promise.all([
page.waitForEvent('download'),
page.on('dialog', dialog => dialog.accept()),
await page.locator('#cmdExport').click(),
]);
const path = download.suggestedFilename();
await download.saveAs(path);
so the way this was solver was by making a const with the Download like it was said in the official documentation of playwright. then inside the promise. all do the wait for event download. then do the pago on for the dialog.
lastly and the most illogical part for me at least. write the line of code of the button that will be pressed to activate all this flow of events.
then create the path save the file with the name that it was supposed to have with the suggested filename function and save it on the path (that is the main root of your project folder. I was trying to save it on another folder inside the de project, but it threw me some errors so that's all.
Related
I'm new on running test with Selenium webdriver and Mocha JS. I did a few things already but I'm stuck on an issue since a few days, and after reading some issues that looked similar on SO, I didn't find my answer.
So, I'm running tests and at some point, on of my clicks causes a page reload. Just after this reload, I'm trying to fill some form inputs, but I have errors because the browser is unable to locate the element I'm searching for.
Here is my script :
it("Test", async function() {
await driver.get("https://www.myURL.com/")
await driver.findElement(By.id("button")).click()
// Here is the reloading
await driver.findElement(By.name("login")).sendKeys("mylogin") // => not working because not loaded
})
I don't know if I have to put a timeout and how to place it or if I need something else.
For information, I'm running my tests on local first with Visual Studio Code (+ extensions).
Thanks everyone for help.
Greg
I'm new to JavaScript, I'm trying to make a bot that interacts with a web page and automates some steps such as clicking some buttons. I wanted to use for example document.getElementById('button').Click but the command should refer to a specific url of a page. If you can, do some code examples.
To run this command you can create some code using selenium or puppeteers.
This enables a webpage to run on backend end, then to do some stuff like cliking button if you want. It's pretty hard to define how you want to do that, but this is en example of puppeteers in javascript (Node JS)
const puppeteer = require("puppeteer")
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://myurl');
await page.evaluate(() => {
//do something on the page
});
})();
don't forget to install pupeteers with npm install and to add some sleep functions if the page takes too long to render (the page can be available but the js of the page didn't finish his work so you need to wait)
i am making a PWA for my college portal, so i need an API for HTTPS call, but unfortunately i have no API for that website which i am making for to use the concepts like FETCH API in js or AXIOS. I would rather go with XMLHTTPS request but, its an old one and very complex too. So, i though of using selenium with javascript. and i did it successfully, just installed npm package and i have installed some webdrivers for firefox.and everything is fine, but the whole selenium webdriver is working with a node command.
node file_name
node file_name.js
any syntax is accepted .
const {Builder, By, Key, util}=require("selenium-webdriver");
async function example(){
let driver = await new Builder().forBrowser("firefox").build();
await driver.get("https://login.gitam.edu/Login.aspx"); //this is the link
await driver.findElement(By.name("txtusername")).sendKeys("0116", Key.INSERT);
await driver.findElement(By.name("password")).sendKeys("password", Key.RETURN);
}
example();
now, the above code is operated by node file_name
but i need this from my UI , Which i am making for my PWA. so, i design button, when user click that, it is going to initiate the above selenium javascript snippet.So i need a code or any resource for making that button to initiate the terminal command and that in return makes selenium javascript code to run.
please answer my question.
ps: this is my first question on stackoverflow !!!!
thank you very much.
Is it possible to manually navigate to a Page, then launch a Puppeteer Script there, navigate to a different Page, launch script there, and so on..
I already did a bit of research but couldn't find anything.
I need to autofill a Calendar but its a little bit difficult to automate the whole Process, so it would be nice if I could navigate manually and launch the script when needed
Does anyone know if this is possible??
You could code an interactive console app, Like the one explained here.
On that app, you would launch a browser with headless in false, navigate where you want to go, and then from the console app you could type a command like fillform and execute the puppeteer code you want to run.
Not sure why someone down voted?
Yeah it's possible. It's not recommended. It's better to work your way through the errors and then understand how page automation really works. This is the point of Puppeteer. It's also already possible to run JavaScript on a page in chrome, using the console in dev-tools.
But if you wanted to manually navigate to a page using puppeteer, then run 'macros' on the page using node.js based on a condition, you'd want to do something like this:
headless: false launch (obviously so you can see the browser)
have your script/fill in function wait for an event on the page like a request which indicates the page was refreshed. You might be able to
use page.on() event to trigger the code wait for the
request to finish.
await page.setRequestInterception(true);
page.on('request', request => {
// Override headers
const headers = Object.assign({}, request.headers(), {
foo: 'bar', // set "foo" header
origin: undefined, // remove "origin" header
});
request.continue({headers});
});
from puppeteer
I am trying to dynamically set the Chrome download path in conf.js for Protractor. Our webservice exports a file and I am writing a test that needs to know where the download file is going to be in order to verify it. Right now, I am hard setting the browser's download path. The problem is that other members of my team and the build machines will also be running this test and there is no single download path I could choose that would work on every dev and build machine. After doing some reading, I thought the best solution would be get the user data directory from the getCapabilities() function inside the onPrepare function, and then set that as the download directory, like so:
onPrepare: function () {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter( {
savePath: 'reports', consolidateAll: true } ));
var cap = browser.getCapabilities();
// Set prefsDict using cap.caps_.userDataDir
},
capabilities: {
'chromeOptions': {
'prefs': prefsDict
}
}
This would allow the code to be dynamic, but getCapabilities returns a promise, so the code above is not going to work because conf.js will finish constructing the config object before the promise resolves. Using a then function on getCapabilities doesn't help because I can't construct the capabilities section of my config object in the then function. I can't call getCapabilities outside of the onPrepare function because conf.js itself doesn't have context for it. Setting a network path is also not feasible for the team's setup.
Has anyone else tackled something like this? Is there any other way to programmatically set the download path for Chrome?
This probably doesn't solve the issue in the way you'd like, but you could run
protractor conf.js --capabilities.chromeOptions.prefs 'path/to/user/folder'
if the user is savvy enough to know, find, or store that information.
Edit:
You might be able to use .execute() to force the browser.getCapabilities(); to fulfill before moving to the next statement.
Normally called by protractor.promise.controlFlow().execute( myPromise )
However, I think execute() also returns a promise, so you might be back to square one with this idea.