首页 文章

无法在导航网址上捕获Selenium WebDriverException

提问于
浏览
1

我在C#中使用Selenium,我写的最新测试有点问题 .

我创建了一个新的FirefoxDriver,它导航到我的URL,它会在进一步导航之前立即创建一个用于身份验证的弹出窗口 . 这里的问题是,driver.navigate() . gotourl(“”)确实等待加载URL,它不会 - 也不应该 - 这样做 . 当我用try catch块包围此语句来捕获WebDriverException时,它只是忽略它并结束我的测试:

Test Name:  SetUserPw
Test FullName:  XUnit_DataManager_Tests.DataManagerGui.PasswordTest.SetUserPw
Test Source:    C:\Programme\IMaT\TestPlugins\Plugins for XUnit\XUnit_DataManager_Tests\DataManagerGui\PasswordTest.cs : line 393
Test Outcome:   Failed
Test Duration:  0:00:00,001

Result StackTrace:  
bei OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
   bei OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   bei OpenQA.Selenium.Firefox.FirefoxDriverCommandExecutor.Execute(Command commandToExecute)
   bei OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   bei OpenQA.Selenium.Remote.RemoteTargetLocator.Alert()
   bei WebsiteAutomation.Helpers.Helper.c.b__4_0(IWebDriver drv)
   bei OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   bei WebsiteAutomation.Helpers.Helper.WaitForAlert(UInt32 timeout)
   bei WebsiteAutomation.Helpers.Helper.WaitForBasicAuth(String user, String password, Boolean accept, UInt32 timeout)
   bei XUnit_DataManager_Tests.DataManagerGui.PasswordTest.OpenPasswordSite(String password) in C:\Programme\IMaT\TestPlugins\Plugins for XUnit\XUnit_DataManager_Tests\DataManagerGui\PasswordTest.cs:Zeile 430.
   bei XUnit_DataManager_Tests.DataManagerGui.PasswordTest..ctor(InverterFixture fixtureData) in C:\Programme\IMaT\TestPlugins\Plugins for XUnit\XUnit_DataManager_Tests\DataManagerGui\PasswordTest.cs:Zeile 36.
----- Inner Stack Trace -----
   bei System.Net.HttpWebRequest.GetResponse()
   bei OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
Result Message: 
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7055/hub/session/4b47ded2-6ba6-4052-8785-e8ba346092da/alert_text timed out after 60 seconds.
---- System.Net.WebException : Die Anfrage wurde abgebrochen: Timeout für Vorgang überschritten.

有没有办法导航到URL,但对身份验证弹出窗口做出反应?

1 回答

  • 2

    要在页面加载时处理警报:

    var driver = new FirefoxDriver();
    
    // set the timeout to 0 and catch it
    driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(0));
    try{
        driver.Navigate().GoToUrl("...");
    }catch(WebDriverTimeoutException){
        driver.SwitchTo().Alert().Dismiss();
    }
    // restore the timeout
    driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(60));
    

相关问题