I put the tested application URL in a text file so I would like Protractor execution to wait without using “Sleep()” method until it finishes reading so Protactor can launch the browser to the website using the text from the file. But so far, Protractor executes so fast so it fails to get the text from the file so it cannot open the website on Chrome
function retrieveAppURL(){ const fs = require('fs') var data = null; fs.readFile("appURL.txt", (err, text) => { if (err){ console.log("error " + err); } else { console.log("text file data = " + text.toString()); data = text.toString(); } }); return data; } function launchAppURL(){ var data = retrieveAppURL(); browser.get(data ); browser.waitForAngularEnabled(false); } catch (err) { console.log("exception " + err.message); } }
Advertisement
Answer
I have made a solution for you, it gets an URL from a file and opens the link in the browser. My whole solution for you is below.
url-spec.js
describe('Open an URL from a text file', function() { async function retrieveAppURL(){ const fs = require('fs') return browser.wait(async function () { return new Promise((resolve, reject) => { fs.readFile("appURL.txt", (err, text) => { if (err){ console.log("error " + err); } else { console.log("text file data = " + text.toString()); resolve(text.toString()) } }); }) }, 10000, "File has not been read within 10 seconds") } async function launchAppURL(){ try{ const data = await retrieveAppURL(); await browser.get(data); // sleep for 5 sec to see the result await browser.sleep(5000); } catch (err) { console.log("exception " + err.message); } } it('should add a todo', function() { launchAppURL(); }); });
conf.js
exports.config = { seleniumAddress: 'http://localhost:4444/wd/hub', specs: ['url-spec.js'] };
appURL.txt
https://google.com
Run it by protractor conf.js
Please, do not forget to install fs and protractor for the project.
It works really fast. I just run protractor conf.js
and it opens the URL in a browser for me.