问题
I got an error trying to execute a test to upload a file with protractor, my code is this one
it('it should be possible to upload a file', function() {
browser.get(buildUrl(''));
popupEnvironment();
element(by.id("menu-transmission")).click();
element(by.id("menu-child-send-datafiles")).click();
expect(element(by.id("drop-zone")).isPresent()).toBeTruthy('the drag and drop zone should be present');
expect(element(by.id("drop-zone")).isDisplayed()).toBeTruthy('the drag and drop zone should be present');
expect(element(by.xpath('//*[@id="drop-zone"]/p[3]/label')).isPresent()).toBeTruthy('the Select File button should be present');
expect(element(by.xpath('//*[@id="drop-zone"]/p[3]/label')).isDisplayed()).toBeTruthy('the Select File button should be displayed');
var path = require('path');
var fileToUpload = "./resources/1.txt";
var absolutePath = path.resolve(__dirname, fileToUpload);
//element(by.id('file-upload')).sendKeys(absolutePath);
browser.executeScript("document.getElementById('file-upload').sendKeys(" + absolutePath + ")");
browser.sleep(20000);
});
I try to do it with element(by.id('file-upload')).sendKeys(absolutePath); but it doesn't work, then later I try using javascript raw code but it doesn't work either
my error message is this one, using the command "ng e2e"
× it should be possible to upload a file
- Failed: script timeout: result was not received in 11 seconds
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17763 x86_64)
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17763 x86_64)
at Object.checkLegacyResponse (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\error.js:546:15)
at parseHttpResponse (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\http.js:509:13)
at doSend.then.response (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Protractor.waitForAngular() - Locator: By(css selector, *[id="drop-zone"])
at Driver.schedule (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\webdriver.js:807:17)
at ProtractorBrowser.executeAsyncScript_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\protractor\built\browser.js:425:28)
at angularAppRoot.then (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\protractor\built\browser.js:456:33)
at ManagedPromise.invokeCallback_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:1376:14)
at TaskQueue.execute_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:2927:27)
at C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:668:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Run it("it should be possible to upload a file") in control flow
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
From asynchronous test:
Error
at Object.transmissionSendDatafile (C:\gitlab\edamis\EDAMIS-portal\src\main\front\e2e\transmissions-send-datafile.ts:26:5)
at Suite.<anonymous> (C:\gitlab\edamis\EDAMIS-portal\src\main\front\e2e\all-access.e2e-spec.ts:37:5)
at Object.<anonymous> (C:\gitlab\edamis\EDAMIS-portal\src\main\front\e2e\all-access.e2e-spec.ts:17:1)
at Module._compile (module.js:643:30)
at Module.m._compile (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\ts-node\src\index.ts:422:23)
at Module._extensions..js (module.js:654:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\ts-node\src\index.ts:425:12)
at Module.load (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\coffeescript\lib\coffee-script\register.js:45:36)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Array.forEach (<anonymous>)
at Function.promise (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:682:9)
at _fulfilled (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:834:54)
at self.promiseDispatch.done (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:863:30)
at Promise.promise.promiseDispatch (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:796:13)
at C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:556:49
at runSingle (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:137:13)
at flush (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:125:13)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
× Test that user is allowed to access secure pages
- Failed: script timeout: result was not received in 11 seconds
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17763 x86_64)
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17763 x86_64)
at Object.checkLegacyResponse (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\error.js:546:15)
at parseHttpResponse (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\http.js:509:13)
at doSend.then.response (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Protractor.waitForAngular() - Locator: By(xpath, //*[@id='environment-info-modal']/div/div/div[3]/button)
at Driver.schedule (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\webdriver.js:807:17)
at ProtractorBrowser.executeAsyncScript_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\protractor\built\browser.js:425:28)
at angularAppRoot.then (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\protractor\built\browser.js:456:33)
at ManagedPromise.invokeCallback_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:1376:14)
at TaskQueue.execute_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:2927:27)
at C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\selenium-webdriver\lib\promise.js:668:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: Run afterAll in control flow
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
From asynchronous test:
Error
at Suite.<anonymous> (C:\gitlab\edamis\EDAMIS-portal\src\main\front\e2e\all-access.e2e-spec.ts:39:5)
at Object.<anonymous> (C:\gitlab\edamis\EDAMIS-portal\src\main\front\e2e\all-access.e2e-spec.ts:17:1)
at Module._compile (module.js:643:30)
at Module.m._compile (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\ts-node\src\index.ts:422:23)
at Module._extensions..js (module.js:654:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\ts-node\src\index.ts:425:12)
at Module.load (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\coffeescript\lib\coffee-script\register.js:45:36)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Array.forEach (<anonymous>)
at Function.promise (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:682:9)
at _fulfilled (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:834:54)
at self.promiseDispatch.done (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:863:30)
at Promise.promise.promiseDispatch (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:796:13)
at C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:556:49
at runSingle (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:137:13)
at flush (C:\gitlab\edamis\EDAMIS-portal\src\main\front\node_modules\q\q.js:125:13)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
the HTML code is this one
<div _ngcontent-c6="" class="drag-n-drop-container" id="drop-zone" ng2filedrop="" ng-reflect-queue="[object Object]" xpath="1"><p _ngcontent-c6="">Drop files here</p><p _ngcontent-c6="">or</p><p _ngcontent-c6=""><label _ngcontent-c6="" class="btn btn-sm btn-default btn-file" for="file-upload"> Select files </label><input _ngcontent-c6="" id="file-upload" multiple="" ng2fileselect="" type="file" ng-reflect-queue="[object Object]" style=""></p></div>
operate system: Windows 10 my protractor version is 5.3.0 angular version is 7.1.3
as well I verify the the folder of the file, but is ok
do you have any idea, what could be the problem? Help me please!!!!
回答1:
I found the solution
var fileToUpload = './resources/1.txt',
absolutePath = path.resolve(__dirname, fileToUpload);
browser.sleep(5000);
browser.ignoreSynchronization = true;
element(by.id('file-upload')).sendKeys(absolutePath);
回答2:
You can try the following:
var path = require('path');
var fileToUpload = "./resources/1.txt", //notice its a comma, it should work with it.
var absolutePath = path.resolve(__dirname, fileToUpload);
$('input[type="file"]').sendKeys(absolutePath);
来源:https://stackoverflow.com/questions/54712899/fail-loading-file-protractor-failed-script-timeout-result-was-not-received-i