我在硒测试中使用PageFactory。我在等待加载页面时遇到了问题。我不是在谈论页面上的元素我在谈论页面加载的超时。 所以,我有一个像下面这样的方法:
public MyProjectsPage ClickSaveAndCloseButton()
{
//Do something and click a button
SaveAndCloseButton.Click();
return new MyProjectsPage(Driver); //return new page
}
当我等待返回新的PageObject(在我的情况下是“MyProjectsPage”)时,我得到了一个超时异常。那么,我在哪里可以设置页面加载超时?
实际错误如下:
AutomatedTests.FrontEnd.SouvenirProduct.SouvenirTestExecution.OrderSouvenirWithAuthorization(ByCash,Pickup,True,Cup):
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7585/session/b68c04d1ead1fc78fe083e06cbece38f/element/0.46564483968541026-14/click timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
我有: 最新版本的WebDriver 以及最新版ChromeDriver和最新版Chrome浏览器 在下一行上面的错误超出了apears:
return new MyProjectsPage(Driver); //return new page
我通过下一种方式创建ChromeDriver:
public DriverCover(IWebDriver driver)
{
_driver = driver;
_driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
}
private readonly IWebDriver _driver;
答案 0 :(得分:1)
1注意考虑页面上的等待机制: 获取几个webElements并申请fluentWait()。这将是显式等待 webdriver方法。
另一种方法是尝试隐式等待,如:
int timeToWait=10;
driver.manage().timeouts().implicitlyWait(timeToWait, TimeUnit.SECONDS);
考虑你的pageObject代码: 我推荐你以下内容:
MyPage myPageInstance= PageFactory.initElements(driver, MyPage.class);
然后你写下面的方法:
public MyPage clickSaveAndOtherActions(MyPage testPageToClick)
{
testPageToClick.clickFirstButton();
testPageToClick.clickSecondButton();
testPageToClick.closePoPup();
return testPageToClick; //return page in a new state
}
如果您想继续工作(我的意思是更新您的页面状态),请执行以下操作:
myPageInstance = clickSaveAndOtherActions(myPageInstance );
希望这会对你有所帮助。感谢。
UPD:正如我从日志中看到的那样,remoteWebdriver服务器出现了问题:
OpenQA.Selenium.WebDriverException:对远程的HTTP请求 用于URL的WebDriver服务器 http://localhost:7585/session/b68c04d1ead1fc78fe083e06cbece38f/element/0.46564483968541026-14/click 60秒后超时。 ----> System.Net.WebException:The 操作已经超时
另外,我建议你仔细检查你的驱动程序方法init。我使用以下 java 代码作为驱动程序初始化(UI,chrome实例,selenium网格+中心节点测试架构):
public static WebDriver driverSetUp(WebDriver driver) throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.chrome();
log.info("Google chrome is selected");
//System.setProperty("webdriver.chrome.driver", System.getProperty("user.home")+"/Documents/Tanjarine/chromedriver");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
String webDriverURL = "http://" + environmentData.getHubIP() + ":" + environmentData.getHubPort() + "/wd/hub";
driver = new RemoteWebDriver(new URL(webDriverURL), capability);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
return driver;
}
答案 1 :(得分:1)
使用PageFactory模式时你应该做的是在初始化你的页面时你应该使用构造函数初始化元素。
public MyProjectsPage ClickSaveAndCloseButton()
{
//Do something and click a button
//I am guessing this is taking you to the MyProjectsPage
SaveAndCloseButton.Click();
return new MyProjectsPage(Driver); //return new page
}
public class MyProjectsPage
{
[FindsBy(How = How.Id, Using = "yourId")]
public IWebElement AWebElement { get; set; }
private IWebDriver WebDriver;
public MyProjectsPage (IWebDriver webDriver)
{
WebDriver = webDriver;
PageFactory.InitElements(WebDriver, this);
}
}
当您返回页面时,将初始化使用FindsBy
属性的所有元素。
更新
初始化时在驱动程序上设置此属性:
WebDriver.Manage().Timeouts().SetPageLoadTimeout(timespan)
答案 2 :(得分:0)
// Wait Until Object is Clickable
public static void WaitUntilClickable(IWebElement elementLocator, int timeout)
{
try
{
WebDriverWait waitForElement = new WebDriverWait(DriverUtil.driver, TimeSpan.FromSeconds(10));
waitForElement.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
// Wait For Page To Load
public static void WaitForPage()
{
new WebDriverWait(DriverUtil.driver, MyDefaultTimeout).Until(
d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
}