1
長話短說:我正在研究一個具有處理對象的通用等待方法的框架。通常每頁都需要在{Area}Page
類中具有Wait
函數,並且需要找到最佳的一組對象,這些對象將自信地判斷是否加載了所有頁面對象。所有這些都取決於管理如何在頁面上加載對象的努力,即頁面底部的項目不一定是要加載的最後一個對象。在框架本身,我想改變像WaitById
這樣的功能簡單地WaitTillDisplayed
或類似的東西,並使用注入識別對象,即功能不應該確定如何識別對象(我認爲我應該使用依賴注入)。我不知道如何完成。有沒有人有任何想法如何做到這一點?謝謝。Selenium webdriver依賴注入的等待方法
public static void WaitForPageLoad(string browserTitle)
{
Containers.LogStep logStep = new Containers.LogStep();
try
{
string checkString = "";
DefaultWait<string> wait = new DefaultWait<string>(checkString);
wait.Timeout = TimeSpan.FromSeconds(10); // .FromMinutes(1);
wait.PollingInterval = TimeSpan.FromMilliseconds(250);
Func<string, bool> waiter = new Func<string, bool>((string ele)
=>
{
String findTitle = Browser.Title;
if (findTitle.Contains(browserTitle))
{
return true;
}
return false;
});
wait.Until(waiter);
logStep.Source = "WaitForPageLoad";
logStep.ElementName = browserTitle;
logStep.Action = "Wait Page";
logStep.Data = "";
logStep.Friendly = String.Concat("Wait for Page with title \"",
browserTitle, "\" to load");
logStep.Screenshot = Capture();
logStep.Result = Reporting.Pass;
Reporting.LogStep(logStep);
}
catch (Exception e)
{
logStep.Friendly = e.Message.ToString();
logStep.Result = Reporting.Fail;
Reporting.LogStep(logStep);
}
}
public static void WaitForDialogLoad(IWebElement dialog, string
dialogTitle)
{
Containers.LogStep logStep = new Containers.LogStep();
try
{
string checkString = "";
DefaultWait<string> wait = new DefaultWait<string>(checkString);
wait.Timeout = TimeSpan.FromSeconds(10); // .FromMinutes(1);
wait.PollingInterval = TimeSpan.FromMilliseconds(250);
Func<string, bool> waiter = new Func<string, bool>((string ele)
=>
{
String findTitle = dialog.Text.ToString();
if (findTitle.Contains(dialogTitle))
{
return true;
}
return false;
});
wait.Until(waiter);
logStep.Source = "WaitForDialogLoad";
logStep.ElementName = dialogTitle;
logStep.Action = "Wait Dialog";
logStep.Data = "";
logStep.Friendly = String.Concat("Wait for Dialog with title
\"", dialogTitle, "\" to load");
logStep.Screenshot = Capture();
logStep.Result = Reporting.Pass;
Reporting.LogStep(logStep);
}
catch (Exception e)
{
logStep.Friendly = e.Message.ToString();
logStep.Result = Reporting.Fail;
Reporting.LogStep(logStep);
}
}
public static void WaitByClassName(string elementName, string findBy)
{
Containers.LogStep logStep = new Containers.LogStep();
try
{
IWebElement element =
Browser.GetDriver.FindElement(By.ClassName(findBy));
int maxWait = 10000;
int counter = 0;
while (element == null && counter < maxWait)
{
element =
Browser.GetDriver.FindElement(By.ClassName(findBy));
Thread.Sleep(500);
counter += 500;
}
logStep.Source = "WaitByClassName";
logStep.ElementName = elementName; // element.GetLogicalName();
logStep.Action = "Displayed";
logStep.Data = element.Displayed.ToString();
logStep.Friendly = "Wait for \"" + logStep.ElementName + "\" to
be displayed on page";
logStep.Result = Reporting.Pass;
Reporting.LogStep(logStep);
}
catch (Exception e)
{
logStep.Friendly = e.Message.ToString();
logStep.Result = Reporting.Fail;
Reporting.LogStep(logStep);
}
}
public static void WaitById(string elementName, string findBy)
{
Containers.LogStep logStep = new Containers.LogStep();
try
{
IWebElement element =
Browser.GetDriver.FindElement(By.Id(findBy));
int maxWait = 10000;
int counter = 0;
while (element == null && counter < maxWait)
{
element = Browser.GetDriver.FindElement(By.Id(findBy));
Thread.Sleep(500);
counter += 500;
}
logStep.Source = "WaitById";
logStep.ElementName = elementName;
logStep.Action = "Displayed";
logStep.Data = element.Displayed.ToString();
logStep.Friendly = "Wait for \"" + logStep.ElementName + "\" to
be displayed on page";
logStep.Result = Reporting.Pass;
Reporting.LogStep(logStep);
}
catch (Exception e)
{
logStep.Friendly = e.Message.ToString();
logStep.Result = Reporting.Fail;
Reporting.LogStep(logStep);
}
}