如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件?

如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件?,perl,testing,selenium,Perl,Testing,Selenium,我试过这个: test1.pl >output.log 2>&1 但结果是: Can't dup STDOUT: Permission denied at C:/Perl/lib/Test/Builder.pm line 1376. Compilation failed in require at C:/Perl/lib/Test/Builder/Module.pm line 3. BEGIN failed--compilation aborted at C:/Perl/

我试过这个:

test1.pl >output.log 2>&1
但结果是:

Can't dup STDOUT:  Permission denied at C:/Perl/lib/Test/Builder.pm line 1376.
Compilation failed in require at C:/Perl/lib/Test/Builder/Module.pm line 3.
BEGIN failed--compilation aborted at C:/Perl/lib/Test/Builder/Module.pm line 3.
Compilation failed in require at C:/Perl/lib/Test/More.pm line 22.
BEGIN failed--compilation aborted at C:/Perl/lib/Test/More.pm line 22.
Compilation failed in require at C:/Perl/site/lib/Test/WWW/Selenium.pm line 72.
BEGIN failed--compilation aborted at C:/Perl/site/lib/Test/WWW/Selenium.pm line 72.
Compilation failed in require at C:\Software\selenium-remote-control-1.0-beta-2\tests\test1.pl line 5.
BEGIN failed--compilation aborted at C:\Software\selenium-remote-control-1.0-beta-2\tests\test1.pl line 5.
只要我不试图以任何方式重定向命令行的输出,脚本就会运行该文件

这是我的剧本,以防万一。(这是一个测试脚本):


对于
Windows
,在
Perl
中重定向存在一般性问题

Test::More
包中失败的行表示:

open TESTOUT, ">&STDOUT" or die $!;
当您以
test.pl>outlog.log
的形式调用命令时,此操作失败,因为您重定向到的
STDOUT
文件被
cmd.exe
锁定,而不是被
perl.exe
锁定。您不能从
perl.exe

您需要运行:

perl test1.pl >output.log 2>&1

相反。

在我所有的测试脚本中,我总是配置测试报告和日志选项(而不是使用stdout)。我还遇到了重定向输出的问题。您可以使用我上面列出的解决方案,也可以像我所做的那样:

my $res_file = "C:\\Automation\\Results\\Test_Logs\\login_test_output.txt"; 
my $err_file = "C:\\Automation\\Results\\Test_Logs\\login_error_output.txt";

open FH, ">$res_file" or die "couldn't open file: $!";

FH->autoflush(1); # Make FileHandle HOT. Set to 0 to turn autoflush off

Test::More->builder->output (*FH{IO}); # Redirect to test result file ($res_file)
Test::More->builder->failure_output ($err_file); # and test failure output file

使用这种方法,我可以将perl脚本中的stdout和stderr输出重定向到Windows上的文件。

这对我来说似乎是一个特定于Selenium的问题。您是否尝试过在不调用Selenium的情况下运行脚本?一般来说,您将始终需要运行
perl foo.pl
,而不仅仅是
foo.pl
,重定向才能正常工作——这不是本例所特有的。窗户嗡嗡作响。
my $res_file = "C:\\Automation\\Results\\Test_Logs\\login_test_output.txt"; 
my $err_file = "C:\\Automation\\Results\\Test_Logs\\login_error_output.txt";

open FH, ">$res_file" or die "couldn't open file: $!";

FH->autoflush(1); # Make FileHandle HOT. Set to 0 to turn autoflush off

Test::More->builder->output (*FH{IO}); # Redirect to test result file ($res_file)
Test::More->builder->failure_output ($err_file); # and test failure output file