Cucumber and Protractor with Javascript - Write in

2019-08-18 05:22发布

问题:

I'm using Cucumber and Protractor with Node.js to test my web application. I use javascript to write code with chai expectation. I need to write some results in a separate text file. How can I do it? I need the result in the text file because, I analize the result in the excel. The code is that:

When('I insert the related username and password to access it', {timeout: 90*1000}, function (next) {
    let username_field = element(by.css('.UsernameInput'));
    browser.wait(until.presenceOf(username_field), 1200000, 'Element taking too long to appear in the DOM');
    let username_password = element(by.css('.PasswordInput'));
    browser.wait(until.presenceOf(username_password), 1200000, 'Element taking too long to appear in the DOM');
    const fs = require('fs');   
    const userFile = fs.readFileSync('parametri.txt', {encoding: 'utf8'});  
    const users = userFile.split('\n');
    const randomUser = () => {
        const number = Math.floor(Math.random() * Math.floor(users.length - 1));
        const user = users[number].split(',');
        return {username: user[0], password: user[1]}
    }
    const user = randomUser();  
    username_field.sendKeys(user.username);
    username_password.sendKeys(user.password);
    username_field.getText().then(function(text){
        var n=new Date();
        var time= n.getTime();
        browser.sleep(d);
        console.log(time);
        console.log('1°When');
        function WriteFile(){   
            const feat1_stepUno = require('feat1_stepUno');
            let a= '5000';
            const file_time = feat1_stepUno.writeFile('tempo.txt',a,(err) => {
            if (err) throw err; 
            })              

        }
expect(username_field.isPresent()).to.eventually.be.true.and.notify(next);          
    })
});

Thank you so much for your help!

回答1:

This can solve your issue:

const fs = require('fs');
fs.writeFile("tempo.txt", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

Bye!