2009-01-28 28 views
4

我嘗試這樣做:如何將stdout和stderr輸出從Perl腳本重定向到Windows上的文件?

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. 

運行該腳本文件,只要我不嘗試在命令行輸出重定向以任何方式。

這是我的腳本,以防萬一。 (這是一個Selenium測試腳本):

#!C:/perl/bin/perl.exe -w 
use strict; 
use warnings; 
use Time::HiRes qw(sleep); 
use Test::WWW::Selenium; 
use Test::More "no_plan"; 
use Test::Exception; 

my $sel = Test::WWW::Selenium->new(host => "localhost", 
            port => 4444, 
            browser => "*chrome", 
            browser_url => "http://localhost/"); 
print "Start Time: " . localtime() . "\n"; 
for (my $count = 3000; $count > 0; $count--) 
{ 
    print $count . " tests remaining.\n"; 
    $sel->open_ok("/home"); 
    $sel->click_ok("link=News"); 
    $sel->wait_for_page_to_load_ok("30000"); 
    $sel->click_ok("video"); 
    $sel->wait_for_page_to_load_ok("30000"); 
    $sel->click_ok("link=Sports"); 
    $sel->wait_for_page_to_load_ok("30000"); 
    $sel->click_ok("link=Movies"); 
    $sel->wait_for_page_to_load_ok("30000"); 
    $sel->click_ok("moremovies"); 
    $sel->wait_for_page_to_load_ok("30000"); 
} 

print "End Time: " . localtime() . "\n"; 
+0

這看起來像一個硒對我來說特定的問題。您是否嘗試過在沒有任何對Selenium的調用的情況下運行腳本? – 2009-01-28 22:44:43

回答

10

有與Perl重定向Windows普遍問題。

,在Test::More包失敗的行說:

open TESTOUT, ">&STDOUT" or die $!; 

此當你調用命令test.pl > outlog.log,爲您重定向至STDOUTcmd.exe鎖定,而不是由perl.exe文件失敗。你不能dup()perl.exe

你需要運行:

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

代替。

+1

一般來說,你總是需要運行`perl foo.pl`而不是`foo.pl`才能使重定向正常工作 - 這不是特定於這種情況。 Windows blargh。 – ephemient 2009-01-29 16:12:26

0

在我所有的測試腳本中,我總是配置測試報告和日誌記錄選項(而不是使用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腳本重定向輸出和錯誤輸出到Windows上的文件。

相關問題